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