ACPICA: acpidump: Add ACPI 1.0 RSDP support.
[linux-2.6-block.git] / drivers / acpi / acpica / evxface.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: evxface - External interfaces for ACPI events
4 *
5 *****************************************************************************/
6
7/*
fbb7a2dc 8 * Copyright (C) 2000 - 2014, Intel Corp.
1da177e4
LT
9 * All rights reserved.
10 *
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.
25 *
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.
29 *
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.
42 */
43
839e928f
LZ
44#define EXPORT_ACPI_INTERFACES
45
1da177e4 46#include <acpi/acpi.h>
e2f7a777
LB
47#include "accommon.h"
48#include "acnamesp.h"
49#include "acevents.h"
50#include "acinterp.h"
1da177e4
LT
51
52#define _COMPONENT ACPI_EVENTS
4be44fcd 53ACPI_MODULE_NAME("evxface")
1da177e4 54
1da177e4
LT
55
56/*******************************************************************************
57 *
58 * FUNCTION: acpi_install_notify_handler
59 *
75c8044f 60 * PARAMETERS: device - The device for which notifies will be handled
1da177e4 61 * handler_type - The type of handler:
86ed4bc8
BM
62 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
63 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
64 * ACPI_ALL_NOTIFY: Both System and Device
75c8044f
LZ
65 * handler - Address of the handler
66 * context - Value passed to the handler on each GPE
1da177e4
LT
67 *
68 * RETURN: Status
69 *
86ed4bc8
BM
70 * DESCRIPTION: Install a handler for notifications on an ACPI Device,
71 * thermal_zone, or Processor object.
72 *
73 * NOTES: The Root namespace object may have only one handler for each
74 * type of notify (System/Device). Device/Thermal/Processor objects
75 * may have one device notify handler, and multiple system notify
76 * handlers.
1da177e4
LT
77 *
78 ******************************************************************************/
1da177e4 79acpi_status
4be44fcd
LB
80acpi_install_notify_handler(acpi_handle device,
81 u32 handler_type,
82 acpi_notify_handler handler, void *context)
1da177e4 83{
86ed4bc8
BM
84 struct acpi_namespace_node *node =
85 ACPI_CAST_PTR(struct acpi_namespace_node, device);
4be44fcd 86 union acpi_operand_object *obj_desc;
86ed4bc8 87 union acpi_operand_object *handler_obj;
4be44fcd 88 acpi_status status;
86ed4bc8 89 u32 i;
1da177e4 90
b229cf92 91 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
1da177e4
LT
92
93 /* Parameter validation */
94
86ed4bc8
BM
95 if ((!device) || (!handler) || (!handler_type) ||
96 (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
4be44fcd 97 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
98 }
99
4be44fcd
LB
100 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
101 if (ACPI_FAILURE(status)) {
102 return_ACPI_STATUS(status);
1da177e4
LT
103 }
104
1da177e4
LT
105 /*
106 * Root Object:
107 * Registering a notify handler on the root object indicates that the
9f15fc66 108 * caller wishes to receive notifications for all objects. Note that
86ed4bc8
BM
109 * only one global handler can be registered per notify type.
110 * Ensure that a handler is not already installed.
1da177e4
LT
111 */
112 if (device == ACPI_ROOT_OBJECT) {
86ed4bc8
BM
113 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
114 if (handler_type & (i + 1)) {
115 if (acpi_gbl_global_notify[i].handler) {
116 status = AE_ALREADY_EXISTS;
117 goto unlock_and_exit;
118 }
52fc0b02 119
86ed4bc8
BM
120 acpi_gbl_global_notify[i].handler = handler;
121 acpi_gbl_global_notify[i].context = context;
122 }
1da177e4
LT
123 }
124
86ed4bc8 125 goto unlock_and_exit; /* Global notify handler installed, all done */
1da177e4
LT
126 }
127
128 /*
129 * All Other Objects:
86ed4bc8
BM
130 * Caller will only receive notifications specific to the target
131 * object. Note that only certain object types are allowed to
132 * receive notifications.
1da177e4 133 */
1da177e4 134
86ed4bc8 135 /* Are Notifies allowed on this object? */
1da177e4 136
86ed4bc8
BM
137 if (!acpi_ev_is_notify_object(node)) {
138 status = AE_TYPE;
139 goto unlock_and_exit;
140 }
1da177e4 141
86ed4bc8 142 /* Check for an existing internal object, might not exist */
52fc0b02 143
86ed4bc8
BM
144 obj_desc = acpi_ns_get_attached_object(node);
145 if (!obj_desc) {
1da177e4 146
86ed4bc8
BM
147 /* Create a new object */
148
149 obj_desc = acpi_ut_create_internal_object(node->type);
150 if (!obj_desc) {
151 status = AE_NO_MEMORY;
152 goto unlock_and_exit;
153 }
3f0be671 154
86ed4bc8 155 /* Attach new object to the Node, remove local reference */
3f0be671 156
86ed4bc8
BM
157 status = acpi_ns_attach_object(device, obj_desc, node->type);
158 acpi_ut_remove_reference(obj_desc);
159 if (ACPI_FAILURE(status)) {
160 goto unlock_and_exit;
161 }
162 }
3f0be671 163
86ed4bc8
BM
164 /* Ensure that the handler is not already installed in the lists */
165
166 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
167 if (handler_type & (i + 1)) {
168 handler_obj = obj_desc->common_notify.notify_list[i];
169 while (handler_obj) {
170 if (handler_obj->notify.handler == handler) {
3f0be671
RW
171 status = AE_ALREADY_EXISTS;
172 goto unlock_and_exit;
173 }
174
86ed4bc8 175 handler_obj = handler_obj->notify.next[i];
1da177e4
LT
176 }
177 }
86ed4bc8 178 }
1da177e4 179
86ed4bc8 180 /* Create and populate a new notify handler object */
1da177e4 181
86ed4bc8
BM
182 handler_obj = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
183 if (!handler_obj) {
184 status = AE_NO_MEMORY;
185 goto unlock_and_exit;
186 }
1da177e4 187
86ed4bc8
BM
188 handler_obj->notify.node = node;
189 handler_obj->notify.handler_type = handler_type;
190 handler_obj->notify.handler = handler;
191 handler_obj->notify.context = context;
1da177e4 192
86ed4bc8 193 /* Install the handler at the list head(s) */
1da177e4 194
86ed4bc8
BM
195 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
196 if (handler_type & (i + 1)) {
197 handler_obj->notify.next[i] =
198 obj_desc->common_notify.notify_list[i];
1da177e4 199
86ed4bc8
BM
200 obj_desc->common_notify.notify_list[i] = handler_obj;
201 }
202 }
52fc0b02 203
86ed4bc8 204 /* Add an extra reference if handler was installed in both lists */
1da177e4 205
86ed4bc8
BM
206 if (handler_type == ACPI_ALL_NOTIFY) {
207 acpi_ut_add_reference(handler_obj);
1da177e4
LT
208 }
209
86ed4bc8 210unlock_and_exit:
4be44fcd
LB
211 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
212 return_ACPI_STATUS(status);
1da177e4 213}
1da177e4 214
8313524a 215ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
1da177e4
LT
216
217/*******************************************************************************
218 *
219 * FUNCTION: acpi_remove_notify_handler
220 *
75c8044f 221 * PARAMETERS: device - The device for which the handler is installed
1da177e4 222 * handler_type - The type of handler:
86ed4bc8
BM
223 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
224 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
225 * ACPI_ALL_NOTIFY: Both System and Device
75c8044f 226 * handler - Address of the handler
1da177e4
LT
227 *
228 * RETURN: Status
229 *
230 * DESCRIPTION: Remove a handler for notifies on an ACPI device
231 *
232 ******************************************************************************/
1da177e4 233acpi_status
4be44fcd
LB
234acpi_remove_notify_handler(acpi_handle device,
235 u32 handler_type, acpi_notify_handler handler)
1da177e4 236{
86ed4bc8
BM
237 struct acpi_namespace_node *node =
238 ACPI_CAST_PTR(struct acpi_namespace_node, device);
4be44fcd 239 union acpi_operand_object *obj_desc;
86ed4bc8
BM
240 union acpi_operand_object *handler_obj;
241 union acpi_operand_object *previous_handler_obj;
69c841b6 242 acpi_status status = AE_OK;
86ed4bc8 243 u32 i;
1da177e4 244
b229cf92 245 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
1da177e4
LT
246
247 /* Parameter validation */
248
86ed4bc8
BM
249 if ((!device) || (!handler) || (!handler_type) ||
250 (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
251 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4 252 }
3e8214e5 253
86ed4bc8 254 /* Root Object. Global handlers are removed here */
1da177e4
LT
255
256 if (device == ACPI_ROOT_OBJECT) {
86ed4bc8
BM
257 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
258 if (handler_type & (i + 1)) {
69c841b6
LZ
259 status =
260 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
261 if (ACPI_FAILURE(status)) {
262 return_ACPI_STATUS(status);
263 }
264
86ed4bc8
BM
265 if (!acpi_gbl_global_notify[i].handler ||
266 (acpi_gbl_global_notify[i].handler !=
267 handler)) {
268 status = AE_NOT_EXIST;
269 goto unlock_and_exit;
270 }
1da177e4 271
86ed4bc8
BM
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
273 "Removing global notify handler\n"));
1da177e4 274
86ed4bc8
BM
275 acpi_gbl_global_notify[i].handler = NULL;
276 acpi_gbl_global_notify[i].context = NULL;
69c841b6
LZ
277
278 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
279
280 /* Make sure all deferred notify tasks are completed */
281
282 acpi_os_wait_events_complete();
86ed4bc8 283 }
1da177e4 284 }
1da177e4 285
69c841b6 286 return_ACPI_STATUS(AE_OK);
86ed4bc8 287 }
1da177e4 288
86ed4bc8 289 /* All other objects: Are Notifies allowed on this object? */
1da177e4 290
86ed4bc8 291 if (!acpi_ev_is_notify_object(node)) {
69c841b6 292 return_ACPI_STATUS(AE_TYPE);
86ed4bc8 293 }
1da177e4 294
86ed4bc8 295 /* Must have an existing internal object */
1da177e4 296
86ed4bc8
BM
297 obj_desc = acpi_ns_get_attached_object(node);
298 if (!obj_desc) {
69c841b6 299 return_ACPI_STATUS(AE_NOT_EXIST);
86ed4bc8 300 }
1da177e4 301
86ed4bc8 302 /* Internal object exists. Find the handler and remove it */
1da177e4 303
86ed4bc8
BM
304 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
305 if (handler_type & (i + 1)) {
69c841b6
LZ
306 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
307 if (ACPI_FAILURE(status)) {
308 return_ACPI_STATUS(status);
309 }
310
86ed4bc8
BM
311 handler_obj = obj_desc->common_notify.notify_list[i];
312 previous_handler_obj = NULL;
3f0be671 313
86ed4bc8 314 /* Attempt to find the handler in the handler list */
f6dd9221 315
86ed4bc8
BM
316 while (handler_obj &&
317 (handler_obj->notify.handler != handler)) {
318 previous_handler_obj = handler_obj;
319 handler_obj = handler_obj->notify.next[i];
3f0be671
RW
320 }
321
86ed4bc8
BM
322 if (!handler_obj) {
323 status = AE_NOT_EXIST;
f6dd9221 324 goto unlock_and_exit;
1da177e4 325 }
1da177e4 326
86ed4bc8 327 /* Remove the handler object from the list */
1da177e4 328
86ed4bc8
BM
329 if (previous_handler_obj) { /* Handler is not at the list head */
330 previous_handler_obj->notify.next[i] =
331 handler_obj->notify.next[i];
332 } else { /* Handler is at the list head */
f6dd9221 333
86ed4bc8
BM
334 obj_desc->common_notify.notify_list[i] =
335 handler_obj->notify.next[i];
1da177e4 336 }
1da177e4 337
69c841b6
LZ
338 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
339
340 /* Make sure all deferred notify tasks are completed */
341
342 acpi_os_wait_events_complete();
86ed4bc8 343 acpi_ut_remove_reference(handler_obj);
1da177e4
LT
344 }
345 }
346
69c841b6
LZ
347 return_ACPI_STATUS(status);
348
86ed4bc8 349unlock_and_exit:
4be44fcd
LB
350 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
351 return_ACPI_STATUS(status);
1da177e4 352}
1da177e4 353
8313524a 354ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
1da177e4 355
33620c54
BM
356/*******************************************************************************
357 *
358 * FUNCTION: acpi_install_exception_handler
359 *
ba494bee 360 * PARAMETERS: handler - Pointer to the handler function for the
33620c54
BM
361 * event
362 *
363 * RETURN: Status
364 *
365 * DESCRIPTION: Saves the pointer to the handler function
366 *
367 ******************************************************************************/
368#ifdef ACPI_FUTURE_USAGE
369acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
370{
371 acpi_status status;
372
373 ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
374
375 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
376 if (ACPI_FAILURE(status)) {
377 return_ACPI_STATUS(status);
378 }
379
380 /* Don't allow two handlers. */
381
382 if (acpi_gbl_exception_handler) {
383 status = AE_ALREADY_EXISTS;
384 goto cleanup;
385 }
386
387 /* Install the handler */
388
389 acpi_gbl_exception_handler = handler;
390
10622bf8 391cleanup:
33620c54
BM
392 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
393 return_ACPI_STATUS(status);
394}
395
396ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
397#endif /* ACPI_FUTURE_USAGE */
398
399#if (!ACPI_REDUCED_HARDWARE)
a2fd4b4b
LZ
400/*******************************************************************************
401 *
402 * FUNCTION: acpi_install_sci_handler
403 *
404 * PARAMETERS: address - Address of the handler
405 * context - Value passed to the handler on each SCI
406 *
407 * RETURN: Status
408 *
409 * DESCRIPTION: Install a handler for a System Control Interrupt.
410 *
411 ******************************************************************************/
412acpi_status acpi_install_sci_handler(acpi_sci_handler address, void *context)
413{
414 struct acpi_sci_handler_info *new_sci_handler;
415 struct acpi_sci_handler_info *sci_handler;
416 acpi_cpu_flags flags;
417 acpi_status status;
418
419 ACPI_FUNCTION_TRACE(acpi_install_sci_handler);
420
421 if (!address) {
422 return_ACPI_STATUS(AE_BAD_PARAMETER);
423 }
424
425 /* Allocate and init a handler object */
426
427 new_sci_handler = ACPI_ALLOCATE(sizeof(struct acpi_sci_handler_info));
428 if (!new_sci_handler) {
429 return_ACPI_STATUS(AE_NO_MEMORY);
430 }
431
432 new_sci_handler->address = address;
433 new_sci_handler->context = context;
434
435 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
436 if (ACPI_FAILURE(status)) {
437 goto exit;
438 }
439
440 /* Lock list during installation */
441
442 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
443 sci_handler = acpi_gbl_sci_handler_list;
444
445 /* Ensure handler does not already exist */
446
447 while (sci_handler) {
448 if (address == sci_handler->address) {
449 status = AE_ALREADY_EXISTS;
450 goto unlock_and_exit;
451 }
452
453 sci_handler = sci_handler->next;
454 }
455
456 /* Install the new handler into the global list (at head) */
457
458 new_sci_handler->next = acpi_gbl_sci_handler_list;
459 acpi_gbl_sci_handler_list = new_sci_handler;
460
10622bf8 461unlock_and_exit:
a2fd4b4b
LZ
462
463 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
464 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
465
10622bf8 466exit:
a2fd4b4b
LZ
467 if (ACPI_FAILURE(status)) {
468 ACPI_FREE(new_sci_handler);
469 }
470 return_ACPI_STATUS(status);
471}
472
1d44efab
BM
473ACPI_EXPORT_SYMBOL(acpi_install_sci_handler)
474
a2fd4b4b
LZ
475/*******************************************************************************
476 *
477 * FUNCTION: acpi_remove_sci_handler
478 *
479 * PARAMETERS: address - Address of the handler
480 *
481 * RETURN: Status
482 *
483 * DESCRIPTION: Remove a handler for a System Control Interrupt.
484 *
485 ******************************************************************************/
a2fd4b4b
LZ
486acpi_status acpi_remove_sci_handler(acpi_sci_handler address)
487{
488 struct acpi_sci_handler_info *prev_sci_handler;
489 struct acpi_sci_handler_info *next_sci_handler;
490 acpi_cpu_flags flags;
491 acpi_status status;
492
493 ACPI_FUNCTION_TRACE(acpi_remove_sci_handler);
494
495 if (!address) {
496 return_ACPI_STATUS(AE_BAD_PARAMETER);
497 }
498
499 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
500 if (ACPI_FAILURE(status)) {
501 return_ACPI_STATUS(status);
502 }
503
504 /* Remove the SCI handler with lock */
505
506 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
507
508 prev_sci_handler = NULL;
509 next_sci_handler = acpi_gbl_sci_handler_list;
510 while (next_sci_handler) {
511 if (next_sci_handler->address == address) {
512
513 /* Unlink and free the SCI handler info block */
514
515 if (prev_sci_handler) {
516 prev_sci_handler->next = next_sci_handler->next;
517 } else {
518 acpi_gbl_sci_handler_list =
519 next_sci_handler->next;
520 }
521
522 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
523 ACPI_FREE(next_sci_handler);
524 goto unlock_and_exit;
525 }
526
527 prev_sci_handler = next_sci_handler;
528 next_sci_handler = next_sci_handler->next;
529 }
530
531 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
532 status = AE_NOT_EXIST;
533
10622bf8 534unlock_and_exit:
a2fd4b4b
LZ
535 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
536 return_ACPI_STATUS(status);
537}
538
1d44efab
BM
539ACPI_EXPORT_SYMBOL(acpi_remove_sci_handler)
540
33620c54
BM
541/*******************************************************************************
542 *
543 * FUNCTION: acpi_install_global_event_handler
544 *
ba494bee
BM
545 * PARAMETERS: handler - Pointer to the global event handler function
546 * context - Value passed to the handler on each event
33620c54
BM
547 *
548 * RETURN: Status
549 *
550 * DESCRIPTION: Saves the pointer to the handler function. The global handler
551 * is invoked upon each incoming GPE and Fixed Event. It is
552 * invoked at interrupt level at the time of the event dispatch.
553 * Can be used to update event counters, etc.
554 *
555 ******************************************************************************/
556acpi_status
644ef74e 557acpi_install_global_event_handler(acpi_gbl_event_handler handler, void *context)
33620c54
BM
558{
559 acpi_status status;
560
561 ACPI_FUNCTION_TRACE(acpi_install_global_event_handler);
562
563 /* Parameter validation */
564
565 if (!handler) {
566 return_ACPI_STATUS(AE_BAD_PARAMETER);
567 }
568
569 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
570 if (ACPI_FAILURE(status)) {
571 return_ACPI_STATUS(status);
572 }
573
574 /* Don't allow two handlers. */
575
576 if (acpi_gbl_global_event_handler) {
577 status = AE_ALREADY_EXISTS;
578 goto cleanup;
579 }
580
581 acpi_gbl_global_event_handler = handler;
582 acpi_gbl_global_event_handler_context = context;
583
10622bf8 584cleanup:
33620c54
BM
585 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
586 return_ACPI_STATUS(status);
587}
588
589ACPI_EXPORT_SYMBOL(acpi_install_global_event_handler)
590
591/*******************************************************************************
592 *
593 * FUNCTION: acpi_install_fixed_event_handler
594 *
ba494bee
BM
595 * PARAMETERS: event - Event type to enable.
596 * handler - Pointer to the handler function for the
33620c54 597 * event
ba494bee 598 * context - Value passed to the handler on each GPE
33620c54
BM
599 *
600 * RETURN: Status
601 *
602 * DESCRIPTION: Saves the pointer to the handler function and then enables the
603 * event.
604 *
605 ******************************************************************************/
606acpi_status
607acpi_install_fixed_event_handler(u32 event,
608 acpi_event_handler handler, void *context)
609{
610 acpi_status status;
611
612 ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
613
614 /* Parameter validation */
615
616 if (event > ACPI_EVENT_MAX) {
617 return_ACPI_STATUS(AE_BAD_PARAMETER);
618 }
619
620 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
621 if (ACPI_FAILURE(status)) {
622 return_ACPI_STATUS(status);
623 }
624
d4d32195 625 /* Do not allow multiple handlers */
33620c54 626
d4d32195 627 if (acpi_gbl_fixed_event_handlers[event].handler) {
33620c54
BM
628 status = AE_ALREADY_EXISTS;
629 goto cleanup;
630 }
631
632 /* Install the handler before enabling the event */
633
634 acpi_gbl_fixed_event_handlers[event].handler = handler;
635 acpi_gbl_fixed_event_handlers[event].context = context;
636
637 status = acpi_clear_event(event);
638 if (ACPI_SUCCESS(status))
639 status = acpi_enable_event(event, 0);
640 if (ACPI_FAILURE(status)) {
d4d32195
BM
641 ACPI_WARNING((AE_INFO,
642 "Could not enable fixed event - %s (%u)",
643 acpi_ut_get_event_name(event), event));
33620c54
BM
644
645 /* Remove the handler */
646
647 acpi_gbl_fixed_event_handlers[event].handler = NULL;
648 acpi_gbl_fixed_event_handlers[event].context = NULL;
649 } else {
650 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
d4d32195
BM
651 "Enabled fixed event %s (%X), Handler=%p\n",
652 acpi_ut_get_event_name(event), event,
33620c54
BM
653 handler));
654 }
655
10622bf8 656cleanup:
33620c54
BM
657 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
658 return_ACPI_STATUS(status);
659}
660
661ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
662
663/*******************************************************************************
664 *
665 * FUNCTION: acpi_remove_fixed_event_handler
666 *
ba494bee
BM
667 * PARAMETERS: event - Event type to disable.
668 * handler - Address of the handler
33620c54
BM
669 *
670 * RETURN: Status
671 *
672 * DESCRIPTION: Disables the event and unregisters the event handler.
673 *
674 ******************************************************************************/
675acpi_status
676acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
677{
678 acpi_status status = AE_OK;
679
680 ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
681
682 /* Parameter validation */
683
684 if (event > ACPI_EVENT_MAX) {
685 return_ACPI_STATUS(AE_BAD_PARAMETER);
686 }
687
688 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
689 if (ACPI_FAILURE(status)) {
690 return_ACPI_STATUS(status);
691 }
692
693 /* Disable the event before removing the handler */
694
695 status = acpi_disable_event(event, 0);
696
697 /* Always Remove the handler */
698
699 acpi_gbl_fixed_event_handlers[event].handler = NULL;
700 acpi_gbl_fixed_event_handlers[event].context = NULL;
701
702 if (ACPI_FAILURE(status)) {
703 ACPI_WARNING((AE_INFO,
d4d32195
BM
704 "Could not disable fixed event - %s (%u)",
705 acpi_ut_get_event_name(event), event));
33620c54 706 } else {
d4d32195
BM
707 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
708 "Disabled fixed event - %s (%X)\n",
709 acpi_ut_get_event_name(event), event));
33620c54
BM
710 }
711
712 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
713 return_ACPI_STATUS(status);
714}
715
716ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
717
1da177e4
LT
718/*******************************************************************************
719 *
720 * FUNCTION: acpi_install_gpe_handler
721 *
44f6c012
RM
722 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
723 * defined GPEs)
724 * gpe_number - The GPE number within the GPE block
ba494bee 725 * type - Whether this GPE should be treated as an
1da177e4 726 * edge- or level-triggered interrupt.
ba494bee
BM
727 * address - Address of the handler
728 * context - Value passed to the handler on each GPE
1da177e4
LT
729 *
730 * RETURN: Status
731 *
732 * DESCRIPTION: Install a handler for a General Purpose Event.
733 *
734 ******************************************************************************/
1da177e4 735acpi_status
4be44fcd
LB
736acpi_install_gpe_handler(acpi_handle gpe_device,
737 u32 gpe_number,
8b6cd8ad 738 u32 type, acpi_gpe_handler address, void *context)
1da177e4 739{
4be44fcd 740 struct acpi_gpe_event_info *gpe_event_info;
8b6cd8ad 741 struct acpi_gpe_handler_info *handler;
4be44fcd 742 acpi_status status;
b8e4d893 743 acpi_cpu_flags flags;
1da177e4 744
b229cf92 745 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
1da177e4
LT
746
747 /* Parameter validation */
748
0f849d2c
LM
749 if ((!address) || (type & ~ACPI_GPE_XRUPT_TYPE_MASK)) {
750 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
751 }
752
4be44fcd
LB
753 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
754 if (ACPI_FAILURE(status)) {
0f849d2c 755 return_ACPI_STATUS(status);
1da177e4
LT
756 }
757
75c8044f 758 /* Allocate and init handler object (before lock) */
28f4f8a9 759
8b6cd8ad 760 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_handler_info));
28f4f8a9
RW
761 if (!handler) {
762 status = AE_NO_MEMORY;
763 goto unlock_and_exit;
764 }
765
766 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
767
1da177e4
LT
768 /* Ensure that we have a valid GPE number */
769
4be44fcd 770 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
1da177e4
LT
771 if (!gpe_event_info) {
772 status = AE_BAD_PARAMETER;
28f4f8a9 773 goto free_and_exit;
1da177e4
LT
774 }
775
776 /* Make sure that there isn't a handler there already */
777
4be44fcd
LB
778 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
779 ACPI_GPE_DISPATCH_HANDLER) {
1da177e4 780 status = AE_ALREADY_EXISTS;
28f4f8a9 781 goto free_and_exit;
1da177e4
LT
782 }
783
4be44fcd
LB
784 handler->address = address;
785 handler->context = context;
1da177e4 786 handler->method_node = gpe_event_info->dispatch.method_node;
3e8214e5
LZ
787 handler->original_flags = (u8)(gpe_event_info->flags &
788 (ACPI_GPE_XRUPT_TYPE_MASK |
789 ACPI_GPE_DISPATCH_MASK));
28f4f8a9
RW
790
791 /*
75c8044f 792 * If the GPE is associated with a method, it may have been enabled
a2100801
RW
793 * automatically during initialization, in which case it has to be
794 * disabled now to avoid spurious execution of the handler.
28f4f8a9
RW
795 */
796
3a37898d 797 if ((handler->original_flags & ACPI_GPE_DISPATCH_METHOD)
a2100801 798 && gpe_event_info->runtime_count) {
3a37898d
LM
799 handler->originally_enabled = 1;
800 (void)acpi_ev_remove_gpe_reference(gpe_event_info);
a2100801 801 }
1da177e4 802
1da177e4
LT
803 /* Install the handler */
804
1da177e4
LT
805 gpe_event_info->dispatch.handler = handler;
806
75c8044f 807 /* Setup up dispatch flags to indicate handler (vs. method/notify) */
1da177e4 808
d4913dc6
BM
809 gpe_event_info->flags &=
810 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
1da177e4
LT
811 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
812
4be44fcd 813 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
1da177e4 814
0f849d2c 815unlock_and_exit:
4be44fcd
LB
816 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
817 return_ACPI_STATUS(status);
28f4f8a9
RW
818
819free_and_exit:
820 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
821 ACPI_FREE(handler);
822 goto unlock_and_exit;
1da177e4 823}
1da177e4 824
8313524a 825ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
1da177e4
LT
826
827/*******************************************************************************
828 *
829 * FUNCTION: acpi_remove_gpe_handler
830 *
44f6c012
RM
831 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
832 * defined GPEs)
833 * gpe_number - The event to remove a handler
ba494bee 834 * address - Address of the handler
1da177e4
LT
835 *
836 * RETURN: Status
837 *
838 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
839 *
840 ******************************************************************************/
1da177e4 841acpi_status
4be44fcd 842acpi_remove_gpe_handler(acpi_handle gpe_device,
8b6cd8ad 843 u32 gpe_number, acpi_gpe_handler address)
1da177e4 844{
4be44fcd 845 struct acpi_gpe_event_info *gpe_event_info;
8b6cd8ad 846 struct acpi_gpe_handler_info *handler;
4be44fcd 847 acpi_status status;
b8e4d893 848 acpi_cpu_flags flags;
1da177e4 849
b229cf92 850 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
1da177e4
LT
851
852 /* Parameter validation */
853
854 if (!address) {
4be44fcd 855 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
856 }
857
4be44fcd
LB
858 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
859 if (ACPI_FAILURE(status)) {
860 return_ACPI_STATUS(status);
1da177e4
LT
861 }
862
28f4f8a9
RW
863 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
864
1da177e4
LT
865 /* Ensure that we have a valid GPE number */
866
4be44fcd 867 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
1da177e4
LT
868 if (!gpe_event_info) {
869 status = AE_BAD_PARAMETER;
870 goto unlock_and_exit;
871 }
872
873 /* Make sure that a handler is indeed installed */
874
4be44fcd
LB
875 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
876 ACPI_GPE_DISPATCH_HANDLER) {
1da177e4
LT
877 status = AE_NOT_EXIST;
878 goto unlock_and_exit;
879 }
880
881 /* Make sure that the installed handler is the same */
882
883 if (gpe_event_info->dispatch.handler->address != address) {
884 status = AE_BAD_PARAMETER;
885 goto unlock_and_exit;
886 }
887
1da177e4
LT
888 /* Remove the handler */
889
1da177e4
LT
890 handler = gpe_event_info->dispatch.handler;
891
892 /* Restore Method node (if any), set dispatch flags */
893
894 gpe_event_info->dispatch.method_node = handler->method_node;
28f4f8a9
RW
895 gpe_event_info->flags &=
896 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
3a37898d 897 gpe_event_info->flags |= handler->original_flags;
28f4f8a9
RW
898
899 /*
a2100801
RW
900 * If the GPE was previously associated with a method and it was
901 * enabled, it should be enabled at this point to restore the
902 * post-initialization configuration.
28f4f8a9 903 */
3e8214e5
LZ
904 if ((handler->original_flags & ACPI_GPE_DISPATCH_METHOD) &&
905 handler->originally_enabled) {
3a37898d 906 (void)acpi_ev_add_gpe_reference(gpe_event_info);
3e8214e5 907 }
1da177e4 908
69c841b6
LZ
909 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
910 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
911
912 /* Make sure all deferred GPE tasks are completed */
913
914 acpi_os_wait_events_complete();
915
1da177e4
LT
916 /* Now we can free the handler object */
917
8313524a 918 ACPI_FREE(handler);
69c841b6 919 return_ACPI_STATUS(status);
1da177e4 920
28f4f8a9
RW
921unlock_and_exit:
922 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
923
4be44fcd
LB
924 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
925 return_ACPI_STATUS(status);
1da177e4 926}
1da177e4 927
8313524a 928ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
1da177e4
LT
929
930/*******************************************************************************
931 *
932 * FUNCTION: acpi_acquire_global_lock
933 *
ba494bee
BM
934 * PARAMETERS: timeout - How long the caller is willing to wait
935 * handle - Where the handle to the lock is returned
44f6c012 936 * (if acquired)
1da177e4
LT
937 *
938 * RETURN: Status
939 *
940 * DESCRIPTION: Acquire the ACPI Global Lock
941 *
ba886cd4
BM
942 * Note: Allows callers with the same thread ID to acquire the global lock
943 * multiple times. In other words, externally, the behavior of the global lock
944 * is identical to an AML mutex. On the first acquire, a new handle is
945 * returned. On any subsequent calls to acquire by the same thread, the same
946 * handle is returned.
947 *
1da177e4 948 ******************************************************************************/
4be44fcd 949acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
1da177e4 950{
4be44fcd 951 acpi_status status;
1da177e4
LT
952
953 if (!handle) {
954 return (AE_BAD_PARAMETER);
955 }
956
4d2acd9e 957 /* Must lock interpreter to prevent race conditions */
1da177e4 958
4d2acd9e 959 acpi_ex_enter_interpreter();
ba886cd4
BM
960
961 status = acpi_ex_acquire_mutex_object(timeout,
962 acpi_gbl_global_lock_mutex,
963 acpi_os_get_thread_id());
1da177e4 964
4be44fcd 965 if (ACPI_SUCCESS(status)) {
ba886cd4 966
e5567afa 967 /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
ba886cd4 968
1da177e4
LT
969 *handle = acpi_gbl_global_lock_handle;
970 }
971
ba886cd4 972 acpi_ex_exit_interpreter();
1da177e4
LT
973 return (status);
974}
1da177e4 975
8313524a 976ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
1da177e4
LT
977
978/*******************************************************************************
979 *
980 * FUNCTION: acpi_release_global_lock
981 *
ba494bee 982 * PARAMETERS: handle - Returned from acpi_acquire_global_lock
1da177e4
LT
983 *
984 * RETURN: Status
985 *
44f6c012 986 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
1da177e4
LT
987 *
988 ******************************************************************************/
4be44fcd 989acpi_status acpi_release_global_lock(u32 handle)
1da177e4 990{
4be44fcd 991 acpi_status status;
1da177e4 992
f02e9fa1 993 if (!handle || (handle != acpi_gbl_global_lock_handle)) {
1da177e4
LT
994 return (AE_NOT_ACQUIRED);
995 }
996
ba886cd4 997 status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
1da177e4
LT
998 return (status);
999}
1da177e4 1000
8313524a 1001ACPI_EXPORT_SYMBOL(acpi_release_global_lock)
33620c54 1002#endif /* !ACPI_REDUCED_HARDWARE */