ACPICA: Update internal namespace node/handle interfaces
[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/*
75a44ce0 8 * Copyright (C) 2000 - 2008, 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
1da177e4 44#include <acpi/acpi.h>
e2f7a777
LB
45#include "accommon.h"
46#include "acnamesp.h"
47#include "acevents.h"
48#include "acinterp.h"
1da177e4
LT
49
50#define _COMPONENT ACPI_EVENTS
4be44fcd 51ACPI_MODULE_NAME("evxface")
1da177e4
LT
52
53/*******************************************************************************
54 *
55 * FUNCTION: acpi_install_exception_handler
56 *
57 * PARAMETERS: Handler - Pointer to the handler function for the
58 * event
59 *
60 * RETURN: Status
61 *
62 * DESCRIPTION: Saves the pointer to the handler function
63 *
64 ******************************************************************************/
65#ifdef ACPI_FUTURE_USAGE
4be44fcd 66acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
1da177e4 67{
4be44fcd 68 acpi_status status;
1da177e4 69
b229cf92 70 ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
1da177e4 71
4be44fcd
LB
72 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
73 if (ACPI_FAILURE(status)) {
74 return_ACPI_STATUS(status);
1da177e4
LT
75 }
76
77 /* Don't allow two handlers. */
78
79 if (acpi_gbl_exception_handler) {
80 status = AE_ALREADY_EXISTS;
81 goto cleanup;
82 }
83
84 /* Install the handler */
85
86 acpi_gbl_exception_handler = handler;
87
4be44fcd
LB
88 cleanup:
89 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
90 return_ACPI_STATUS(status);
1da177e4 91}
8313524a
BM
92
93ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
4be44fcd 94#endif /* ACPI_FUTURE_USAGE */
1da177e4
LT
95/*******************************************************************************
96 *
97 * FUNCTION: acpi_install_fixed_event_handler
98 *
99 * PARAMETERS: Event - Event type to enable.
100 * Handler - Pointer to the handler function for the
101 * event
102 * Context - Value passed to the handler on each GPE
103 *
104 * RETURN: Status
105 *
106 * DESCRIPTION: Saves the pointer to the handler function and then enables the
107 * event.
108 *
109 ******************************************************************************/
1da177e4 110acpi_status
4be44fcd
LB
111acpi_install_fixed_event_handler(u32 event,
112 acpi_event_handler handler, void *context)
1da177e4 113{
4be44fcd 114 acpi_status status;
1da177e4 115
b229cf92 116 ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
1da177e4
LT
117
118 /* Parameter validation */
119
120 if (event > ACPI_EVENT_MAX) {
4be44fcd 121 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
122 }
123
4be44fcd
LB
124 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
125 if (ACPI_FAILURE(status)) {
126 return_ACPI_STATUS(status);
1da177e4
LT
127 }
128
129 /* Don't allow two handlers. */
130
131 if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
132 status = AE_ALREADY_EXISTS;
133 goto cleanup;
134 }
135
136 /* Install the handler before enabling the event */
137
138 acpi_gbl_fixed_event_handlers[event].handler = handler;
139 acpi_gbl_fixed_event_handlers[event].context = context;
140
4be44fcd 141 status = acpi_clear_event(event);
1da177e4 142 if (ACPI_SUCCESS(status))
4be44fcd
LB
143 status = acpi_enable_event(event, 0);
144 if (ACPI_FAILURE(status)) {
b8e4d893
BM
145 ACPI_WARNING((AE_INFO, "Could not enable fixed event %X",
146 event));
1da177e4
LT
147
148 /* Remove the handler */
149
150 acpi_gbl_fixed_event_handlers[event].handler = NULL;
151 acpi_gbl_fixed_event_handlers[event].context = NULL;
4be44fcd
LB
152 } else {
153 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
154 "Enabled fixed event %X, Handler=%p\n", event,
155 handler));
1da177e4
LT
156 }
157
4be44fcd
LB
158 cleanup:
159 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
160 return_ACPI_STATUS(status);
1da177e4 161}
1da177e4 162
8313524a 163ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
1da177e4
LT
164
165/*******************************************************************************
166 *
167 * FUNCTION: acpi_remove_fixed_event_handler
168 *
169 * PARAMETERS: Event - Event type to disable.
170 * Handler - Address of the handler
171 *
172 * RETURN: Status
173 *
174 * DESCRIPTION: Disables the event and unregisters the event handler.
175 *
176 ******************************************************************************/
1da177e4 177acpi_status
4be44fcd 178acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
1da177e4 179{
4be44fcd 180 acpi_status status = AE_OK;
1da177e4 181
b229cf92 182 ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
1da177e4
LT
183
184 /* Parameter validation */
185
186 if (event > ACPI_EVENT_MAX) {
4be44fcd 187 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
188 }
189
4be44fcd
LB
190 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
191 if (ACPI_FAILURE(status)) {
192 return_ACPI_STATUS(status);
1da177e4
LT
193 }
194
195 /* Disable the event before removing the handler */
196
4be44fcd 197 status = acpi_disable_event(event, 0);
1da177e4
LT
198
199 /* Always Remove the handler */
200
201 acpi_gbl_fixed_event_handlers[event].handler = NULL;
202 acpi_gbl_fixed_event_handlers[event].context = NULL;
203
4be44fcd 204 if (ACPI_FAILURE(status)) {
b8e4d893
BM
205 ACPI_WARNING((AE_INFO,
206 "Could not write to fixed event enable register %X",
207 event));
4be44fcd 208 } else {
4a90c7e8 209 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
4be44fcd 210 event));
1da177e4
LT
211 }
212
4be44fcd
LB
213 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
214 return_ACPI_STATUS(status);
1da177e4 215}
1da177e4 216
8313524a 217ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
1da177e4
LT
218
219/*******************************************************************************
220 *
221 * FUNCTION: acpi_install_notify_handler
222 *
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
230 *
231 * RETURN: Status
232 *
233 * DESCRIPTION: Install a handler for notifies on an ACPI device
234 *
235 ******************************************************************************/
1da177e4 236acpi_status
4be44fcd
LB
237acpi_install_notify_handler(acpi_handle device,
238 u32 handler_type,
239 acpi_notify_handler handler, void *context)
1da177e4 240{
4be44fcd
LB
241 union acpi_operand_object *obj_desc;
242 union acpi_operand_object *notify_obj;
243 struct acpi_namespace_node *node;
244 acpi_status status;
1da177e4 245
b229cf92 246 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
1da177e4
LT
247
248 /* Parameter validation */
249
4be44fcd
LB
250 if ((!device) ||
251 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
252 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
253 }
254
4be44fcd
LB
255 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
256 if (ACPI_FAILURE(status)) {
257 return_ACPI_STATUS(status);
1da177e4
LT
258 }
259
260 /* Convert and validate the device handle */
261
f24b664d 262 node = acpi_ns_validate_handle(device);
1da177e4
LT
263 if (!node) {
264 status = AE_BAD_PARAMETER;
265 goto unlock_and_exit;
266 }
267
268 /*
269 * Root Object:
270 * Registering a notify handler on the root object indicates that the
9f15fc66 271 * caller wishes to receive notifications for all objects. Note that
1da177e4
LT
272 * only one <external> global handler can be regsitered (per notify type).
273 */
274 if (device == ACPI_ROOT_OBJECT) {
52fc0b02 275
1da177e4
LT
276 /* Make sure the handler is not already installed */
277
278 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
4be44fcd
LB
279 acpi_gbl_system_notify.handler) ||
280 ((handler_type & ACPI_DEVICE_NOTIFY) &&
281 acpi_gbl_device_notify.handler)) {
1da177e4
LT
282 status = AE_ALREADY_EXISTS;
283 goto unlock_and_exit;
284 }
285
286 if (handler_type & ACPI_SYSTEM_NOTIFY) {
4be44fcd 287 acpi_gbl_system_notify.node = node;
1da177e4
LT
288 acpi_gbl_system_notify.handler = handler;
289 acpi_gbl_system_notify.context = context;
290 }
291
292 if (handler_type & ACPI_DEVICE_NOTIFY) {
4be44fcd 293 acpi_gbl_device_notify.node = node;
1da177e4
LT
294 acpi_gbl_device_notify.handler = handler;
295 acpi_gbl_device_notify.context = context;
296 }
297
298 /* Global notify handler installed */
299 }
300
301 /*
302 * All Other Objects:
303 * Caller will only receive notifications specific to the target object.
304 * Note that only certain object types can receive notifications.
305 */
306 else {
307 /* Notifies allowed on this object? */
308
4be44fcd 309 if (!acpi_ev_is_notify_object(node)) {
1da177e4
LT
310 status = AE_TYPE;
311 goto unlock_and_exit;
312 }
313
314 /* Check for an existing internal object */
315
4be44fcd 316 obj_desc = acpi_ns_get_attached_object(node);
1da177e4 317 if (obj_desc) {
52fc0b02 318
1da177e4
LT
319 /* Object exists - make sure there's no handler */
320
321 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
4be44fcd
LB
322 obj_desc->common_notify.system_notify) ||
323 ((handler_type & ACPI_DEVICE_NOTIFY) &&
324 obj_desc->common_notify.device_notify)) {
1da177e4
LT
325 status = AE_ALREADY_EXISTS;
326 goto unlock_and_exit;
327 }
4be44fcd 328 } else {
1da177e4
LT
329 /* Create a new object */
330
4be44fcd 331 obj_desc = acpi_ut_create_internal_object(node->type);
1da177e4
LT
332 if (!obj_desc) {
333 status = AE_NO_MEMORY;
334 goto unlock_and_exit;
335 }
336
337 /* Attach new object to the Node */
338
4be44fcd
LB
339 status =
340 acpi_ns_attach_object(device, obj_desc, node->type);
1da177e4
LT
341
342 /* Remove local reference to the object */
343
4be44fcd
LB
344 acpi_ut_remove_reference(obj_desc);
345 if (ACPI_FAILURE(status)) {
1da177e4
LT
346 goto unlock_and_exit;
347 }
348 }
349
350 /* Install the handler */
351
4be44fcd
LB
352 notify_obj =
353 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
1da177e4
LT
354 if (!notify_obj) {
355 status = AE_NO_MEMORY;
356 goto unlock_and_exit;
357 }
358
4be44fcd 359 notify_obj->notify.node = node;
1da177e4
LT
360 notify_obj->notify.handler = handler;
361 notify_obj->notify.context = context;
362
363 if (handler_type & ACPI_SYSTEM_NOTIFY) {
364 obj_desc->common_notify.system_notify = notify_obj;
365 }
366
367 if (handler_type & ACPI_DEVICE_NOTIFY) {
368 obj_desc->common_notify.device_notify = notify_obj;
369 }
370
371 if (handler_type == ACPI_ALL_NOTIFY) {
52fc0b02 372
1da177e4
LT
373 /* Extra ref if installed in both */
374
4be44fcd 375 acpi_ut_add_reference(notify_obj);
1da177e4
LT
376 }
377 }
378
4be44fcd
LB
379 unlock_and_exit:
380 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
381 return_ACPI_STATUS(status);
1da177e4 382}
1da177e4 383
8313524a 384ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
1da177e4
LT
385
386/*******************************************************************************
387 *
388 * FUNCTION: acpi_remove_notify_handler
389 *
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
396 *
397 * RETURN: Status
398 *
399 * DESCRIPTION: Remove a handler for notifies on an ACPI device
400 *
401 ******************************************************************************/
1da177e4 402acpi_status
4be44fcd
LB
403acpi_remove_notify_handler(acpi_handle device,
404 u32 handler_type, acpi_notify_handler handler)
1da177e4 405{
4be44fcd
LB
406 union acpi_operand_object *notify_obj;
407 union acpi_operand_object *obj_desc;
408 struct acpi_namespace_node *node;
409 acpi_status status;
1da177e4 410
b229cf92 411 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
1da177e4
LT
412
413 /* Parameter validation */
414
4be44fcd
LB
415 if ((!device) ||
416 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
e8406b44
TR
417 status = AE_BAD_PARAMETER;
418 goto exit;
1da177e4
LT
419 }
420
4be44fcd
LB
421 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
422 if (ACPI_FAILURE(status)) {
e8406b44 423 goto exit;
1da177e4
LT
424 }
425
426 /* Convert and validate the device handle */
427
f24b664d 428 node = acpi_ns_validate_handle(device);
1da177e4
LT
429 if (!node) {
430 status = AE_BAD_PARAMETER;
f6dd9221 431 goto unlock_and_exit;
1da177e4
LT
432 }
433
434 /* Root Object */
435
436 if (device == ACPI_ROOT_OBJECT) {
4be44fcd 437 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4a90c7e8 438 "Removing notify handler for namespace root object\n"));
1da177e4
LT
439
440 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
4be44fcd
LB
441 !acpi_gbl_system_notify.handler) ||
442 ((handler_type & ACPI_DEVICE_NOTIFY) &&
443 !acpi_gbl_device_notify.handler)) {
1da177e4 444 status = AE_NOT_EXIST;
f6dd9221 445 goto unlock_and_exit;
1da177e4
LT
446 }
447
448 /* Make sure all deferred tasks are completed */
449
4be44fcd 450 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
1da177e4 451 acpi_os_wait_events_complete(NULL);
4be44fcd
LB
452 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
453 if (ACPI_FAILURE(status)) {
e8406b44 454 goto exit;
4be44fcd 455 }
1da177e4
LT
456
457 if (handler_type & ACPI_SYSTEM_NOTIFY) {
4be44fcd 458 acpi_gbl_system_notify.node = NULL;
1da177e4
LT
459 acpi_gbl_system_notify.handler = NULL;
460 acpi_gbl_system_notify.context = NULL;
461 }
462
463 if (handler_type & ACPI_DEVICE_NOTIFY) {
4be44fcd 464 acpi_gbl_device_notify.node = NULL;
1da177e4
LT
465 acpi_gbl_device_notify.handler = NULL;
466 acpi_gbl_device_notify.context = NULL;
467 }
468 }
469
470 /* All Other Objects */
471
472 else {
473 /* Notifies allowed on this object? */
474
4be44fcd 475 if (!acpi_ev_is_notify_object(node)) {
1da177e4 476 status = AE_TYPE;
f6dd9221 477 goto unlock_and_exit;
1da177e4
LT
478 }
479
480 /* Check for an existing internal object */
481
4be44fcd 482 obj_desc = acpi_ns_get_attached_object(node);
1da177e4
LT
483 if (!obj_desc) {
484 status = AE_NOT_EXIST;
f6dd9221 485 goto unlock_and_exit;
1da177e4
LT
486 }
487
488 /* Object exists - make sure there's an existing handler */
489
490 if (handler_type & ACPI_SYSTEM_NOTIFY) {
491 notify_obj = obj_desc->common_notify.system_notify;
f6dd9221
BM
492 if (!notify_obj) {
493 status = AE_NOT_EXIST;
494 goto unlock_and_exit;
495 }
496
497 if (notify_obj->notify.handler != handler) {
1da177e4 498 status = AE_BAD_PARAMETER;
f6dd9221 499 goto unlock_and_exit;
1da177e4
LT
500 }
501 /* Make sure all deferred tasks are completed */
502
4be44fcd 503 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
1da177e4 504 acpi_os_wait_events_complete(NULL);
4be44fcd
LB
505 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
506 if (ACPI_FAILURE(status)) {
e8406b44 507 goto exit;
4be44fcd 508 }
1da177e4
LT
509
510 /* Remove the handler */
511 obj_desc->common_notify.system_notify = NULL;
4be44fcd 512 acpi_ut_remove_reference(notify_obj);
1da177e4
LT
513 }
514
515 if (handler_type & ACPI_DEVICE_NOTIFY) {
516 notify_obj = obj_desc->common_notify.device_notify;
f6dd9221
BM
517 if (!notify_obj) {
518 status = AE_NOT_EXIST;
519 goto unlock_and_exit;
520 }
521
522 if (notify_obj->notify.handler != handler) {
1da177e4 523 status = AE_BAD_PARAMETER;
f6dd9221 524 goto unlock_and_exit;
1da177e4
LT
525 }
526 /* Make sure all deferred tasks are completed */
527
4be44fcd 528 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
1da177e4 529 acpi_os_wait_events_complete(NULL);
4be44fcd
LB
530 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
531 if (ACPI_FAILURE(status)) {
e8406b44 532 goto exit;
4be44fcd 533 }
1da177e4
LT
534
535 /* Remove the handler */
536 obj_desc->common_notify.device_notify = NULL;
4be44fcd 537 acpi_ut_remove_reference(notify_obj);
1da177e4
LT
538 }
539 }
540
f6dd9221 541 unlock_and_exit:
4be44fcd 542 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
f6dd9221 543 exit:
e8406b44
TR
544 if (ACPI_FAILURE(status))
545 ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
4be44fcd 546 return_ACPI_STATUS(status);
1da177e4 547}
1da177e4 548
8313524a 549ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
1da177e4
LT
550
551/*******************************************************************************
552 *
553 * FUNCTION: acpi_install_gpe_handler
554 *
44f6c012
RM
555 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
556 * defined GPEs)
557 * gpe_number - The GPE number within the GPE block
1da177e4
LT
558 * Type - Whether this GPE should be treated as an
559 * edge- or level-triggered interrupt.
560 * Address - Address of the handler
561 * Context - Value passed to the handler on each GPE
562 *
563 * RETURN: Status
564 *
565 * DESCRIPTION: Install a handler for a General Purpose Event.
566 *
567 ******************************************************************************/
1da177e4 568acpi_status
4be44fcd
LB
569acpi_install_gpe_handler(acpi_handle gpe_device,
570 u32 gpe_number,
571 u32 type, acpi_event_handler address, void *context)
1da177e4 572{
4be44fcd
LB
573 struct acpi_gpe_event_info *gpe_event_info;
574 struct acpi_handler_info *handler;
575 acpi_status status;
b8e4d893 576 acpi_cpu_flags flags;
1da177e4 577
b229cf92 578 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
1da177e4
LT
579
580 /* Parameter validation */
581
582 if ((!address) || (type > ACPI_GPE_XRUPT_TYPE_MASK)) {
e8406b44
TR
583 status = AE_BAD_PARAMETER;
584 goto exit;
1da177e4
LT
585 }
586
4be44fcd
LB
587 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
588 if (ACPI_FAILURE(status)) {
e8406b44 589 goto exit;
1da177e4
LT
590 }
591
592 /* Ensure that we have a valid GPE number */
593
4be44fcd 594 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
1da177e4
LT
595 if (!gpe_event_info) {
596 status = AE_BAD_PARAMETER;
f6dd9221 597 goto unlock_and_exit;
1da177e4
LT
598 }
599
600 /* Make sure that there isn't a handler there already */
601
4be44fcd
LB
602 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
603 ACPI_GPE_DISPATCH_HANDLER) {
1da177e4 604 status = AE_ALREADY_EXISTS;
f6dd9221 605 goto unlock_and_exit;
1da177e4
LT
606 }
607
608 /* Allocate and init handler object */
609
8313524a 610 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
1da177e4
LT
611 if (!handler) {
612 status = AE_NO_MEMORY;
f6dd9221 613 goto unlock_and_exit;
1da177e4
LT
614 }
615
4be44fcd
LB
616 handler->address = address;
617 handler->context = context;
1da177e4
LT
618 handler->method_node = gpe_event_info->dispatch.method_node;
619
620 /* Disable the GPE before installing the handler */
621
4be44fcd
LB
622 status = acpi_ev_disable_gpe(gpe_event_info);
623 if (ACPI_FAILURE(status)) {
f6dd9221 624 goto unlock_and_exit;
1da177e4
LT
625 }
626
627 /* Install the handler */
628
4be44fcd 629 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
1da177e4
LT
630 gpe_event_info->dispatch.handler = handler;
631
632 /* Setup up dispatch flags to indicate handler (vs. method) */
633
d4913dc6
BM
634 gpe_event_info->flags &=
635 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
1da177e4
LT
636 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
637
4be44fcd 638 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
1da177e4 639
f6dd9221 640 unlock_and_exit:
4be44fcd 641 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
f6dd9221 642 exit:
e8406b44
TR
643 if (ACPI_FAILURE(status))
644 ACPI_EXCEPTION((AE_INFO, status,
645 "Installing notify handler failed"));
4be44fcd 646 return_ACPI_STATUS(status);
1da177e4 647}
1da177e4 648
8313524a 649ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
1da177e4
LT
650
651/*******************************************************************************
652 *
653 * FUNCTION: acpi_remove_gpe_handler
654 *
44f6c012
RM
655 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
656 * defined GPEs)
657 * gpe_number - The event to remove a handler
1da177e4
LT
658 * Address - Address of the handler
659 *
660 * RETURN: Status
661 *
662 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
663 *
664 ******************************************************************************/
1da177e4 665acpi_status
4be44fcd
LB
666acpi_remove_gpe_handler(acpi_handle gpe_device,
667 u32 gpe_number, acpi_event_handler address)
1da177e4 668{
4be44fcd
LB
669 struct acpi_gpe_event_info *gpe_event_info;
670 struct acpi_handler_info *handler;
671 acpi_status status;
b8e4d893 672 acpi_cpu_flags flags;
1da177e4 673
b229cf92 674 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
1da177e4
LT
675
676 /* Parameter validation */
677
678 if (!address) {
4be44fcd 679 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
680 }
681
4be44fcd
LB
682 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
683 if (ACPI_FAILURE(status)) {
684 return_ACPI_STATUS(status);
1da177e4
LT
685 }
686
687 /* Ensure that we have a valid GPE number */
688
4be44fcd 689 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
1da177e4
LT
690 if (!gpe_event_info) {
691 status = AE_BAD_PARAMETER;
692 goto unlock_and_exit;
693 }
694
695 /* Make sure that a handler is indeed installed */
696
4be44fcd
LB
697 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
698 ACPI_GPE_DISPATCH_HANDLER) {
1da177e4
LT
699 status = AE_NOT_EXIST;
700 goto unlock_and_exit;
701 }
702
703 /* Make sure that the installed handler is the same */
704
705 if (gpe_event_info->dispatch.handler->address != address) {
706 status = AE_BAD_PARAMETER;
707 goto unlock_and_exit;
708 }
709
710 /* Disable the GPE before removing the handler */
711
4be44fcd
LB
712 status = acpi_ev_disable_gpe(gpe_event_info);
713 if (ACPI_FAILURE(status)) {
1da177e4
LT
714 goto unlock_and_exit;
715 }
716
717 /* Make sure all deferred tasks are completed */
718
4be44fcd 719 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
1da177e4 720 acpi_os_wait_events_complete(NULL);
4be44fcd
LB
721 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
722 if (ACPI_FAILURE(status)) {
723 return_ACPI_STATUS(status);
724 }
1da177e4
LT
725
726 /* Remove the handler */
727
4be44fcd 728 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
1da177e4
LT
729 handler = gpe_event_info->dispatch.handler;
730
731 /* Restore Method node (if any), set dispatch flags */
732
733 gpe_event_info->dispatch.method_node = handler->method_node;
4be44fcd 734 gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK; /* Clear bits */
1da177e4
LT
735 if (handler->method_node) {
736 gpe_event_info->flags |= ACPI_GPE_DISPATCH_METHOD;
737 }
4be44fcd 738 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
1da177e4
LT
739
740 /* Now we can free the handler object */
741
8313524a 742 ACPI_FREE(handler);
1da177e4 743
4be44fcd
LB
744 unlock_and_exit:
745 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
746 return_ACPI_STATUS(status);
1da177e4 747}
1da177e4 748
8313524a 749ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
1da177e4
LT
750
751/*******************************************************************************
752 *
753 * FUNCTION: acpi_acquire_global_lock
754 *
755 * PARAMETERS: Timeout - How long the caller is willing to wait
44f6c012
RM
756 * Handle - Where the handle to the lock is returned
757 * (if acquired)
1da177e4
LT
758 *
759 * RETURN: Status
760 *
761 * DESCRIPTION: Acquire the ACPI Global Lock
762 *
ba886cd4
BM
763 * Note: Allows callers with the same thread ID to acquire the global lock
764 * multiple times. In other words, externally, the behavior of the global lock
765 * is identical to an AML mutex. On the first acquire, a new handle is
766 * returned. On any subsequent calls to acquire by the same thread, the same
767 * handle is returned.
768 *
1da177e4 769 ******************************************************************************/
4be44fcd 770acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
1da177e4 771{
4be44fcd 772 acpi_status status;
1da177e4
LT
773
774 if (!handle) {
775 return (AE_BAD_PARAMETER);
776 }
777
4d2acd9e 778 /* Must lock interpreter to prevent race conditions */
1da177e4 779
4d2acd9e 780 acpi_ex_enter_interpreter();
ba886cd4
BM
781
782 status = acpi_ex_acquire_mutex_object(timeout,
783 acpi_gbl_global_lock_mutex,
784 acpi_os_get_thread_id());
1da177e4 785
4be44fcd 786 if (ACPI_SUCCESS(status)) {
ba886cd4 787
e5567afa 788 /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
ba886cd4 789
1da177e4
LT
790 *handle = acpi_gbl_global_lock_handle;
791 }
792
ba886cd4 793 acpi_ex_exit_interpreter();
1da177e4
LT
794 return (status);
795}
1da177e4 796
8313524a 797ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
1da177e4
LT
798
799/*******************************************************************************
800 *
801 * FUNCTION: acpi_release_global_lock
802 *
803 * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
804 *
805 * RETURN: Status
806 *
44f6c012 807 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
1da177e4
LT
808 *
809 ******************************************************************************/
4be44fcd 810acpi_status acpi_release_global_lock(u32 handle)
1da177e4 811{
4be44fcd 812 acpi_status status;
1da177e4 813
f02e9fa1 814 if (!handle || (handle != acpi_gbl_global_lock_handle)) {
1da177e4
LT
815 return (AE_NOT_ACQUIRED);
816 }
817
ba886cd4 818 status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
1da177e4
LT
819 return (status);
820}
1da177e4 821
8313524a 822ACPI_EXPORT_SYMBOL(acpi_release_global_lock)