Merge branch 'omap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tmlind...
[pandora-kernel.git] / drivers / acpi / acpica / evxfevnt.c
1 /******************************************************************************
2  *
3  * Module Name: evxfevnt - External Interfaces, ACPI event disable/enable
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2010, Intel Corp.
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 "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.h"
48 #include "actables.h"
49
50 #define _COMPONENT          ACPI_EVENTS
51 ACPI_MODULE_NAME("evxfevnt")
52
53 /* Local prototypes */
54 static acpi_status
55 acpi_ev_get_gpe_device(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
56                        struct acpi_gpe_block_info *gpe_block, void *context);
57
58 /*******************************************************************************
59  *
60  * FUNCTION:    acpi_enable
61  *
62  * PARAMETERS:  None
63  *
64  * RETURN:      Status
65  *
66  * DESCRIPTION: Transfers the system into ACPI mode.
67  *
68  ******************************************************************************/
69
70 acpi_status acpi_enable(void)
71 {
72         acpi_status status = AE_OK;
73
74         ACPI_FUNCTION_TRACE(acpi_enable);
75
76         /* ACPI tables must be present */
77
78         if (!acpi_tb_tables_loaded()) {
79                 return_ACPI_STATUS(AE_NO_ACPI_TABLES);
80         }
81
82         /* Check current mode */
83
84         if (acpi_hw_get_mode() == ACPI_SYS_MODE_ACPI) {
85                 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
86                                   "System is already in ACPI mode\n"));
87         } else {
88                 /* Transition to ACPI mode */
89
90                 status = acpi_hw_set_mode(ACPI_SYS_MODE_ACPI);
91                 if (ACPI_FAILURE(status)) {
92                         ACPI_ERROR((AE_INFO,
93                                     "Could not transition to ACPI mode"));
94                         return_ACPI_STATUS(status);
95                 }
96
97                 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
98                                   "Transition to ACPI mode successful\n"));
99         }
100
101         return_ACPI_STATUS(status);
102 }
103
104 ACPI_EXPORT_SYMBOL(acpi_enable)
105
106 /*******************************************************************************
107  *
108  * FUNCTION:    acpi_disable
109  *
110  * PARAMETERS:  None
111  *
112  * RETURN:      Status
113  *
114  * DESCRIPTION: Transfers the system into LEGACY (non-ACPI) mode.
115  *
116  ******************************************************************************/
117 acpi_status acpi_disable(void)
118 {
119         acpi_status status = AE_OK;
120
121         ACPI_FUNCTION_TRACE(acpi_disable);
122
123         if (acpi_hw_get_mode() == ACPI_SYS_MODE_LEGACY) {
124                 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
125                                   "System is already in legacy (non-ACPI) mode\n"));
126         } else {
127                 /* Transition to LEGACY mode */
128
129                 status = acpi_hw_set_mode(ACPI_SYS_MODE_LEGACY);
130
131                 if (ACPI_FAILURE(status)) {
132                         ACPI_ERROR((AE_INFO,
133                                     "Could not exit ACPI mode to legacy mode"));
134                         return_ACPI_STATUS(status);
135                 }
136
137                 ACPI_DEBUG_PRINT((ACPI_DB_INIT, "ACPI mode disabled\n"));
138         }
139
140         return_ACPI_STATUS(status);
141 }
142
143 ACPI_EXPORT_SYMBOL(acpi_disable)
144
145 /*******************************************************************************
146  *
147  * FUNCTION:    acpi_enable_event
148  *
149  * PARAMETERS:  Event           - The fixed eventto be enabled
150  *              Flags           - Reserved
151  *
152  * RETURN:      Status
153  *
154  * DESCRIPTION: Enable an ACPI event (fixed)
155  *
156  ******************************************************************************/
157 acpi_status acpi_enable_event(u32 event, u32 flags)
158 {
159         acpi_status status = AE_OK;
160         u32 value;
161
162         ACPI_FUNCTION_TRACE(acpi_enable_event);
163
164         /* Decode the Fixed Event */
165
166         if (event > ACPI_EVENT_MAX) {
167                 return_ACPI_STATUS(AE_BAD_PARAMETER);
168         }
169
170         /*
171          * Enable the requested fixed event (by writing a one to the enable
172          * register bit)
173          */
174         status =
175             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
176                                     enable_register_id, ACPI_ENABLE_EVENT);
177         if (ACPI_FAILURE(status)) {
178                 return_ACPI_STATUS(status);
179         }
180
181         /* Make sure that the hardware responded */
182
183         status =
184             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
185                                    enable_register_id, &value);
186         if (ACPI_FAILURE(status)) {
187                 return_ACPI_STATUS(status);
188         }
189
190         if (value != 1) {
191                 ACPI_ERROR((AE_INFO,
192                             "Could not enable %s event",
193                             acpi_ut_get_event_name(event)));
194                 return_ACPI_STATUS(AE_NO_HARDWARE_RESPONSE);
195         }
196
197         return_ACPI_STATUS(status);
198 }
199
200 ACPI_EXPORT_SYMBOL(acpi_enable_event)
201
202 /*******************************************************************************
203  *
204  * FUNCTION:    acpi_set_gpe
205  *
206  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
207  *              gpe_number      - GPE level within the GPE block
208  *              action          - ACPI_GPE_ENABLE or ACPI_GPE_DISABLE
209  *
210  * RETURN:      Status
211  *
212  * DESCRIPTION: Enable or disable an individual GPE. This function bypasses
213  *              the reference count mechanism used in the acpi_enable_gpe and
214  *              acpi_disable_gpe interfaces -- and should be used with care.
215  *
216  * Note: Typically used to disable a runtime GPE for short period of time,
217  * then re-enable it, without disturbing the existing reference counts. This
218  * is useful, for example, in the Embedded Controller (EC) driver.
219  *
220  ******************************************************************************/
221 acpi_status acpi_set_gpe(acpi_handle gpe_device, u32 gpe_number, u8 action)
222 {
223         struct acpi_gpe_event_info *gpe_event_info;
224         acpi_status status;
225         acpi_cpu_flags flags;
226
227         ACPI_FUNCTION_TRACE(acpi_set_gpe);
228
229         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
230
231         /* Ensure that we have a valid GPE number */
232
233         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
234         if (!gpe_event_info) {
235                 status = AE_BAD_PARAMETER;
236                 goto unlock_and_exit;
237         }
238
239         /* Perform the action */
240
241         switch (action) {
242         case ACPI_GPE_ENABLE:
243                 status = acpi_ev_enable_gpe(gpe_event_info);
244                 break;
245
246         case ACPI_GPE_DISABLE:
247                 status = acpi_ev_disable_gpe(gpe_event_info);
248                 break;
249
250         default:
251                 status = AE_BAD_PARAMETER;
252                 break;
253         }
254
255       unlock_and_exit:
256         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
257         return_ACPI_STATUS(status);
258 }
259
260 ACPI_EXPORT_SYMBOL(acpi_set_gpe)
261
262 /*******************************************************************************
263  *
264  * FUNCTION:    acpi_enable_gpe
265  *
266  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
267  *              gpe_number      - GPE level within the GPE block
268  *              gpe_type        - ACPI_GPE_TYPE_RUNTIME or ACPI_GPE_TYPE_WAKE
269  *                                or both
270  *
271  * RETURN:      Status
272  *
273  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
274  *              hardware-enabled (for runtime GPEs), or the GPE register mask
275  *              is updated (for wake GPEs).
276  *
277  ******************************************************************************/
278 acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number, u8 gpe_type)
279 {
280         acpi_status status = AE_OK;
281         struct acpi_gpe_event_info *gpe_event_info;
282         acpi_cpu_flags flags;
283
284         ACPI_FUNCTION_TRACE(acpi_enable_gpe);
285
286         /* Parameter validation */
287
288         if (!gpe_type || (gpe_type & ~ACPI_GPE_TYPE_WAKE_RUN)) {
289                 return_ACPI_STATUS(AE_BAD_PARAMETER);
290         }
291
292         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
293
294         /* Ensure that we have a valid GPE number */
295
296         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
297         if (!gpe_event_info) {
298                 status = AE_BAD_PARAMETER;
299                 goto unlock_and_exit;
300         }
301
302         if (gpe_type & ACPI_GPE_TYPE_RUNTIME) {
303                 if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
304                         status = AE_LIMIT;      /* Too many references */
305                         goto unlock_and_exit;
306                 }
307
308                 gpe_event_info->runtime_count++;
309                 if (gpe_event_info->runtime_count == 1) {
310                         status = acpi_ev_enable_gpe(gpe_event_info);
311                         if (ACPI_FAILURE(status)) {
312                                 gpe_event_info->runtime_count--;
313                                 goto unlock_and_exit;
314                         }
315                 }
316         }
317
318         if (gpe_type & ACPI_GPE_TYPE_WAKE) {
319                 /* The GPE must have the ability to wake the system */
320
321                 if (!(gpe_event_info->flags & ACPI_GPE_CAN_WAKE)) {
322                         status = AE_TYPE;
323                         goto unlock_and_exit;
324                 }
325
326                 if (gpe_event_info->wakeup_count == ACPI_UINT8_MAX) {
327                         status = AE_LIMIT;      /* Too many references */
328                         goto unlock_and_exit;
329                 }
330
331                 /*
332                  * Update the enable mask on the first wakeup reference. Wake GPEs
333                  * are only hardware-enabled just before sleeping.
334                  */
335                 gpe_event_info->wakeup_count++;
336                 if (gpe_event_info->wakeup_count == 1) {
337                         (void)acpi_ev_update_gpe_enable_masks(gpe_event_info);
338                 }
339         }
340
341 unlock_and_exit:
342         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
343         return_ACPI_STATUS(status);
344 }
345 ACPI_EXPORT_SYMBOL(acpi_enable_gpe)
346
347 /*******************************************************************************
348  *
349  * FUNCTION:    acpi_disable_gpe
350  *
351  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
352  *              gpe_number      - GPE level within the GPE block
353  *              gpe_type        - ACPI_GPE_TYPE_RUNTIME or ACPI_GPE_TYPE_WAKE
354  *                                or both
355  *
356  * RETURN:      Status
357  *
358  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
359  *              removed, only then is the GPE disabled (for runtime GPEs), or
360  *              the GPE mask bit disabled (for wake GPEs)
361  *
362  ******************************************************************************/
363 acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number, u8 gpe_type)
364 {
365         acpi_status status = AE_OK;
366         struct acpi_gpe_event_info *gpe_event_info;
367         acpi_cpu_flags flags;
368
369         ACPI_FUNCTION_TRACE(acpi_disable_gpe);
370
371         /* Parameter validation */
372
373         if (!gpe_type || (gpe_type & ~ACPI_GPE_TYPE_WAKE_RUN)) {
374                 return_ACPI_STATUS(AE_BAD_PARAMETER);
375         }
376
377         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
378
379         /* Ensure that we have a valid GPE number */
380
381         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
382         if (!gpe_event_info) {
383                 status = AE_BAD_PARAMETER;
384                 goto unlock_and_exit;
385         }
386
387         /* Hardware-disable a runtime GPE on removal of the last reference */
388
389         if (gpe_type & ACPI_GPE_TYPE_RUNTIME) {
390                 if (!gpe_event_info->runtime_count) {
391                         status = AE_LIMIT;      /* There are no references to remove */
392                         goto unlock_and_exit;
393                 }
394
395                 gpe_event_info->runtime_count--;
396                 if (!gpe_event_info->runtime_count) {
397                         status = acpi_ev_disable_gpe(gpe_event_info);
398                         if (ACPI_FAILURE(status)) {
399                                 gpe_event_info->runtime_count++;
400                                 goto unlock_and_exit;
401                         }
402                 }
403         }
404
405         /*
406          * Update masks for wake GPE on removal of the last reference.
407          * No need to hardware-disable wake GPEs here, they are not currently
408          * enabled.
409          */
410         if (gpe_type & ACPI_GPE_TYPE_WAKE) {
411                 if (!gpe_event_info->wakeup_count) {
412                         status = AE_LIMIT;      /* There are no references to remove */
413                         goto unlock_and_exit;
414                 }
415
416                 gpe_event_info->wakeup_count--;
417                 if (!gpe_event_info->wakeup_count) {
418                         (void)acpi_ev_update_gpe_enable_masks(gpe_event_info);
419                 }
420         }
421
422 unlock_and_exit:
423         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
424         return_ACPI_STATUS(status);
425 }
426 ACPI_EXPORT_SYMBOL(acpi_disable_gpe)
427
428 /*******************************************************************************
429  *
430  * FUNCTION:    acpi_disable_event
431  *
432  * PARAMETERS:  Event           - The fixed eventto be enabled
433  *              Flags           - Reserved
434  *
435  * RETURN:      Status
436  *
437  * DESCRIPTION: Disable an ACPI event (fixed)
438  *
439  ******************************************************************************/
440 acpi_status acpi_disable_event(u32 event, u32 flags)
441 {
442         acpi_status status = AE_OK;
443         u32 value;
444
445         ACPI_FUNCTION_TRACE(acpi_disable_event);
446
447         /* Decode the Fixed Event */
448
449         if (event > ACPI_EVENT_MAX) {
450                 return_ACPI_STATUS(AE_BAD_PARAMETER);
451         }
452
453         /*
454          * Disable the requested fixed event (by writing a zero to the enable
455          * register bit)
456          */
457         status =
458             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
459                                     enable_register_id, ACPI_DISABLE_EVENT);
460         if (ACPI_FAILURE(status)) {
461                 return_ACPI_STATUS(status);
462         }
463
464         status =
465             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
466                                    enable_register_id, &value);
467         if (ACPI_FAILURE(status)) {
468                 return_ACPI_STATUS(status);
469         }
470
471         if (value != 0) {
472                 ACPI_ERROR((AE_INFO,
473                             "Could not disable %s events",
474                             acpi_ut_get_event_name(event)));
475                 return_ACPI_STATUS(AE_NO_HARDWARE_RESPONSE);
476         }
477
478         return_ACPI_STATUS(status);
479 }
480
481 ACPI_EXPORT_SYMBOL(acpi_disable_event)
482
483 /*******************************************************************************
484  *
485  * FUNCTION:    acpi_clear_event
486  *
487  * PARAMETERS:  Event           - The fixed event to be cleared
488  *
489  * RETURN:      Status
490  *
491  * DESCRIPTION: Clear an ACPI event (fixed)
492  *
493  ******************************************************************************/
494 acpi_status acpi_clear_event(u32 event)
495 {
496         acpi_status status = AE_OK;
497
498         ACPI_FUNCTION_TRACE(acpi_clear_event);
499
500         /* Decode the Fixed Event */
501
502         if (event > ACPI_EVENT_MAX) {
503                 return_ACPI_STATUS(AE_BAD_PARAMETER);
504         }
505
506         /*
507          * Clear the requested fixed event (By writing a one to the status
508          * register bit)
509          */
510         status =
511             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
512                                     status_register_id, ACPI_CLEAR_STATUS);
513
514         return_ACPI_STATUS(status);
515 }
516
517 ACPI_EXPORT_SYMBOL(acpi_clear_event)
518
519 /*******************************************************************************
520  *
521  * FUNCTION:    acpi_clear_gpe
522  *
523  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
524  *              gpe_number      - GPE level within the GPE block
525  *
526  * RETURN:      Status
527  *
528  * DESCRIPTION: Clear an ACPI event (general purpose)
529  *
530  ******************************************************************************/
531 acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number)
532 {
533         acpi_status status = AE_OK;
534         struct acpi_gpe_event_info *gpe_event_info;
535         acpi_cpu_flags flags;
536
537         ACPI_FUNCTION_TRACE(acpi_clear_gpe);
538
539         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
540
541         /* Ensure that we have a valid GPE number */
542
543         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
544         if (!gpe_event_info) {
545                 status = AE_BAD_PARAMETER;
546                 goto unlock_and_exit;
547         }
548
549         status = acpi_hw_clear_gpe(gpe_event_info);
550
551       unlock_and_exit:
552         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
553         return_ACPI_STATUS(status);
554 }
555
556 ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
557 /*******************************************************************************
558  *
559  * FUNCTION:    acpi_get_event_status
560  *
561  * PARAMETERS:  Event           - The fixed event
562  *              event_status    - Where the current status of the event will
563  *                                be returned
564  *
565  * RETURN:      Status
566  *
567  * DESCRIPTION: Obtains and returns the current status of the event
568  *
569  ******************************************************************************/
570 acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status)
571 {
572         acpi_status status = AE_OK;
573         u32 value;
574
575         ACPI_FUNCTION_TRACE(acpi_get_event_status);
576
577         if (!event_status) {
578                 return_ACPI_STATUS(AE_BAD_PARAMETER);
579         }
580
581         /* Decode the Fixed Event */
582
583         if (event > ACPI_EVENT_MAX) {
584                 return_ACPI_STATUS(AE_BAD_PARAMETER);
585         }
586
587         /* Get the status of the requested fixed event */
588
589         status =
590             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
591                               enable_register_id, &value);
592         if (ACPI_FAILURE(status))
593                 return_ACPI_STATUS(status);
594
595         *event_status = value;
596
597         status =
598             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
599                               status_register_id, &value);
600         if (ACPI_FAILURE(status))
601                 return_ACPI_STATUS(status);
602
603         if (value)
604                 *event_status |= ACPI_EVENT_FLAG_SET;
605
606         if (acpi_gbl_fixed_event_handlers[event].handler)
607                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
608
609         return_ACPI_STATUS(status);
610 }
611
612 ACPI_EXPORT_SYMBOL(acpi_get_event_status)
613
614 /*******************************************************************************
615  *
616  * FUNCTION:    acpi_get_gpe_status
617  *
618  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
619  *              gpe_number      - GPE level within the GPE block
620  *              event_status    - Where the current status of the event will
621  *                                be returned
622  *
623  * RETURN:      Status
624  *
625  * DESCRIPTION: Get status of an event (general purpose)
626  *
627  ******************************************************************************/
628 acpi_status
629 acpi_get_gpe_status(acpi_handle gpe_device,
630                     u32 gpe_number, acpi_event_status *event_status)
631 {
632         acpi_status status = AE_OK;
633         struct acpi_gpe_event_info *gpe_event_info;
634         acpi_cpu_flags flags;
635
636         ACPI_FUNCTION_TRACE(acpi_get_gpe_status);
637
638         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
639
640         /* Ensure that we have a valid GPE number */
641
642         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
643         if (!gpe_event_info) {
644                 status = AE_BAD_PARAMETER;
645                 goto unlock_and_exit;
646         }
647
648         /* Obtain status on the requested GPE number */
649
650         status = acpi_hw_get_gpe_status(gpe_event_info, event_status);
651
652         if (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)
653                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
654
655       unlock_and_exit:
656         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
657         return_ACPI_STATUS(status);
658 }
659
660 ACPI_EXPORT_SYMBOL(acpi_get_gpe_status)
661 /*******************************************************************************
662  *
663  * FUNCTION:    acpi_install_gpe_block
664  *
665  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
666  *              gpe_block_address   - Address and space_iD
667  *              register_count      - Number of GPE register pairs in the block
668  *              interrupt_number    - H/W interrupt for the block
669  *
670  * RETURN:      Status
671  *
672  * DESCRIPTION: Create and Install a block of GPE registers
673  *
674  ******************************************************************************/
675 acpi_status
676 acpi_install_gpe_block(acpi_handle gpe_device,
677                        struct acpi_generic_address *gpe_block_address,
678                        u32 register_count, u32 interrupt_number)
679 {
680         acpi_status status;
681         union acpi_operand_object *obj_desc;
682         struct acpi_namespace_node *node;
683         struct acpi_gpe_block_info *gpe_block;
684
685         ACPI_FUNCTION_TRACE(acpi_install_gpe_block);
686
687         if ((!gpe_device) || (!gpe_block_address) || (!register_count)) {
688                 return_ACPI_STATUS(AE_BAD_PARAMETER);
689         }
690
691         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
692         if (ACPI_FAILURE(status)) {
693                 return (status);
694         }
695
696         node = acpi_ns_validate_handle(gpe_device);
697         if (!node) {
698                 status = AE_BAD_PARAMETER;
699                 goto unlock_and_exit;
700         }
701
702         /*
703          * For user-installed GPE Block Devices, the gpe_block_base_number
704          * is always zero
705          */
706         status =
707             acpi_ev_create_gpe_block(node, gpe_block_address, register_count, 0,
708                                      interrupt_number, &gpe_block);
709         if (ACPI_FAILURE(status)) {
710                 goto unlock_and_exit;
711         }
712
713         /* Install block in the device_object attached to the node */
714
715         obj_desc = acpi_ns_get_attached_object(node);
716         if (!obj_desc) {
717
718                 /*
719                  * No object, create a new one (Device nodes do not always have
720                  * an attached object)
721                  */
722                 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_DEVICE);
723                 if (!obj_desc) {
724                         status = AE_NO_MEMORY;
725                         goto unlock_and_exit;
726                 }
727
728                 status =
729                     acpi_ns_attach_object(node, obj_desc, ACPI_TYPE_DEVICE);
730
731                 /* Remove local reference to the object */
732
733                 acpi_ut_remove_reference(obj_desc);
734
735                 if (ACPI_FAILURE(status)) {
736                         goto unlock_and_exit;
737                 }
738         }
739
740         /* Now install the GPE block in the device_object */
741
742         obj_desc->device.gpe_block = gpe_block;
743
744         /* Run the _PRW methods and enable the runtime GPEs in the new block */
745
746         status = acpi_ev_initialize_gpe_block(node, gpe_block);
747
748       unlock_and_exit:
749         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
750         return_ACPI_STATUS(status);
751 }
752
753 ACPI_EXPORT_SYMBOL(acpi_install_gpe_block)
754
755 /*******************************************************************************
756  *
757  * FUNCTION:    acpi_remove_gpe_block
758  *
759  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
760  *
761  * RETURN:      Status
762  *
763  * DESCRIPTION: Remove a previously installed block of GPE registers
764  *
765  ******************************************************************************/
766 acpi_status acpi_remove_gpe_block(acpi_handle gpe_device)
767 {
768         union acpi_operand_object *obj_desc;
769         acpi_status status;
770         struct acpi_namespace_node *node;
771
772         ACPI_FUNCTION_TRACE(acpi_remove_gpe_block);
773
774         if (!gpe_device) {
775                 return_ACPI_STATUS(AE_BAD_PARAMETER);
776         }
777
778         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
779         if (ACPI_FAILURE(status)) {
780                 return (status);
781         }
782
783         node = acpi_ns_validate_handle(gpe_device);
784         if (!node) {
785                 status = AE_BAD_PARAMETER;
786                 goto unlock_and_exit;
787         }
788
789         /* Get the device_object attached to the node */
790
791         obj_desc = acpi_ns_get_attached_object(node);
792         if (!obj_desc || !obj_desc->device.gpe_block) {
793                 return_ACPI_STATUS(AE_NULL_OBJECT);
794         }
795
796         /* Delete the GPE block (but not the device_object) */
797
798         status = acpi_ev_delete_gpe_block(obj_desc->device.gpe_block);
799         if (ACPI_SUCCESS(status)) {
800                 obj_desc->device.gpe_block = NULL;
801         }
802
803       unlock_and_exit:
804         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
805         return_ACPI_STATUS(status);
806 }
807
808 ACPI_EXPORT_SYMBOL(acpi_remove_gpe_block)
809
810 /*******************************************************************************
811  *
812  * FUNCTION:    acpi_get_gpe_device
813  *
814  * PARAMETERS:  Index               - System GPE index (0-current_gpe_count)
815  *              gpe_device          - Where the parent GPE Device is returned
816  *
817  * RETURN:      Status
818  *
819  * DESCRIPTION: Obtain the GPE device associated with the input index. A NULL
820  *              gpe device indicates that the gpe number is contained in one of
821  *              the FADT-defined gpe blocks. Otherwise, the GPE block device.
822  *
823  ******************************************************************************/
824 acpi_status
825 acpi_get_gpe_device(u32 index, acpi_handle *gpe_device)
826 {
827         struct acpi_gpe_device_info info;
828         acpi_status status;
829
830         ACPI_FUNCTION_TRACE(acpi_get_gpe_device);
831
832         if (!gpe_device) {
833                 return_ACPI_STATUS(AE_BAD_PARAMETER);
834         }
835
836         if (index >= acpi_current_gpe_count) {
837                 return_ACPI_STATUS(AE_NOT_EXIST);
838         }
839
840         /* Setup and walk the GPE list */
841
842         info.index = index;
843         info.status = AE_NOT_EXIST;
844         info.gpe_device = NULL;
845         info.next_block_base_index = 0;
846
847         status = acpi_ev_walk_gpe_list(acpi_ev_get_gpe_device, &info);
848         if (ACPI_FAILURE(status)) {
849                 return_ACPI_STATUS(status);
850         }
851
852         *gpe_device = info.gpe_device;
853         return_ACPI_STATUS(info.status);
854 }
855
856 ACPI_EXPORT_SYMBOL(acpi_get_gpe_device)
857
858 /*******************************************************************************
859  *
860  * FUNCTION:    acpi_ev_get_gpe_device
861  *
862  * PARAMETERS:  GPE_WALK_CALLBACK
863  *
864  * RETURN:      Status
865  *
866  * DESCRIPTION: Matches the input GPE index (0-current_gpe_count) with a GPE
867  *              block device. NULL if the GPE is one of the FADT-defined GPEs.
868  *
869  ******************************************************************************/
870 static acpi_status
871 acpi_ev_get_gpe_device(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
872                        struct acpi_gpe_block_info *gpe_block, void *context)
873 {
874         struct acpi_gpe_device_info *info = context;
875
876         /* Increment Index by the number of GPEs in this block */
877
878         info->next_block_base_index += gpe_block->gpe_count;
879
880         if (info->index < info->next_block_base_index) {
881                 /*
882                  * The GPE index is within this block, get the node. Leave the node
883                  * NULL for the FADT-defined GPEs
884                  */
885                 if ((gpe_block->node)->type == ACPI_TYPE_DEVICE) {
886                         info->gpe_device = gpe_block->node;
887                 }
888
889                 info->status = AE_OK;
890                 return (AE_CTRL_END);
891         }
892
893         return (AE_OK);
894 }
895
896 /******************************************************************************
897  *
898  * FUNCTION:    acpi_disable_all_gpes
899  *
900  * PARAMETERS:  None
901  *
902  * RETURN:      Status
903  *
904  * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
905  *
906  ******************************************************************************/
907
908 acpi_status acpi_disable_all_gpes(void)
909 {
910         acpi_status status;
911
912         ACPI_FUNCTION_TRACE(acpi_disable_all_gpes);
913
914         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
915         if (ACPI_FAILURE(status)) {
916                 return_ACPI_STATUS(status);
917         }
918
919         status = acpi_hw_disable_all_gpes();
920         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
921
922         return_ACPI_STATUS(status);
923 }
924
925 /******************************************************************************
926  *
927  * FUNCTION:    acpi_enable_all_runtime_gpes
928  *
929  * PARAMETERS:  None
930  *
931  * RETURN:      Status
932  *
933  * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
934  *
935  ******************************************************************************/
936
937 acpi_status acpi_enable_all_runtime_gpes(void)
938 {
939         acpi_status status;
940
941         ACPI_FUNCTION_TRACE(acpi_enable_all_runtime_gpes);
942
943         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
944         if (ACPI_FAILURE(status)) {
945                 return_ACPI_STATUS(status);
946         }
947
948         status = acpi_hw_enable_all_runtime_gpes();
949         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
950
951         return_ACPI_STATUS(status);
952 }