9b8a1ceec1bbc28a7589948270e262eead541193
[reactos.git] / reactos / drivers / bus / acpi / acpica / events / evxface.c
1 /******************************************************************************
2 *
3 * Module Name: evxface - External interfaces for ACPI events
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2014, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116 #define EXPORT_ACPI_INTERFACES
117
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "acnamesp.h"
121 #include "acevents.h"
122 #include "acinterp.h"
123
124 #define _COMPONENT ACPI_EVENTS
125 ACPI_MODULE_NAME ("evxface")
126
127
128 /*******************************************************************************
129 *
130 * FUNCTION: AcpiInstallNotifyHandler
131 *
132 * PARAMETERS: Device - The device for which notifies will be handled
133 * HandlerType - The type of handler:
134 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
135 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
136 * ACPI_ALL_NOTIFY: Both System and Device
137 * Handler - Address of the handler
138 * Context - Value passed to the handler on each GPE
139 *
140 * RETURN: Status
141 *
142 * DESCRIPTION: Install a handler for notifications on an ACPI Device,
143 * ThermalZone, or Processor object.
144 *
145 * NOTES: The Root namespace object may have only one handler for each
146 * type of notify (System/Device). Device/Thermal/Processor objects
147 * may have one device notify handler, and multiple system notify
148 * handlers.
149 *
150 ******************************************************************************/
151
152 ACPI_STATUS
153 AcpiInstallNotifyHandler (
154 ACPI_HANDLE Device,
155 UINT32 HandlerType,
156 ACPI_NOTIFY_HANDLER Handler,
157 void *Context)
158 {
159 ACPI_NAMESPACE_NODE *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);
160 ACPI_OPERAND_OBJECT *ObjDesc;
161 ACPI_OPERAND_OBJECT *HandlerObj;
162 ACPI_STATUS Status;
163 UINT32 i;
164
165
166 ACPI_FUNCTION_TRACE (AcpiInstallNotifyHandler);
167
168
169 /* Parameter validation */
170
171 if ((!Device) || (!Handler) || (!HandlerType) ||
172 (HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))
173 {
174 return_ACPI_STATUS (AE_BAD_PARAMETER);
175 }
176
177 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
178 if (ACPI_FAILURE (Status))
179 {
180 return_ACPI_STATUS (Status);
181 }
182
183 /*
184 * Root Object:
185 * Registering a notify handler on the root object indicates that the
186 * caller wishes to receive notifications for all objects. Note that
187 * only one global handler can be registered per notify type.
188 * Ensure that a handler is not already installed.
189 */
190 if (Device == ACPI_ROOT_OBJECT)
191 {
192 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
193 {
194 if (HandlerType & (i+1))
195 {
196 if (AcpiGbl_GlobalNotify[i].Handler)
197 {
198 Status = AE_ALREADY_EXISTS;
199 goto UnlockAndExit;
200 }
201
202 AcpiGbl_GlobalNotify[i].Handler = Handler;
203 AcpiGbl_GlobalNotify[i].Context = Context;
204 }
205 }
206
207 goto UnlockAndExit; /* Global notify handler installed, all done */
208 }
209
210 /*
211 * All Other Objects:
212 * Caller will only receive notifications specific to the target
213 * object. Note that only certain object types are allowed to
214 * receive notifications.
215 */
216
217 /* Are Notifies allowed on this object? */
218
219 if (!AcpiEvIsNotifyObject (Node))
220 {
221 Status = AE_TYPE;
222 goto UnlockAndExit;
223 }
224
225 /* Check for an existing internal object, might not exist */
226
227 ObjDesc = AcpiNsGetAttachedObject (Node);
228 if (!ObjDesc)
229 {
230 /* Create a new object */
231
232 ObjDesc = AcpiUtCreateInternalObject (Node->Type);
233 if (!ObjDesc)
234 {
235 Status = AE_NO_MEMORY;
236 goto UnlockAndExit;
237 }
238
239 /* Attach new object to the Node, remove local reference */
240
241 Status = AcpiNsAttachObject (Device, ObjDesc, Node->Type);
242 AcpiUtRemoveReference (ObjDesc);
243 if (ACPI_FAILURE (Status))
244 {
245 goto UnlockAndExit;
246 }
247 }
248
249 /* Ensure that the handler is not already installed in the lists */
250
251 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
252 {
253 if (HandlerType & (i+1))
254 {
255 HandlerObj = ObjDesc->CommonNotify.NotifyList[i];
256 while (HandlerObj)
257 {
258 if (HandlerObj->Notify.Handler == Handler)
259 {
260 Status = AE_ALREADY_EXISTS;
261 goto UnlockAndExit;
262 }
263
264 HandlerObj = HandlerObj->Notify.Next[i];
265 }
266 }
267 }
268
269 /* Create and populate a new notify handler object */
270
271 HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_NOTIFY);
272 if (!HandlerObj)
273 {
274 Status = AE_NO_MEMORY;
275 goto UnlockAndExit;
276 }
277
278 HandlerObj->Notify.Node = Node;
279 HandlerObj->Notify.HandlerType = HandlerType;
280 HandlerObj->Notify.Handler = Handler;
281 HandlerObj->Notify.Context = Context;
282
283 /* Install the handler at the list head(s) */
284
285 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
286 {
287 if (HandlerType & (i+1))
288 {
289 HandlerObj->Notify.Next[i] =
290 ObjDesc->CommonNotify.NotifyList[i];
291
292 ObjDesc->CommonNotify.NotifyList[i] = HandlerObj;
293 }
294 }
295
296 /* Add an extra reference if handler was installed in both lists */
297
298 if (HandlerType == ACPI_ALL_NOTIFY)
299 {
300 AcpiUtAddReference (HandlerObj);
301 }
302
303
304 UnlockAndExit:
305 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
306 return_ACPI_STATUS (Status);
307 }
308
309 ACPI_EXPORT_SYMBOL (AcpiInstallNotifyHandler)
310
311
312 /*******************************************************************************
313 *
314 * FUNCTION: AcpiRemoveNotifyHandler
315 *
316 * PARAMETERS: Device - The device for which the handler is installed
317 * HandlerType - The type of handler:
318 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
319 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
320 * ACPI_ALL_NOTIFY: Both System and Device
321 * Handler - Address of the handler
322 *
323 * RETURN: Status
324 *
325 * DESCRIPTION: Remove a handler for notifies on an ACPI device
326 *
327 ******************************************************************************/
328
329 ACPI_STATUS
330 AcpiRemoveNotifyHandler (
331 ACPI_HANDLE Device,
332 UINT32 HandlerType,
333 ACPI_NOTIFY_HANDLER Handler)
334 {
335 ACPI_NAMESPACE_NODE *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);
336 ACPI_OPERAND_OBJECT *ObjDesc;
337 ACPI_OPERAND_OBJECT *HandlerObj;
338 ACPI_OPERAND_OBJECT *PreviousHandlerObj;
339 ACPI_STATUS Status = AE_OK;
340 UINT32 i;
341
342
343 ACPI_FUNCTION_TRACE (AcpiRemoveNotifyHandler);
344
345
346 /* Parameter validation */
347
348 if ((!Device) || (!Handler) || (!HandlerType) ||
349 (HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))
350 {
351 return_ACPI_STATUS (AE_BAD_PARAMETER);
352 }
353
354 /* Root Object. Global handlers are removed here */
355
356 if (Device == ACPI_ROOT_OBJECT)
357 {
358 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
359 {
360 if (HandlerType & (i+1))
361 {
362 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
363 if (ACPI_FAILURE (Status))
364 {
365 return_ACPI_STATUS (Status);
366 }
367
368 if (!AcpiGbl_GlobalNotify[i].Handler ||
369 (AcpiGbl_GlobalNotify[i].Handler != Handler))
370 {
371 Status = AE_NOT_EXIST;
372 goto UnlockAndExit;
373 }
374
375 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
376 "Removing global notify handler\n"));
377
378 AcpiGbl_GlobalNotify[i].Handler = NULL;
379 AcpiGbl_GlobalNotify[i].Context = NULL;
380
381 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
382
383 /* Make sure all deferred notify tasks are completed */
384
385 AcpiOsWaitEventsComplete ();
386 }
387 }
388
389 return_ACPI_STATUS (AE_OK);
390 }
391
392 /* All other objects: Are Notifies allowed on this object? */
393
394 if (!AcpiEvIsNotifyObject (Node))
395 {
396 return_ACPI_STATUS (AE_TYPE);
397 }
398
399 /* Must have an existing internal object */
400
401 ObjDesc = AcpiNsGetAttachedObject (Node);
402 if (!ObjDesc)
403 {
404 return_ACPI_STATUS (AE_NOT_EXIST);
405 }
406
407 /* Internal object exists. Find the handler and remove it */
408
409 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
410 {
411 if (HandlerType & (i+1))
412 {
413 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
414 if (ACPI_FAILURE (Status))
415 {
416 return_ACPI_STATUS (Status);
417 }
418
419 HandlerObj = ObjDesc->CommonNotify.NotifyList[i];
420 PreviousHandlerObj = NULL;
421
422 /* Attempt to find the handler in the handler list */
423
424 while (HandlerObj &&
425 (HandlerObj->Notify.Handler != Handler))
426 {
427 PreviousHandlerObj = HandlerObj;
428 HandlerObj = HandlerObj->Notify.Next[i];
429 }
430
431 if (!HandlerObj)
432 {
433 Status = AE_NOT_EXIST;
434 goto UnlockAndExit;
435 }
436
437 /* Remove the handler object from the list */
438
439 if (PreviousHandlerObj) /* Handler is not at the list head */
440 {
441 PreviousHandlerObj->Notify.Next[i] =
442 HandlerObj->Notify.Next[i];
443 }
444 else /* Handler is at the list head */
445 {
446 ObjDesc->CommonNotify.NotifyList[i] =
447 HandlerObj->Notify.Next[i];
448 }
449
450 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
451
452 /* Make sure all deferred notify tasks are completed */
453
454 AcpiOsWaitEventsComplete ();
455 AcpiUtRemoveReference (HandlerObj);
456 }
457 }
458
459 return_ACPI_STATUS (Status);
460
461
462 UnlockAndExit:
463 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
464 return_ACPI_STATUS (Status);
465 }
466
467 ACPI_EXPORT_SYMBOL (AcpiRemoveNotifyHandler)
468
469
470 /*******************************************************************************
471 *
472 * FUNCTION: AcpiInstallExceptionHandler
473 *
474 * PARAMETERS: Handler - Pointer to the handler function for the
475 * event
476 *
477 * RETURN: Status
478 *
479 * DESCRIPTION: Saves the pointer to the handler function
480 *
481 ******************************************************************************/
482
483 ACPI_STATUS
484 AcpiInstallExceptionHandler (
485 ACPI_EXCEPTION_HANDLER Handler)
486 {
487 ACPI_STATUS Status;
488
489
490 ACPI_FUNCTION_TRACE (AcpiInstallExceptionHandler);
491
492
493 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
494 if (ACPI_FAILURE (Status))
495 {
496 return_ACPI_STATUS (Status);
497 }
498
499 /* Don't allow two handlers. */
500
501 if (AcpiGbl_ExceptionHandler)
502 {
503 Status = AE_ALREADY_EXISTS;
504 goto Cleanup;
505 }
506
507 /* Install the handler */
508
509 AcpiGbl_ExceptionHandler = Handler;
510
511 Cleanup:
512 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
513 return_ACPI_STATUS (Status);
514 }
515
516 ACPI_EXPORT_SYMBOL (AcpiInstallExceptionHandler)
517
518
519 #if (!ACPI_REDUCED_HARDWARE)
520 /*******************************************************************************
521 *
522 * FUNCTION: AcpiInstallSciHandler
523 *
524 * PARAMETERS: Address - Address of the handler
525 * Context - Value passed to the handler on each SCI
526 *
527 * RETURN: Status
528 *
529 * DESCRIPTION: Install a handler for a System Control Interrupt.
530 *
531 ******************************************************************************/
532
533 ACPI_STATUS
534 AcpiInstallSciHandler (
535 ACPI_SCI_HANDLER Address,
536 void *Context)
537 {
538 ACPI_SCI_HANDLER_INFO *NewSciHandler;
539 ACPI_SCI_HANDLER_INFO *SciHandler;
540 ACPI_CPU_FLAGS Flags;
541 ACPI_STATUS Status;
542
543
544 ACPI_FUNCTION_TRACE (AcpiInstallSciHandler);
545
546
547 if (!Address)
548 {
549 return_ACPI_STATUS (AE_BAD_PARAMETER);
550 }
551
552 /* Allocate and init a handler object */
553
554 NewSciHandler = ACPI_ALLOCATE (sizeof (ACPI_SCI_HANDLER_INFO));
555 if (!NewSciHandler)
556 {
557 return_ACPI_STATUS (AE_NO_MEMORY);
558 }
559
560 NewSciHandler->Address = Address;
561 NewSciHandler->Context = Context;
562
563 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
564 if (ACPI_FAILURE (Status))
565 {
566 goto Exit;
567 }
568
569 /* Lock list during installation */
570
571 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
572 SciHandler = AcpiGbl_SciHandlerList;
573
574 /* Ensure handler does not already exist */
575
576 while (SciHandler)
577 {
578 if (Address == SciHandler->Address)
579 {
580 Status = AE_ALREADY_EXISTS;
581 goto UnlockAndExit;
582 }
583
584 SciHandler = SciHandler->Next;
585 }
586
587 /* Install the new handler into the global list (at head) */
588
589 NewSciHandler->Next = AcpiGbl_SciHandlerList;
590 AcpiGbl_SciHandlerList = NewSciHandler;
591
592
593 UnlockAndExit:
594
595 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
596 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
597
598 Exit:
599 if (ACPI_FAILURE (Status))
600 {
601 ACPI_FREE (NewSciHandler);
602 }
603 return_ACPI_STATUS (Status);
604 }
605
606 ACPI_EXPORT_SYMBOL (AcpiInstallSciHandler)
607
608
609 /*******************************************************************************
610 *
611 * FUNCTION: AcpiRemoveSciHandler
612 *
613 * PARAMETERS: Address - Address of the handler
614 *
615 * RETURN: Status
616 *
617 * DESCRIPTION: Remove a handler for a System Control Interrupt.
618 *
619 ******************************************************************************/
620
621 ACPI_STATUS
622 AcpiRemoveSciHandler (
623 ACPI_SCI_HANDLER Address)
624 {
625 ACPI_SCI_HANDLER_INFO *PrevSciHandler;
626 ACPI_SCI_HANDLER_INFO *NextSciHandler;
627 ACPI_CPU_FLAGS Flags;
628 ACPI_STATUS Status;
629
630
631 ACPI_FUNCTION_TRACE (AcpiRemoveSciHandler);
632
633
634 if (!Address)
635 {
636 return_ACPI_STATUS (AE_BAD_PARAMETER);
637 }
638
639 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
640 if (ACPI_FAILURE (Status))
641 {
642 return_ACPI_STATUS (Status);
643 }
644
645 /* Remove the SCI handler with lock */
646
647 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
648
649 PrevSciHandler = NULL;
650 NextSciHandler = AcpiGbl_SciHandlerList;
651 while (NextSciHandler)
652 {
653 if (NextSciHandler->Address == Address)
654 {
655 /* Unlink and free the SCI handler info block */
656
657 if (PrevSciHandler)
658 {
659 PrevSciHandler->Next = NextSciHandler->Next;
660 }
661 else
662 {
663 AcpiGbl_SciHandlerList = NextSciHandler->Next;
664 }
665
666 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
667 ACPI_FREE (NextSciHandler);
668 goto UnlockAndExit;
669 }
670
671 PrevSciHandler = NextSciHandler;
672 NextSciHandler = NextSciHandler->Next;
673 }
674
675 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
676 Status = AE_NOT_EXIST;
677
678
679 UnlockAndExit:
680 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
681 return_ACPI_STATUS (Status);
682 }
683
684 ACPI_EXPORT_SYMBOL (AcpiRemoveSciHandler)
685
686
687 /*******************************************************************************
688 *
689 * FUNCTION: AcpiInstallGlobalEventHandler
690 *
691 * PARAMETERS: Handler - Pointer to the global event handler function
692 * Context - Value passed to the handler on each event
693 *
694 * RETURN: Status
695 *
696 * DESCRIPTION: Saves the pointer to the handler function. The global handler
697 * is invoked upon each incoming GPE and Fixed Event. It is
698 * invoked at interrupt level at the time of the event dispatch.
699 * Can be used to update event counters, etc.
700 *
701 ******************************************************************************/
702
703 ACPI_STATUS
704 AcpiInstallGlobalEventHandler (
705 ACPI_GBL_EVENT_HANDLER Handler,
706 void *Context)
707 {
708 ACPI_STATUS Status;
709
710
711 ACPI_FUNCTION_TRACE (AcpiInstallGlobalEventHandler);
712
713
714 /* Parameter validation */
715
716 if (!Handler)
717 {
718 return_ACPI_STATUS (AE_BAD_PARAMETER);
719 }
720
721 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
722 if (ACPI_FAILURE (Status))
723 {
724 return_ACPI_STATUS (Status);
725 }
726
727 /* Don't allow two handlers. */
728
729 if (AcpiGbl_GlobalEventHandler)
730 {
731 Status = AE_ALREADY_EXISTS;
732 goto Cleanup;
733 }
734
735 AcpiGbl_GlobalEventHandler = Handler;
736 AcpiGbl_GlobalEventHandlerContext = Context;
737
738
739 Cleanup:
740 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
741 return_ACPI_STATUS (Status);
742 }
743
744 ACPI_EXPORT_SYMBOL (AcpiInstallGlobalEventHandler)
745
746
747 /*******************************************************************************
748 *
749 * FUNCTION: AcpiInstallFixedEventHandler
750 *
751 * PARAMETERS: Event - Event type to enable.
752 * Handler - Pointer to the handler function for the
753 * event
754 * Context - Value passed to the handler on each GPE
755 *
756 * RETURN: Status
757 *
758 * DESCRIPTION: Saves the pointer to the handler function and then enables the
759 * event.
760 *
761 ******************************************************************************/
762
763 ACPI_STATUS
764 AcpiInstallFixedEventHandler (
765 UINT32 Event,
766 ACPI_EVENT_HANDLER Handler,
767 void *Context)
768 {
769 ACPI_STATUS Status;
770
771
772 ACPI_FUNCTION_TRACE (AcpiInstallFixedEventHandler);
773
774
775 /* Parameter validation */
776
777 if (Event > ACPI_EVENT_MAX)
778 {
779 return_ACPI_STATUS (AE_BAD_PARAMETER);
780 }
781
782 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
783 if (ACPI_FAILURE (Status))
784 {
785 return_ACPI_STATUS (Status);
786 }
787
788 /* Do not allow multiple handlers */
789
790 if (AcpiGbl_FixedEventHandlers[Event].Handler)
791 {
792 Status = AE_ALREADY_EXISTS;
793 goto Cleanup;
794 }
795
796 /* Install the handler before enabling the event */
797
798 AcpiGbl_FixedEventHandlers[Event].Handler = Handler;
799 AcpiGbl_FixedEventHandlers[Event].Context = Context;
800
801 Status = AcpiEnableEvent (Event, 0);
802 if (ACPI_FAILURE (Status))
803 {
804 ACPI_WARNING ((AE_INFO,
805 "Could not enable fixed event - %s (%u)",
806 AcpiUtGetEventName (Event), Event));
807
808 /* Remove the handler */
809
810 AcpiGbl_FixedEventHandlers[Event].Handler = NULL;
811 AcpiGbl_FixedEventHandlers[Event].Context = NULL;
812 }
813 else
814 {
815 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
816 "Enabled fixed event %s (%X), Handler=%p\n",
817 AcpiUtGetEventName (Event), Event, Handler));
818 }
819
820
821 Cleanup:
822 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
823 return_ACPI_STATUS (Status);
824 }
825
826 ACPI_EXPORT_SYMBOL (AcpiInstallFixedEventHandler)
827
828
829 /*******************************************************************************
830 *
831 * FUNCTION: AcpiRemoveFixedEventHandler
832 *
833 * PARAMETERS: Event - Event type to disable.
834 * Handler - Address of the handler
835 *
836 * RETURN: Status
837 *
838 * DESCRIPTION: Disables the event and unregisters the event handler.
839 *
840 ******************************************************************************/
841
842 ACPI_STATUS
843 AcpiRemoveFixedEventHandler (
844 UINT32 Event,
845 ACPI_EVENT_HANDLER Handler)
846 {
847 ACPI_STATUS Status = AE_OK;
848
849
850 ACPI_FUNCTION_TRACE (AcpiRemoveFixedEventHandler);
851
852
853 /* Parameter validation */
854
855 if (Event > ACPI_EVENT_MAX)
856 {
857 return_ACPI_STATUS (AE_BAD_PARAMETER);
858 }
859
860 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
861 if (ACPI_FAILURE (Status))
862 {
863 return_ACPI_STATUS (Status);
864 }
865
866 /* Disable the event before removing the handler */
867
868 Status = AcpiDisableEvent (Event, 0);
869
870 /* Always Remove the handler */
871
872 AcpiGbl_FixedEventHandlers[Event].Handler = NULL;
873 AcpiGbl_FixedEventHandlers[Event].Context = NULL;
874
875 if (ACPI_FAILURE (Status))
876 {
877 ACPI_WARNING ((AE_INFO,
878 "Could not disable fixed event - %s (%u)",
879 AcpiUtGetEventName (Event), Event));
880 }
881 else
882 {
883 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
884 "Disabled fixed event - %s (%X)\n",
885 AcpiUtGetEventName (Event), Event));
886 }
887
888 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
889 return_ACPI_STATUS (Status);
890 }
891
892 ACPI_EXPORT_SYMBOL (AcpiRemoveFixedEventHandler)
893
894
895 /*******************************************************************************
896 *
897 * FUNCTION: AcpiInstallGpeHandler
898 *
899 * PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT
900 * defined GPEs)
901 * GpeNumber - The GPE number within the GPE block
902 * Type - Whether this GPE should be treated as an
903 * edge- or level-triggered interrupt.
904 * Address - Address of the handler
905 * Context - Value passed to the handler on each GPE
906 *
907 * RETURN: Status
908 *
909 * DESCRIPTION: Install a handler for a General Purpose Event.
910 *
911 ******************************************************************************/
912
913 ACPI_STATUS
914 AcpiInstallGpeHandler (
915 ACPI_HANDLE GpeDevice,
916 UINT32 GpeNumber,
917 UINT32 Type,
918 ACPI_GPE_HANDLER Address,
919 void *Context)
920 {
921 ACPI_GPE_EVENT_INFO *GpeEventInfo;
922 ACPI_GPE_HANDLER_INFO *Handler;
923 ACPI_STATUS Status;
924 ACPI_CPU_FLAGS Flags;
925
926
927 ACPI_FUNCTION_TRACE (AcpiInstallGpeHandler);
928
929
930 /* Parameter validation */
931
932 if ((!Address) || (Type & ~ACPI_GPE_XRUPT_TYPE_MASK))
933 {
934 return_ACPI_STATUS (AE_BAD_PARAMETER);
935 }
936
937 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
938 if (ACPI_FAILURE (Status))
939 {
940 return_ACPI_STATUS (Status);
941 }
942
943 /* Allocate and init handler object (before lock) */
944
945 Handler = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_HANDLER_INFO));
946 if (!Handler)
947 {
948 Status = AE_NO_MEMORY;
949 goto UnlockAndExit;
950 }
951
952 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
953
954 /* Ensure that we have a valid GPE number */
955
956 GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);
957 if (!GpeEventInfo)
958 {
959 Status = AE_BAD_PARAMETER;
960 goto FreeAndExit;
961 }
962
963 /* Make sure that there isn't a handler there already */
964
965 if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
966 ACPI_GPE_DISPATCH_HANDLER)
967 {
968 Status = AE_ALREADY_EXISTS;
969 goto FreeAndExit;
970 }
971
972 Handler->Address = Address;
973 Handler->Context = Context;
974 Handler->MethodNode = GpeEventInfo->Dispatch.MethodNode;
975 Handler->OriginalFlags = (UINT8) (GpeEventInfo->Flags &
976 (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK));
977
978 /*
979 * If the GPE is associated with a method, it may have been enabled
980 * automatically during initialization, in which case it has to be
981 * disabled now to avoid spurious execution of the handler.
982 */
983 if (((Handler->OriginalFlags & ACPI_GPE_DISPATCH_METHOD) ||
984 (Handler->OriginalFlags & ACPI_GPE_DISPATCH_NOTIFY)) &&
985 GpeEventInfo->RuntimeCount)
986 {
987 Handler->OriginallyEnabled = TRUE;
988 (void) AcpiEvRemoveGpeReference (GpeEventInfo);
989
990 /* Sanity check of original type against new type */
991
992 if (Type != (UINT32) (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))
993 {
994 ACPI_WARNING ((AE_INFO, "GPE type mismatch (level/edge)"));
995 }
996 }
997
998 /* Install the handler */
999
1000 GpeEventInfo->Dispatch.Handler = Handler;
1001
1002 /* Setup up dispatch flags to indicate handler (vs. method/notify) */
1003
1004 GpeEventInfo->Flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
1005 GpeEventInfo->Flags |= (UINT8) (Type | ACPI_GPE_DISPATCH_HANDLER);
1006
1007 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
1008
1009
1010 UnlockAndExit:
1011 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
1012 return_ACPI_STATUS (Status);
1013
1014 FreeAndExit:
1015 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
1016 ACPI_FREE (Handler);
1017 goto UnlockAndExit;
1018 }
1019
1020 ACPI_EXPORT_SYMBOL (AcpiInstallGpeHandler)
1021
1022
1023 /*******************************************************************************
1024 *
1025 * FUNCTION: AcpiRemoveGpeHandler
1026 *
1027 * PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT
1028 * defined GPEs)
1029 * GpeNumber - The event to remove a handler
1030 * Address - Address of the handler
1031 *
1032 * RETURN: Status
1033 *
1034 * DESCRIPTION: Remove a handler for a General Purpose AcpiEvent.
1035 *
1036 ******************************************************************************/
1037
1038 ACPI_STATUS
1039 AcpiRemoveGpeHandler (
1040 ACPI_HANDLE GpeDevice,
1041 UINT32 GpeNumber,
1042 ACPI_GPE_HANDLER Address)
1043 {
1044 ACPI_GPE_EVENT_INFO *GpeEventInfo;
1045 ACPI_GPE_HANDLER_INFO *Handler;
1046 ACPI_STATUS Status;
1047 ACPI_CPU_FLAGS Flags;
1048
1049
1050 ACPI_FUNCTION_TRACE (AcpiRemoveGpeHandler);
1051
1052
1053 /* Parameter validation */
1054
1055 if (!Address)
1056 {
1057 return_ACPI_STATUS (AE_BAD_PARAMETER);
1058 }
1059
1060 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
1061 if (ACPI_FAILURE (Status))
1062 {
1063 return_ACPI_STATUS (Status);
1064 }
1065
1066 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
1067
1068 /* Ensure that we have a valid GPE number */
1069
1070 GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);
1071 if (!GpeEventInfo)
1072 {
1073 Status = AE_BAD_PARAMETER;
1074 goto UnlockAndExit;
1075 }
1076
1077 /* Make sure that a handler is indeed installed */
1078
1079 if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) !=
1080 ACPI_GPE_DISPATCH_HANDLER)
1081 {
1082 Status = AE_NOT_EXIST;
1083 goto UnlockAndExit;
1084 }
1085
1086 /* Make sure that the installed handler is the same */
1087
1088 if (GpeEventInfo->Dispatch.Handler->Address != Address)
1089 {
1090 Status = AE_BAD_PARAMETER;
1091 goto UnlockAndExit;
1092 }
1093
1094 /* Remove the handler */
1095
1096 Handler = GpeEventInfo->Dispatch.Handler;
1097
1098 /* Restore Method node (if any), set dispatch flags */
1099
1100 GpeEventInfo->Dispatch.MethodNode = Handler->MethodNode;
1101 GpeEventInfo->Flags &=
1102 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
1103 GpeEventInfo->Flags |= Handler->OriginalFlags;
1104
1105 /*
1106 * If the GPE was previously associated with a method and it was
1107 * enabled, it should be enabled at this point to restore the
1108 * post-initialization configuration.
1109 */
1110 if (((Handler->OriginalFlags & ACPI_GPE_DISPATCH_METHOD) ||
1111 (Handler->OriginalFlags & ACPI_GPE_DISPATCH_NOTIFY)) &&
1112 Handler->OriginallyEnabled)
1113 {
1114 (void) AcpiEvAddGpeReference (GpeEventInfo);
1115 }
1116
1117 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
1118 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
1119
1120 /* Make sure all deferred GPE tasks are completed */
1121
1122 AcpiOsWaitEventsComplete ();
1123
1124 /* Now we can free the handler object */
1125
1126 ACPI_FREE (Handler);
1127 return_ACPI_STATUS (Status);
1128
1129 UnlockAndExit:
1130 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
1131 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
1132 return_ACPI_STATUS (Status);
1133 }
1134
1135 ACPI_EXPORT_SYMBOL (AcpiRemoveGpeHandler)
1136
1137
1138 /*******************************************************************************
1139 *
1140 * FUNCTION: AcpiAcquireGlobalLock
1141 *
1142 * PARAMETERS: Timeout - How long the caller is willing to wait
1143 * Handle - Where the handle to the lock is returned
1144 * (if acquired)
1145 *
1146 * RETURN: Status
1147 *
1148 * DESCRIPTION: Acquire the ACPI Global Lock
1149 *
1150 * Note: Allows callers with the same thread ID to acquire the global lock
1151 * multiple times. In other words, externally, the behavior of the global lock
1152 * is identical to an AML mutex. On the first acquire, a new handle is
1153 * returned. On any subsequent calls to acquire by the same thread, the same
1154 * handle is returned.
1155 *
1156 ******************************************************************************/
1157
1158 ACPI_STATUS
1159 AcpiAcquireGlobalLock (
1160 UINT16 Timeout,
1161 UINT32 *Handle)
1162 {
1163 ACPI_STATUS Status;
1164
1165
1166 if (!Handle)
1167 {
1168 return (AE_BAD_PARAMETER);
1169 }
1170
1171 /* Must lock interpreter to prevent race conditions */
1172
1173 AcpiExEnterInterpreter ();
1174
1175 Status = AcpiExAcquireMutexObject (Timeout,
1176 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
1177
1178 if (ACPI_SUCCESS (Status))
1179 {
1180 /* Return the global lock handle (updated in AcpiEvAcquireGlobalLock) */
1181
1182 *Handle = AcpiGbl_GlobalLockHandle;
1183 }
1184
1185 AcpiExExitInterpreter ();
1186 return (Status);
1187 }
1188
1189 ACPI_EXPORT_SYMBOL (AcpiAcquireGlobalLock)
1190
1191
1192 /*******************************************************************************
1193 *
1194 * FUNCTION: AcpiReleaseGlobalLock
1195 *
1196 * PARAMETERS: Handle - Returned from AcpiAcquireGlobalLock
1197 *
1198 * RETURN: Status
1199 *
1200 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
1201 *
1202 ******************************************************************************/
1203
1204 ACPI_STATUS
1205 AcpiReleaseGlobalLock (
1206 UINT32 Handle)
1207 {
1208 ACPI_STATUS Status;
1209
1210
1211 if (!Handle || (Handle != AcpiGbl_GlobalLockHandle))
1212 {
1213 return (AE_NOT_ACQUIRED);
1214 }
1215
1216 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
1217 return (Status);
1218 }
1219
1220 ACPI_EXPORT_SYMBOL (AcpiReleaseGlobalLock)
1221
1222 #endif /* !ACPI_REDUCED_HARDWARE */