Merge ../scsi-misc-2.6
[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 - 2006, 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 <acpi/acpi.h>
45 #include <acpi/acnamesp.h>
46 #include <acpi/acevents.h>
47 #include <acpi/acinterp.h>
48
49 #define _COMPONENT          ACPI_EVENTS
50 ACPI_MODULE_NAME("evxface")
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
65 acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
66 {
67         acpi_status status;
68
69         ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
70
71         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
72         if (ACPI_FAILURE(status)) {
73                 return_ACPI_STATUS(status);
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
87       cleanup:
88         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
89         return_ACPI_STATUS(status);
90 }
91
92 ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
93 #endif                          /*  ACPI_FUTURE_USAGE  */
94
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  ******************************************************************************/
110 acpi_status
111 acpi_install_fixed_event_handler(u32 event,
112                                  acpi_event_handler handler, void *context)
113 {
114         acpi_status status;
115
116         ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
117
118         /* Parameter validation */
119
120         if (event > ACPI_EVENT_MAX) {
121                 return_ACPI_STATUS(AE_BAD_PARAMETER);
122         }
123
124         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
125         if (ACPI_FAILURE(status)) {
126                 return_ACPI_STATUS(status);
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
141         status = acpi_clear_event(event);
142         if (ACPI_SUCCESS(status))
143                 status = acpi_enable_event(event, 0);
144         if (ACPI_FAILURE(status)) {
145                 ACPI_WARNING((AE_INFO, "Could not enable fixed event %X",
146                               event));
147
148                 /* Remove the handler */
149
150                 acpi_gbl_fixed_event_handlers[event].handler = NULL;
151                 acpi_gbl_fixed_event_handlers[event].context = NULL;
152         } else {
153                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
154                                   "Enabled fixed event %X, Handler=%p\n", event,
155                                   handler));
156         }
157
158       cleanup:
159         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
160         return_ACPI_STATUS(status);
161 }
162
163 ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
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  ******************************************************************************/
177 acpi_status
178 acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
179 {
180         acpi_status status = AE_OK;
181
182         ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
183
184         /* Parameter validation */
185
186         if (event > ACPI_EVENT_MAX) {
187                 return_ACPI_STATUS(AE_BAD_PARAMETER);
188         }
189
190         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
191         if (ACPI_FAILURE(status)) {
192                 return_ACPI_STATUS(status);
193         }
194
195         /* Disable the event before removing the handler */
196
197         status = acpi_disable_event(event, 0);
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
204         if (ACPI_FAILURE(status)) {
205                 ACPI_WARNING((AE_INFO,
206                               "Could not write to fixed event enable register %X",
207                               event));
208         } else {
209                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
210                                   event));
211         }
212
213         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
214         return_ACPI_STATUS(status);
215 }
216
217 ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
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  ******************************************************************************/
236 acpi_status
237 acpi_install_notify_handler(acpi_handle device,
238                             u32 handler_type,
239                             acpi_notify_handler handler, void *context)
240 {
241         union acpi_operand_object *obj_desc;
242         union acpi_operand_object *notify_obj;
243         struct acpi_namespace_node *node;
244         acpi_status status;
245
246         ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
247
248         /* Parameter validation */
249
250         if ((!device) ||
251             (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
252                 return_ACPI_STATUS(AE_BAD_PARAMETER);
253         }
254
255         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
256         if (ACPI_FAILURE(status)) {
257                 return_ACPI_STATUS(status);
258         }
259
260         /* Convert and validate the device handle */
261
262         node = acpi_ns_map_handle_to_node(device);
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
271          * caller wishes to receive notifications for all objects.  Note that
272          * only one <external> global handler can be regsitered (per notify type).
273          */
274         if (device == ACPI_ROOT_OBJECT) {
275
276                 /* Make sure the handler is not already installed */
277
278                 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
279                      acpi_gbl_system_notify.handler) ||
280                     ((handler_type & ACPI_DEVICE_NOTIFY) &&
281                      acpi_gbl_device_notify.handler)) {
282                         status = AE_ALREADY_EXISTS;
283                         goto unlock_and_exit;
284                 }
285
286                 if (handler_type & ACPI_SYSTEM_NOTIFY) {
287                         acpi_gbl_system_notify.node = node;
288                         acpi_gbl_system_notify.handler = handler;
289                         acpi_gbl_system_notify.context = context;
290                 }
291
292                 if (handler_type & ACPI_DEVICE_NOTIFY) {
293                         acpi_gbl_device_notify.node = node;
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
309                 if (!acpi_ev_is_notify_object(node)) {
310                         status = AE_TYPE;
311                         goto unlock_and_exit;
312                 }
313
314                 /* Check for an existing internal object */
315
316                 obj_desc = acpi_ns_get_attached_object(node);
317                 if (obj_desc) {
318
319                         /* Object exists - make sure there's no handler */
320
321                         if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
322                              obj_desc->common_notify.system_notify) ||
323                             ((handler_type & ACPI_DEVICE_NOTIFY) &&
324                              obj_desc->common_notify.device_notify)) {
325                                 status = AE_ALREADY_EXISTS;
326                                 goto unlock_and_exit;
327                         }
328                 } else {
329                         /* Create a new object */
330
331                         obj_desc = acpi_ut_create_internal_object(node->type);
332                         if (!obj_desc) {
333                                 status = AE_NO_MEMORY;
334                                 goto unlock_and_exit;
335                         }
336
337                         /* Attach new object to the Node */
338
339                         status =
340                             acpi_ns_attach_object(device, obj_desc, node->type);
341
342                         /* Remove local reference to the object */
343
344                         acpi_ut_remove_reference(obj_desc);
345                         if (ACPI_FAILURE(status)) {
346                                 goto unlock_and_exit;
347                         }
348                 }
349
350                 /* Install the handler */
351
352                 notify_obj =
353                     acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
354                 if (!notify_obj) {
355                         status = AE_NO_MEMORY;
356                         goto unlock_and_exit;
357                 }
358
359                 notify_obj->notify.node = node;
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) {
372
373                         /* Extra ref if installed in both */
374
375                         acpi_ut_add_reference(notify_obj);
376                 }
377         }
378
379       unlock_and_exit:
380         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
381         return_ACPI_STATUS(status);
382 }
383
384 ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
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  ******************************************************************************/
402 acpi_status
403 acpi_remove_notify_handler(acpi_handle device,
404                            u32 handler_type, acpi_notify_handler handler)
405 {
406         union acpi_operand_object *notify_obj;
407         union acpi_operand_object *obj_desc;
408         struct acpi_namespace_node *node;
409         acpi_status status;
410
411         ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
412
413         /* Parameter validation */
414
415         if ((!device) ||
416             (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
417                 status = AE_BAD_PARAMETER;
418                 goto exit;
419         }
420
421         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
422         if (ACPI_FAILURE(status)) {
423                 goto exit;
424         }
425
426         /* Convert and validate the device handle */
427
428         node = acpi_ns_map_handle_to_node(device);
429         if (!node) {
430                 status = AE_BAD_PARAMETER;
431                 goto unlock;
432         }
433
434         /* Root Object */
435
436         if (device == ACPI_ROOT_OBJECT) {
437                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
438                                   "Removing notify handler for namespace root object\n"));
439
440                 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
441                      !acpi_gbl_system_notify.handler) ||
442                     ((handler_type & ACPI_DEVICE_NOTIFY) &&
443                      !acpi_gbl_device_notify.handler)) {
444                         status = AE_NOT_EXIST;
445                         goto unlock;
446                 }
447
448                 /* Make sure all deferred tasks are completed */
449
450                 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
451                 acpi_os_wait_events_complete(NULL);
452                 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
453                 if (ACPI_FAILURE(status)) {
454                         goto exit;
455                 }
456
457                 if (handler_type & ACPI_SYSTEM_NOTIFY) {
458                         acpi_gbl_system_notify.node = NULL;
459                         acpi_gbl_system_notify.handler = NULL;
460                         acpi_gbl_system_notify.context = NULL;
461                 }
462
463                 if (handler_type & ACPI_DEVICE_NOTIFY) {
464                         acpi_gbl_device_notify.node = NULL;
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
475                 if (!acpi_ev_is_notify_object(node)) {
476                         status = AE_TYPE;
477                         goto unlock;
478                 }
479
480                 /* Check for an existing internal object */
481
482                 obj_desc = acpi_ns_get_attached_object(node);
483                 if (!obj_desc) {
484                         status = AE_NOT_EXIST;
485                         goto unlock;
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;
492                         if ((!notify_obj) ||
493                             (notify_obj->notify.handler != handler)) {
494                                 status = AE_BAD_PARAMETER;
495                                 goto unlock;
496                         }
497                         /* Make sure all deferred tasks are completed */
498
499                         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
500                         acpi_os_wait_events_complete(NULL);
501                         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
502                         if (ACPI_FAILURE(status)) {
503                                 goto exit;
504                         }
505
506                         /* Remove the handler */
507                         obj_desc->common_notify.system_notify = NULL;
508                         acpi_ut_remove_reference(notify_obj);
509                 }
510
511                 if (handler_type & ACPI_DEVICE_NOTIFY) {
512                         notify_obj = obj_desc->common_notify.device_notify;
513                         if ((!notify_obj) ||
514                             (notify_obj->notify.handler != handler)) {
515                                 status = AE_BAD_PARAMETER;
516                                 goto unlock;
517                         }
518                         /* Make sure all deferred tasks are completed */
519
520                         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
521                         acpi_os_wait_events_complete(NULL);
522                         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
523                         if (ACPI_FAILURE(status)) {
524                                 goto exit;
525                         }
526
527                         /* Remove the handler */
528                         obj_desc->common_notify.device_notify = NULL;
529                         acpi_ut_remove_reference(notify_obj);
530                 }
531         }
532
533 unlock:
534         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
535 exit:
536         if (ACPI_FAILURE(status))
537                 ACPI_EXCEPTION((AE_INFO, status, "Removing notify handler"));
538         return_ACPI_STATUS(status);
539 }
540
541 ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
542
543 /*******************************************************************************
544  *
545  * FUNCTION:    acpi_install_gpe_handler
546  *
547  * PARAMETERS:  gpe_device      - Namespace node for the GPE (NULL for FADT
548  *                                defined GPEs)
549  *              gpe_number      - The GPE number within the GPE block
550  *              Type            - Whether this GPE should be treated as an
551  *                                edge- or level-triggered interrupt.
552  *              Address         - Address of the handler
553  *              Context         - Value passed to the handler on each GPE
554  *
555  * RETURN:      Status
556  *
557  * DESCRIPTION: Install a handler for a General Purpose Event.
558  *
559  ******************************************************************************/
560 acpi_status
561 acpi_install_gpe_handler(acpi_handle gpe_device,
562                          u32 gpe_number,
563                          u32 type, acpi_event_handler address, void *context)
564 {
565         struct acpi_gpe_event_info *gpe_event_info;
566         struct acpi_handler_info *handler;
567         acpi_status status;
568         acpi_cpu_flags flags;
569
570         ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
571
572         /* Parameter validation */
573
574         if ((!address) || (type > ACPI_GPE_XRUPT_TYPE_MASK)) {
575                 status = AE_BAD_PARAMETER;
576                 goto exit;
577         }
578
579         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
580         if (ACPI_FAILURE(status)) {
581                 goto exit;
582         }
583
584         /* Ensure that we have a valid GPE number */
585
586         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
587         if (!gpe_event_info) {
588                 status = AE_BAD_PARAMETER;
589                 goto unlock;
590         }
591
592         /* Make sure that there isn't a handler there already */
593
594         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
595             ACPI_GPE_DISPATCH_HANDLER) {
596                 status = AE_ALREADY_EXISTS;
597                 goto unlock;
598         }
599
600         /* Allocate and init handler object */
601
602         handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
603         if (!handler) {
604                 status = AE_NO_MEMORY;
605                 goto unlock;
606         }
607
608         handler->address = address;
609         handler->context = context;
610         handler->method_node = gpe_event_info->dispatch.method_node;
611
612         /* Disable the GPE before installing the handler */
613
614         status = acpi_ev_disable_gpe(gpe_event_info);
615         if (ACPI_FAILURE(status)) {
616                 goto unlock;
617         }
618
619         /* Install the handler */
620
621         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
622         gpe_event_info->dispatch.handler = handler;
623
624         /* Setup up dispatch flags to indicate handler (vs. method) */
625
626         gpe_event_info->flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);  /* Clear bits */
627         gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
628
629         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
630
631 unlock:
632         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
633 exit:
634         if (ACPI_FAILURE(status))
635                 ACPI_EXCEPTION((AE_INFO, status,
636                                 "Installing notify handler failed"));
637         return_ACPI_STATUS(status);
638 }
639
640 ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
641
642 /*******************************************************************************
643  *
644  * FUNCTION:    acpi_remove_gpe_handler
645  *
646  * PARAMETERS:  gpe_device      - Namespace node for the GPE (NULL for FADT
647  *                                defined GPEs)
648  *              gpe_number      - The event to remove a handler
649  *              Address         - Address of the handler
650  *
651  * RETURN:      Status
652  *
653  * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
654  *
655  ******************************************************************************/
656 acpi_status
657 acpi_remove_gpe_handler(acpi_handle gpe_device,
658                         u32 gpe_number, acpi_event_handler address)
659 {
660         struct acpi_gpe_event_info *gpe_event_info;
661         struct acpi_handler_info *handler;
662         acpi_status status;
663         acpi_cpu_flags flags;
664
665         ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
666
667         /* Parameter validation */
668
669         if (!address) {
670                 return_ACPI_STATUS(AE_BAD_PARAMETER);
671         }
672
673         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
674         if (ACPI_FAILURE(status)) {
675                 return_ACPI_STATUS(status);
676         }
677
678         /* Ensure that we have a valid GPE number */
679
680         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
681         if (!gpe_event_info) {
682                 status = AE_BAD_PARAMETER;
683                 goto unlock_and_exit;
684         }
685
686         /* Make sure that a handler is indeed installed */
687
688         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
689             ACPI_GPE_DISPATCH_HANDLER) {
690                 status = AE_NOT_EXIST;
691                 goto unlock_and_exit;
692         }
693
694         /* Make sure that the installed handler is the same */
695
696         if (gpe_event_info->dispatch.handler->address != address) {
697                 status = AE_BAD_PARAMETER;
698                 goto unlock_and_exit;
699         }
700
701         /* Disable the GPE before removing the handler */
702
703         status = acpi_ev_disable_gpe(gpe_event_info);
704         if (ACPI_FAILURE(status)) {
705                 goto unlock_and_exit;
706         }
707
708         /* Make sure all deferred tasks are completed */
709
710         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
711         acpi_os_wait_events_complete(NULL);
712         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
713         if (ACPI_FAILURE(status)) {
714                 return_ACPI_STATUS(status);
715         }
716
717         /* Remove the handler */
718
719         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
720         handler = gpe_event_info->dispatch.handler;
721
722         /* Restore Method node (if any), set dispatch flags */
723
724         gpe_event_info->dispatch.method_node = handler->method_node;
725         gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK;       /* Clear bits */
726         if (handler->method_node) {
727                 gpe_event_info->flags |= ACPI_GPE_DISPATCH_METHOD;
728         }
729         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
730
731         /* Now we can free the handler object */
732
733         ACPI_FREE(handler);
734
735       unlock_and_exit:
736         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
737         return_ACPI_STATUS(status);
738 }
739
740 ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
741
742 /*******************************************************************************
743  *
744  * FUNCTION:    acpi_acquire_global_lock
745  *
746  * PARAMETERS:  Timeout         - How long the caller is willing to wait
747  *              Handle          - Where the handle to the lock is returned
748  *                                (if acquired)
749  *
750  * RETURN:      Status
751  *
752  * DESCRIPTION: Acquire the ACPI Global Lock
753  *
754  ******************************************************************************/
755 acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
756 {
757         acpi_status status;
758
759         if (!handle) {
760                 return (AE_BAD_PARAMETER);
761         }
762
763         status = acpi_ex_enter_interpreter();
764         if (ACPI_FAILURE(status)) {
765                 return (status);
766         }
767
768         status = acpi_ev_acquire_global_lock(timeout);
769         acpi_ex_exit_interpreter();
770
771         if (ACPI_SUCCESS(status)) {
772                 acpi_gbl_global_lock_handle++;
773                 *handle = acpi_gbl_global_lock_handle;
774         }
775
776         return (status);
777 }
778
779 ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
780
781 /*******************************************************************************
782  *
783  * FUNCTION:    acpi_release_global_lock
784  *
785  * PARAMETERS:  Handle      - Returned from acpi_acquire_global_lock
786  *
787  * RETURN:      Status
788  *
789  * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
790  *
791  ******************************************************************************/
792 acpi_status acpi_release_global_lock(u32 handle)
793 {
794         acpi_status status;
795
796         if (handle != acpi_gbl_global_lock_handle) {
797                 return (AE_NOT_ACQUIRED);
798         }
799
800         status = acpi_ev_release_global_lock();
801         return (status);
802 }
803
804 ACPI_EXPORT_SYMBOL(acpi_release_global_lock)