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