* Sync up to trunk head (r64959).
[reactos.git] / ntoskrnl / po / power.c
index 85839a9..cb7d2cc 100644 (file)
@@ -9,7 +9,6 @@
 
 /* INCLUDES ******************************************************************/
 
-#include "initguid.h"
 #include <ntoskrnl.h>
 #define NDEBUG
 #include <debug.h>
@@ -21,6 +20,7 @@ typedef struct _REQUEST_POWER_ITEM
     PREQUEST_POWER_COMPLETE CompletionRoutine;
     POWER_STATE PowerState;
     PVOID Context;
+    PDEVICE_OBJECT TopDeviceObject;
 } REQUEST_POWER_ITEM, *PREQUEST_POWER_ITEM;
 
 typedef struct _POWER_STATE_TRAVERSE_CONTEXT
@@ -56,10 +56,11 @@ PopRequestPowerIrpCompletion(IN PDEVICE_OBJECT DeviceObject,
                                         RequestPowerItem->Context,
                                         &Irp->IoStatus);
 
-    ExFreePool(Context);
-
     IoFreeIrp(Irp);
 
+    ObDereferenceObject(RequestPowerItem->TopDeviceObject);
+    ExFreePool(Context);
+
     return STATUS_MORE_PROCESSING_REQUIRED;
 }
 
@@ -78,11 +79,11 @@ PopSendQuerySystemPowerState(PDEVICE_OBJECT DeviceObject, SYSTEM_POWER_STATE Sys
     PIO_STACK_LOCATION IrpSp;
     PIRP Irp;
     NTSTATUS Status;
-    
+
     KeInitializeEvent(&Event,
                       NotificationEvent,
                       FALSE);
-    
+
     Irp = IoBuildSynchronousFsdRequest(IRP_MJ_POWER,
                                        DeviceObject,
                                        NULL,
@@ -90,13 +91,14 @@ PopSendQuerySystemPowerState(PDEVICE_OBJECT DeviceObject, SYSTEM_POWER_STATE Sys
                                        NULL,
                                        &Event,
                                        &IoStatusBlock);
-    
+    if (!Irp) return STATUS_INSUFFICIENT_RESOURCES;
+
     IrpSp = IoGetNextIrpStackLocation(Irp);
     IrpSp->MinorFunction = IRP_MN_QUERY_POWER;
     IrpSp->Parameters.Power.Type = SystemPowerState;
     IrpSp->Parameters.Power.State.SystemState = SystemState;
     IrpSp->Parameters.Power.ShutdownType = PowerAction;
-    
+
     Status = PoCallDriver(DeviceObject, Irp);
     if (Status == STATUS_PENDING)
     {
@@ -107,7 +109,7 @@ PopSendQuerySystemPowerState(PDEVICE_OBJECT DeviceObject, SYSTEM_POWER_STATE Sys
                               NULL);
         Status = IoStatusBlock.Status;
     }
-    
+
     return Status;
 }
 
@@ -119,11 +121,11 @@ PopSendSetSystemPowerState(PDEVICE_OBJECT DeviceObject, SYSTEM_POWER_STATE Syste
     PIO_STACK_LOCATION IrpSp;
     PIRP Irp;
     NTSTATUS Status;
-    
+
     KeInitializeEvent(&Event,
                       NotificationEvent,
                       FALSE);
-    
+
     Irp = IoBuildSynchronousFsdRequest(IRP_MJ_POWER,
                                        DeviceObject,
                                        NULL,
@@ -131,13 +133,14 @@ PopSendSetSystemPowerState(PDEVICE_OBJECT DeviceObject, SYSTEM_POWER_STATE Syste
                                        NULL,
                                        &Event,
                                        &IoStatusBlock);
-    
+    if (!Irp) return STATUS_INSUFFICIENT_RESOURCES;
+
     IrpSp = IoGetNextIrpStackLocation(Irp);
     IrpSp->MinorFunction = IRP_MN_SET_POWER;
     IrpSp->Parameters.Power.Type = SystemPowerState;
     IrpSp->Parameters.Power.State.SystemState = SystemState;
     IrpSp->Parameters.Power.ShutdownType = PowerAction;
-    
+
     Status = PoCallDriver(DeviceObject, Irp);
     if (Status == STATUS_PENDING)
     {
@@ -148,7 +151,7 @@ PopSendSetSystemPowerState(PDEVICE_OBJECT DeviceObject, SYSTEM_POWER_STATE Syste
                               NULL);
         Status = IoStatusBlock.Status;
     }
-    
+
     return Status;
 }
 
@@ -226,7 +229,7 @@ PopSetSystemPowerState(SYSTEM_POWER_STATE PowerState, POWER_ACTION PowerAction)
     POWER_STATE_TRAVERSE_CONTEXT PowerContext;
     
     Status = IopGetSystemPowerDeviceObject(&DeviceObject);
-    if (!NT_SUCCESS(Status)) 
+    if (!NT_SUCCESS(Status))
     {
         DPRINT1("No system power driver available\n");
         Fdo = NULL;
@@ -340,6 +343,10 @@ PoInitSystem(IN ULONG BootPhase)
     
     /* Initialize support for dope */
     KeInitializeSpinLock(&PopDopeGlobalLock);
+
+    /* Initialize support for shutdown waits and work-items */
+    PopInitShutdownList();
+
     return TRUE;
 }
 
@@ -441,17 +448,6 @@ PoShutdownBugCheck(IN BOOLEAN LogError,
                  BugCheckParameter4);
 }
 
-/*
- * @unimplemented
- */
-NTSTATUS
-NTAPI
-PoRequestShutdownEvent(OUT PVOID *Event)
-{
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
-}
-
 /*
  * @unimplemented
  */
@@ -547,6 +543,7 @@ PoRequestPowerIrp(IN PDEVICE_OBJECT DeviceObject,
                                         NULL);
     if (!Irp)
     {
+        ObDereferenceObject(TopDeviceObject);
         ExFreePool(RequestPowerItem);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
@@ -569,6 +566,7 @@ PoRequestPowerIrp(IN PDEVICE_OBJECT DeviceObject,
     RequestPowerItem->CompletionRoutine = CompletionFunction;
     RequestPowerItem->PowerState = PowerState;
     RequestPowerItem->Context = Context;
+    RequestPowerItem->TopDeviceObject = TopDeviceObject;
   
     if (pIrp != NULL)
         *pIrp = Irp;
@@ -631,19 +629,6 @@ PoUnregisterSystemState(IN PVOID StateHandle)
     UNIMPLEMENTED;
 }
 
-/*
- * @unimplemented
- */
-NTSTATUS
-NTAPI
-PoQueueShutdownWorkItem(IN PWORK_QUEUE_ITEM WorkItem)
-{
-    PAGED_CODE();
-
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
-}
-
 /*
  * @unimplemented
  */
@@ -673,8 +658,8 @@ NtPowerInformation(IN POWER_INFORMATION_LEVEL PowerInformationLevel,
 
     PAGED_CODE();
 
-    DPRINT("NtPowerInformation(PowerInformationLevel 0x%x, InputBuffer 0x%x, "
-           "InputBufferLength 0x%x, OutputBuffer 0x%x, OutputBufferLength 0x%x)\n",
+    DPRINT("NtPowerInformation(PowerInformationLevel 0x%x, InputBuffer 0x%p, "
+           "InputBufferLength 0x%x, OutputBuffer 0x%p, OutputBufferLength 0x%x)\n",
            PowerInformationLevel,
            InputBuffer, InputBufferLength,
            OutputBuffer, OutputBufferLength);
@@ -787,7 +772,7 @@ NtSetThreadExecutionState(IN EXECUTION_STATE esFlags,
     PreviousState = Thread->PowerState | ES_CONTINUOUS;
 
     /* Check if we need to update the power state */
-    if (esFlags & ES_CONTINUOUS) Thread->PowerState = esFlags;
+    if (esFlags & ES_CONTINUOUS) Thread->PowerState = (UCHAR)esFlags;
 
     /* Protect the write back to user mode */
     _SEH2_TRY
@@ -822,12 +807,12 @@ NtSetSystemPowerState(IN POWER_ACTION SystemAction,
         (MinSystemState <= PowerSystemUnspecified) ||
         (SystemAction > PowerActionWarmEject) ||
         (SystemAction < PowerActionReserved) ||
-        (Flags & ~(POWER_ACTION_QUERY_ALLOWED  |  
-                   POWER_ACTION_UI_ALLOWED     | 
-                   POWER_ACTION_OVERRIDE_APPS  | 
-                   POWER_ACTION_LIGHTEST_FIRST | 
-                   POWER_ACTION_LOCK_CONSOLE   | 
-                   POWER_ACTION_DISABLE_WAKES  | 
+        (Flags & ~(POWER_ACTION_QUERY_ALLOWED  |
+                   POWER_ACTION_UI_ALLOWED     |
+                   POWER_ACTION_OVERRIDE_APPS  |
+                   POWER_ACTION_LIGHTEST_FIRST |
+                   POWER_ACTION_LOCK_CONSOLE   |
+                   POWER_ACTION_DISABLE_WAKES  |
                    POWER_ACTION_CRITICAL)))
     {
         DPRINT1("NtSetSystemPowerState: Bad parameters!\n");
@@ -845,7 +830,7 @@ NtSetSystemPowerState(IN POWER_ACTION SystemAction,
         {
             /* Not granted */
             DPRINT1("ERROR: Privilege not held for shutdown\n");
-            //return STATUS_PRIVILEGE_NOT_HELD; HACK!
+            return STATUS_PRIVILEGE_NOT_HELD;
         }
 
         /* Do it as a kernel-mode caller for consistency with system state */
@@ -902,13 +887,13 @@ NtSetSystemPowerState(IN POWER_ACTION SystemAction,
         
 #ifndef NEWCC
         /* Flush dirty cache pages */
-        CcRosFlushDirtyPages(-1, &Dummy);
+        CcRosFlushDirtyPages(-1, &Dummy, FALSE); //HACK: We really should wait here!
 #else
         Dummy = 0;
 #endif
 
         /* Flush all volumes and the registry */
-        DPRINT1("Flushing volumes, cache flushed %d pages\n", Dummy);
+        DPRINT1("Flushing volumes, cache flushed %lu pages\n", Dummy);
         PopFlushVolumes(PopAction.Shutdown);
 
         /* Set IRP for drivers */
@@ -940,7 +925,7 @@ NtSetSystemPowerState(IN POWER_ACTION SystemAction,
         }
         
         /* You should not have made it this far */
-        ASSERT(FALSE && "System is still up and running?!");
+        ASSERTMSG("System is still up and running?!", FALSE);
         break;
     }