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