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