]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/acpi/acpica/evxface.c
Merge branch 'cleanup' into release
[net-next-2.6.git] / drivers / acpi / acpica / evxface.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: evxface - External interfaces for ACPI events
4 *
5 *****************************************************************************/
6
7/*
a8357b0c 8 * Copyright (C) 2000 - 2010, 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)) {
f6a22b0b 145 ACPI_WARNING((AE_INFO, "Could not enable fixed event 0x%X",
b8e4d893 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 205 ACPI_WARNING((AE_INFO,
f6a22b0b 206 "Could not write to fixed event enable register 0x%X",
b8e4d893 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 218
3f0be671
RW
219/*******************************************************************************
220 *
221 * FUNCTION: acpi_populate_handler_object
222 *
223 * PARAMETERS: handler_obj - Handler object to populate
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 * next - Address of a handler object to link to
231 *
232 * RETURN: None
233 *
234 * DESCRIPTION: Populate a handler object.
235 *
236 ******************************************************************************/
237static void
238acpi_populate_handler_object(struct acpi_object_notify_handler *handler_obj,
239 u32 handler_type,
240 acpi_notify_handler handler, void *context,
241 struct acpi_object_notify_handler *next)
242{
243 handler_obj->handler_type = handler_type;
244 handler_obj->handler = handler;
245 handler_obj->context = context;
246 handler_obj->next = next;
247}
248
249/*******************************************************************************
250 *
251 * FUNCTION: acpi_add_handler_object
252 *
253 * PARAMETERS: parent_obj - Parent of the new object
254 * handler - Address of the handler
255 * context - Value passed to the handler on each GPE
256 *
257 * RETURN: Status
258 *
259 * DESCRIPTION: Create a new handler object and populate it.
260 *
261 ******************************************************************************/
262static acpi_status
263acpi_add_handler_object(struct acpi_object_notify_handler *parent_obj,
264 acpi_notify_handler handler, void *context)
265{
266 struct acpi_object_notify_handler *handler_obj;
267
268 /* The parent must not be a defice notify handler object. */
269 if (parent_obj->handler_type & ACPI_DEVICE_NOTIFY)
270 return AE_BAD_PARAMETER;
271
272 handler_obj = ACPI_ALLOCATE_ZEROED(sizeof(*handler_obj));
273 if (!handler_obj)
274 return AE_NO_MEMORY;
275
276 acpi_populate_handler_object(handler_obj,
277 ACPI_SYSTEM_NOTIFY,
278 handler, context,
279 parent_obj->next);
280 parent_obj->next = handler_obj;
281
282 return AE_OK;
283}
284
1da177e4
LT
285/*******************************************************************************
286 *
287 * FUNCTION: acpi_install_notify_handler
288 *
289 * PARAMETERS: Device - The device for which notifies will be handled
290 * handler_type - The type of handler:
291 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
292 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
293 * ACPI_ALL_NOTIFY: both system and device
294 * Handler - Address of the handler
295 * Context - Value passed to the handler on each GPE
296 *
297 * RETURN: Status
298 *
299 * DESCRIPTION: Install a handler for notifies on an ACPI device
300 *
301 ******************************************************************************/
1da177e4 302acpi_status
4be44fcd
LB
303acpi_install_notify_handler(acpi_handle device,
304 u32 handler_type,
305 acpi_notify_handler handler, void *context)
1da177e4 306{
4be44fcd
LB
307 union acpi_operand_object *obj_desc;
308 union acpi_operand_object *notify_obj;
309 struct acpi_namespace_node *node;
310 acpi_status status;
1da177e4 311
b229cf92 312 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
1da177e4
LT
313
314 /* Parameter validation */
315
4be44fcd
LB
316 if ((!device) ||
317 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
318 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
319 }
320
4be44fcd
LB
321 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
322 if (ACPI_FAILURE(status)) {
323 return_ACPI_STATUS(status);
1da177e4
LT
324 }
325
326 /* Convert and validate the device handle */
327
f24b664d 328 node = acpi_ns_validate_handle(device);
1da177e4
LT
329 if (!node) {
330 status = AE_BAD_PARAMETER;
331 goto unlock_and_exit;
332 }
333
334 /*
335 * Root Object:
336 * Registering a notify handler on the root object indicates that the
9f15fc66 337 * caller wishes to receive notifications for all objects. Note that
1da177e4
LT
338 * only one <external> global handler can be regsitered (per notify type).
339 */
340 if (device == ACPI_ROOT_OBJECT) {
52fc0b02 341
1da177e4
LT
342 /* Make sure the handler is not already installed */
343
344 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
4be44fcd
LB
345 acpi_gbl_system_notify.handler) ||
346 ((handler_type & ACPI_DEVICE_NOTIFY) &&
347 acpi_gbl_device_notify.handler)) {
1da177e4
LT
348 status = AE_ALREADY_EXISTS;
349 goto unlock_and_exit;
350 }
351
352 if (handler_type & ACPI_SYSTEM_NOTIFY) {
4be44fcd 353 acpi_gbl_system_notify.node = node;
1da177e4
LT
354 acpi_gbl_system_notify.handler = handler;
355 acpi_gbl_system_notify.context = context;
356 }
357
358 if (handler_type & ACPI_DEVICE_NOTIFY) {
4be44fcd 359 acpi_gbl_device_notify.node = node;
1da177e4
LT
360 acpi_gbl_device_notify.handler = handler;
361 acpi_gbl_device_notify.context = context;
362 }
363
364 /* Global notify handler installed */
365 }
366
367 /*
368 * All Other Objects:
369 * Caller will only receive notifications specific to the target object.
370 * Note that only certain object types can receive notifications.
371 */
372 else {
373 /* Notifies allowed on this object? */
374
4be44fcd 375 if (!acpi_ev_is_notify_object(node)) {
1da177e4
LT
376 status = AE_TYPE;
377 goto unlock_and_exit;
378 }
379
380 /* Check for an existing internal object */
381
4be44fcd 382 obj_desc = acpi_ns_get_attached_object(node);
1da177e4 383 if (obj_desc) {
52fc0b02 384
3f0be671 385 /* Object exists. */
1da177e4 386
3f0be671
RW
387 /* For a device notify, make sure there's no handler. */
388 if ((handler_type & ACPI_DEVICE_NOTIFY) &&
389 obj_desc->common_notify.device_notify) {
1da177e4
LT
390 status = AE_ALREADY_EXISTS;
391 goto unlock_and_exit;
392 }
3f0be671
RW
393
394 /* System notifies may have more handlers installed. */
395 notify_obj = obj_desc->common_notify.system_notify;
396
397 if ((handler_type & ACPI_SYSTEM_NOTIFY) && notify_obj) {
398 struct acpi_object_notify_handler *parent_obj;
399
400 if (handler_type & ACPI_DEVICE_NOTIFY) {
401 status = AE_ALREADY_EXISTS;
402 goto unlock_and_exit;
403 }
404
405 parent_obj = &notify_obj->notify;
406 status = acpi_add_handler_object(parent_obj,
407 handler,
408 context);
409 goto unlock_and_exit;
410 }
4be44fcd 411 } else {
1da177e4
LT
412 /* Create a new object */
413
4be44fcd 414 obj_desc = acpi_ut_create_internal_object(node->type);
1da177e4
LT
415 if (!obj_desc) {
416 status = AE_NO_MEMORY;
417 goto unlock_and_exit;
418 }
419
420 /* Attach new object to the Node */
421
4be44fcd
LB
422 status =
423 acpi_ns_attach_object(device, obj_desc, node->type);
1da177e4
LT
424
425 /* Remove local reference to the object */
426
4be44fcd
LB
427 acpi_ut_remove_reference(obj_desc);
428 if (ACPI_FAILURE(status)) {
1da177e4
LT
429 goto unlock_and_exit;
430 }
431 }
432
433 /* Install the handler */
434
4be44fcd
LB
435 notify_obj =
436 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
1da177e4
LT
437 if (!notify_obj) {
438 status = AE_NO_MEMORY;
439 goto unlock_and_exit;
440 }
441
3f0be671
RW
442 acpi_populate_handler_object(&notify_obj->notify,
443 handler_type,
444 handler, context,
445 NULL);
1da177e4
LT
446
447 if (handler_type & ACPI_SYSTEM_NOTIFY) {
448 obj_desc->common_notify.system_notify = notify_obj;
449 }
450
451 if (handler_type & ACPI_DEVICE_NOTIFY) {
452 obj_desc->common_notify.device_notify = notify_obj;
453 }
454
455 if (handler_type == ACPI_ALL_NOTIFY) {
52fc0b02 456
1da177e4
LT
457 /* Extra ref if installed in both */
458
4be44fcd 459 acpi_ut_add_reference(notify_obj);
1da177e4
LT
460 }
461 }
462
4be44fcd
LB
463 unlock_and_exit:
464 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
465 return_ACPI_STATUS(status);
1da177e4 466}
1da177e4 467
8313524a 468ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
1da177e4
LT
469
470/*******************************************************************************
471 *
472 * FUNCTION: acpi_remove_notify_handler
473 *
474 * PARAMETERS: Device - The device for which notifies will be handled
475 * handler_type - The type of handler:
476 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
477 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
478 * ACPI_ALL_NOTIFY: both system and device
479 * Handler - Address of the handler
480 *
481 * RETURN: Status
482 *
483 * DESCRIPTION: Remove a handler for notifies on an ACPI device
484 *
485 ******************************************************************************/
1da177e4 486acpi_status
4be44fcd
LB
487acpi_remove_notify_handler(acpi_handle device,
488 u32 handler_type, acpi_notify_handler handler)
1da177e4 489{
4be44fcd
LB
490 union acpi_operand_object *notify_obj;
491 union acpi_operand_object *obj_desc;
492 struct acpi_namespace_node *node;
493 acpi_status status;
1da177e4 494
b229cf92 495 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
1da177e4
LT
496
497 /* Parameter validation */
498
4be44fcd
LB
499 if ((!device) ||
500 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
e8406b44
TR
501 status = AE_BAD_PARAMETER;
502 goto exit;
1da177e4
LT
503 }
504
3f0be671
RW
505
506 /* Make sure all deferred tasks are completed */
507 acpi_os_wait_events_complete(NULL);
508
4be44fcd
LB
509 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
510 if (ACPI_FAILURE(status)) {
e8406b44 511 goto exit;
1da177e4
LT
512 }
513
514 /* Convert and validate the device handle */
515
f24b664d 516 node = acpi_ns_validate_handle(device);
1da177e4
LT
517 if (!node) {
518 status = AE_BAD_PARAMETER;
f6dd9221 519 goto unlock_and_exit;
1da177e4
LT
520 }
521
522 /* Root Object */
523
524 if (device == ACPI_ROOT_OBJECT) {
4be44fcd 525 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4a90c7e8 526 "Removing notify handler for namespace root object\n"));
1da177e4
LT
527
528 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
4be44fcd
LB
529 !acpi_gbl_system_notify.handler) ||
530 ((handler_type & ACPI_DEVICE_NOTIFY) &&
531 !acpi_gbl_device_notify.handler)) {
1da177e4 532 status = AE_NOT_EXIST;
f6dd9221 533 goto unlock_and_exit;
1da177e4
LT
534 }
535
1da177e4 536 if (handler_type & ACPI_SYSTEM_NOTIFY) {
4be44fcd 537 acpi_gbl_system_notify.node = NULL;
1da177e4
LT
538 acpi_gbl_system_notify.handler = NULL;
539 acpi_gbl_system_notify.context = NULL;
540 }
541
542 if (handler_type & ACPI_DEVICE_NOTIFY) {
4be44fcd 543 acpi_gbl_device_notify.node = NULL;
1da177e4
LT
544 acpi_gbl_device_notify.handler = NULL;
545 acpi_gbl_device_notify.context = NULL;
546 }
547 }
548
549 /* All Other Objects */
550
551 else {
552 /* Notifies allowed on this object? */
553
4be44fcd 554 if (!acpi_ev_is_notify_object(node)) {
1da177e4 555 status = AE_TYPE;
f6dd9221 556 goto unlock_and_exit;
1da177e4
LT
557 }
558
559 /* Check for an existing internal object */
560
4be44fcd 561 obj_desc = acpi_ns_get_attached_object(node);
1da177e4
LT
562 if (!obj_desc) {
563 status = AE_NOT_EXIST;
f6dd9221 564 goto unlock_and_exit;
1da177e4
LT
565 }
566
567 /* Object exists - make sure there's an existing handler */
568
569 if (handler_type & ACPI_SYSTEM_NOTIFY) {
3f0be671
RW
570 struct acpi_object_notify_handler *handler_obj;
571 struct acpi_object_notify_handler *parent_obj;
572
1da177e4 573 notify_obj = obj_desc->common_notify.system_notify;
f6dd9221
BM
574 if (!notify_obj) {
575 status = AE_NOT_EXIST;
576 goto unlock_and_exit;
577 }
578
3f0be671
RW
579 handler_obj = &notify_obj->notify;
580 parent_obj = NULL;
581 while (handler_obj->handler != handler) {
582 if (handler_obj->next) {
583 parent_obj = handler_obj;
584 handler_obj = handler_obj->next;
585 } else {
586 break;
587 }
588 }
589
590 if (handler_obj->handler != handler) {
1da177e4 591 status = AE_BAD_PARAMETER;
f6dd9221 592 goto unlock_and_exit;
1da177e4 593 }
1da177e4 594
3f0be671
RW
595 /*
596 * Remove the handler. There are three possible cases.
597 * First, we may need to remove a non-embedded object.
598 * Second, we may need to remove the embedded object's
599 * handler data, while non-embedded objects exist.
600 * Finally, we may need to remove the embedded object
601 * entirely along with its container.
602 */
603 if (parent_obj) {
604 /* Non-embedded object is being removed. */
605 parent_obj->next = handler_obj->next;
606 ACPI_FREE(handler_obj);
607 } else if (notify_obj->notify.next) {
608 /*
609 * The handler matches the embedded object, but
610 * there are more handler objects in the list.
611 * Replace the embedded object's data with the
612 * first next object's data and remove that
613 * object.
614 */
615 parent_obj = &notify_obj->notify;
616 handler_obj = notify_obj->notify.next;
617 *parent_obj = *handler_obj;
618 ACPI_FREE(handler_obj);
619 } else {
620 /* No more handler objects in the list. */
621 obj_desc->common_notify.system_notify = NULL;
622 acpi_ut_remove_reference(notify_obj);
4be44fcd 623 }
1da177e4
LT
624 }
625
626 if (handler_type & ACPI_DEVICE_NOTIFY) {
627 notify_obj = obj_desc->common_notify.device_notify;
f6dd9221
BM
628 if (!notify_obj) {
629 status = AE_NOT_EXIST;
630 goto unlock_and_exit;
631 }
632
633 if (notify_obj->notify.handler != handler) {
1da177e4 634 status = AE_BAD_PARAMETER;
f6dd9221 635 goto unlock_and_exit;
1da177e4 636 }
1da177e4
LT
637
638 /* Remove the handler */
639 obj_desc->common_notify.device_notify = NULL;
4be44fcd 640 acpi_ut_remove_reference(notify_obj);
1da177e4
LT
641 }
642 }
643
f6dd9221 644 unlock_and_exit:
4be44fcd 645 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
f6dd9221 646 exit:
e8406b44
TR
647 if (ACPI_FAILURE(status))
648 ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
4be44fcd 649 return_ACPI_STATUS(status);
1da177e4 650}
1da177e4 651
8313524a 652ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
1da177e4
LT
653
654/*******************************************************************************
655 *
656 * FUNCTION: acpi_install_gpe_handler
657 *
44f6c012
RM
658 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
659 * defined GPEs)
660 * gpe_number - The GPE number within the GPE block
1da177e4
LT
661 * Type - Whether this GPE should be treated as an
662 * edge- or level-triggered interrupt.
663 * Address - Address of the handler
664 * Context - Value passed to the handler on each GPE
665 *
666 * RETURN: Status
667 *
668 * DESCRIPTION: Install a handler for a General Purpose Event.
669 *
670 ******************************************************************************/
1da177e4 671acpi_status
4be44fcd
LB
672acpi_install_gpe_handler(acpi_handle gpe_device,
673 u32 gpe_number,
674 u32 type, acpi_event_handler address, void *context)
1da177e4 675{
4be44fcd
LB
676 struct acpi_gpe_event_info *gpe_event_info;
677 struct acpi_handler_info *handler;
678 acpi_status status;
b8e4d893 679 acpi_cpu_flags flags;
1da177e4 680
b229cf92 681 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
1da177e4
LT
682
683 /* Parameter validation */
684
0f849d2c
LM
685 if ((!address) || (type & ~ACPI_GPE_XRUPT_TYPE_MASK)) {
686 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
687 }
688
4be44fcd
LB
689 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
690 if (ACPI_FAILURE(status)) {
0f849d2c 691 return_ACPI_STATUS(status);
1da177e4
LT
692 }
693
28f4f8a9
RW
694 /* Allocate memory for the handler object */
695
696 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
697 if (!handler) {
698 status = AE_NO_MEMORY;
699 goto unlock_and_exit;
700 }
701
702 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
703
1da177e4
LT
704 /* Ensure that we have a valid GPE number */
705
4be44fcd 706 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
1da177e4
LT
707 if (!gpe_event_info) {
708 status = AE_BAD_PARAMETER;
28f4f8a9 709 goto free_and_exit;
1da177e4
LT
710 }
711
712 /* Make sure that there isn't a handler there already */
713
4be44fcd
LB
714 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
715 ACPI_GPE_DISPATCH_HANDLER) {
1da177e4 716 status = AE_ALREADY_EXISTS;
28f4f8a9 717 goto free_and_exit;
1da177e4
LT
718 }
719
720 /* Allocate and init handler object */
721
4be44fcd
LB
722 handler->address = address;
723 handler->context = context;
1da177e4 724 handler->method_node = gpe_event_info->dispatch.method_node;
28f4f8a9
RW
725 handler->orig_flags = gpe_event_info->flags &
726 (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
727
728 /*
a2100801
RW
729 * If the GPE is associated with a method, it might have been enabled
730 * automatically during initialization, in which case it has to be
731 * disabled now to avoid spurious execution of the handler.
28f4f8a9
RW
732 */
733
734 if ((handler->orig_flags & ACPI_GPE_DISPATCH_METHOD)
a2100801
RW
735 && gpe_event_info->runtime_count) {
736 handler->orig_enabled = 1;
28f4f8a9 737 (void)acpi_raw_disable_gpe(gpe_event_info);
a2100801 738 }
1da177e4 739
1da177e4
LT
740 /* Install the handler */
741
1da177e4
LT
742 gpe_event_info->dispatch.handler = handler;
743
744 /* Setup up dispatch flags to indicate handler (vs. method) */
745
d4913dc6
BM
746 gpe_event_info->flags &=
747 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
1da177e4
LT
748 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
749
4be44fcd 750 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
1da177e4 751
0f849d2c 752unlock_and_exit:
4be44fcd
LB
753 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
754 return_ACPI_STATUS(status);
28f4f8a9
RW
755
756free_and_exit:
757 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
758 ACPI_FREE(handler);
759 goto unlock_and_exit;
1da177e4 760}
1da177e4 761
8313524a 762ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
1da177e4
LT
763
764/*******************************************************************************
765 *
766 * FUNCTION: acpi_remove_gpe_handler
767 *
44f6c012
RM
768 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
769 * defined GPEs)
770 * gpe_number - The event to remove a handler
1da177e4
LT
771 * Address - Address of the handler
772 *
773 * RETURN: Status
774 *
775 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
776 *
777 ******************************************************************************/
1da177e4 778acpi_status
4be44fcd
LB
779acpi_remove_gpe_handler(acpi_handle gpe_device,
780 u32 gpe_number, acpi_event_handler address)
1da177e4 781{
4be44fcd
LB
782 struct acpi_gpe_event_info *gpe_event_info;
783 struct acpi_handler_info *handler;
784 acpi_status status;
b8e4d893 785 acpi_cpu_flags flags;
1da177e4 786
b229cf92 787 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
1da177e4
LT
788
789 /* Parameter validation */
790
791 if (!address) {
4be44fcd 792 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
793 }
794
28f4f8a9
RW
795 /* Make sure all deferred tasks are completed */
796
797 acpi_os_wait_events_complete(NULL);
798
4be44fcd
LB
799 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
800 if (ACPI_FAILURE(status)) {
801 return_ACPI_STATUS(status);
1da177e4
LT
802 }
803
28f4f8a9
RW
804 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
805
1da177e4
LT
806 /* Ensure that we have a valid GPE number */
807
4be44fcd 808 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
1da177e4
LT
809 if (!gpe_event_info) {
810 status = AE_BAD_PARAMETER;
811 goto unlock_and_exit;
812 }
813
814 /* Make sure that a handler is indeed installed */
815
4be44fcd
LB
816 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
817 ACPI_GPE_DISPATCH_HANDLER) {
1da177e4
LT
818 status = AE_NOT_EXIST;
819 goto unlock_and_exit;
820 }
821
822 /* Make sure that the installed handler is the same */
823
824 if (gpe_event_info->dispatch.handler->address != address) {
825 status = AE_BAD_PARAMETER;
826 goto unlock_and_exit;
827 }
828
1da177e4
LT
829 /* Remove the handler */
830
1da177e4
LT
831 handler = gpe_event_info->dispatch.handler;
832
833 /* Restore Method node (if any), set dispatch flags */
834
835 gpe_event_info->dispatch.method_node = handler->method_node;
28f4f8a9
RW
836 gpe_event_info->flags &=
837 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
838 gpe_event_info->flags |= handler->orig_flags;
839
840 /*
a2100801
RW
841 * If the GPE was previously associated with a method and it was
842 * enabled, it should be enabled at this point to restore the
843 * post-initialization configuration.
28f4f8a9
RW
844 */
845
846 if ((handler->orig_flags & ACPI_GPE_DISPATCH_METHOD)
a2100801 847 && handler->orig_enabled)
28f4f8a9 848 (void)acpi_raw_enable_gpe(gpe_event_info);
1da177e4
LT
849
850 /* Now we can free the handler object */
851
8313524a 852 ACPI_FREE(handler);
1da177e4 853
28f4f8a9
RW
854unlock_and_exit:
855 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
856
4be44fcd
LB
857 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
858 return_ACPI_STATUS(status);
1da177e4 859}
1da177e4 860
8313524a 861ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
1da177e4
LT
862
863/*******************************************************************************
864 *
865 * FUNCTION: acpi_acquire_global_lock
866 *
867 * PARAMETERS: Timeout - How long the caller is willing to wait
44f6c012
RM
868 * Handle - Where the handle to the lock is returned
869 * (if acquired)
1da177e4
LT
870 *
871 * RETURN: Status
872 *
873 * DESCRIPTION: Acquire the ACPI Global Lock
874 *
ba886cd4
BM
875 * Note: Allows callers with the same thread ID to acquire the global lock
876 * multiple times. In other words, externally, the behavior of the global lock
877 * is identical to an AML mutex. On the first acquire, a new handle is
878 * returned. On any subsequent calls to acquire by the same thread, the same
879 * handle is returned.
880 *
1da177e4 881 ******************************************************************************/
4be44fcd 882acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
1da177e4 883{
4be44fcd 884 acpi_status status;
1da177e4
LT
885
886 if (!handle) {
887 return (AE_BAD_PARAMETER);
888 }
889
4d2acd9e 890 /* Must lock interpreter to prevent race conditions */
1da177e4 891
4d2acd9e 892 acpi_ex_enter_interpreter();
ba886cd4
BM
893
894 status = acpi_ex_acquire_mutex_object(timeout,
895 acpi_gbl_global_lock_mutex,
896 acpi_os_get_thread_id());
1da177e4 897
4be44fcd 898 if (ACPI_SUCCESS(status)) {
ba886cd4 899
e5567afa 900 /* Return the global lock handle (updated in acpi_ev_acquire_global_lock) */
ba886cd4 901
1da177e4
LT
902 *handle = acpi_gbl_global_lock_handle;
903 }
904
ba886cd4 905 acpi_ex_exit_interpreter();
1da177e4
LT
906 return (status);
907}
1da177e4 908
8313524a 909ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
1da177e4
LT
910
911/*******************************************************************************
912 *
913 * FUNCTION: acpi_release_global_lock
914 *
915 * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
916 *
917 * RETURN: Status
918 *
44f6c012 919 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
1da177e4
LT
920 *
921 ******************************************************************************/
4be44fcd 922acpi_status acpi_release_global_lock(u32 handle)
1da177e4 923{
4be44fcd 924 acpi_status status;
1da177e4 925
f02e9fa1 926 if (!handle || (handle != acpi_gbl_global_lock_handle)) {
1da177e4
LT
927 return (AE_NOT_ACQUIRED);
928 }
929
ba886cd4 930 status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
1da177e4
LT
931 return (status);
932}
1da177e4 933
8313524a 934ACPI_EXPORT_SYMBOL(acpi_release_global_lock)