1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * COPYRIGHT:       See COPYING in the top level directory
3*c2c66affSColin Finck  * PROJECT:         ReactOS Kernel Streaming
4*c2c66affSColin Finck  * FILE:            drivers/wdm/audio/backpln/portcls/power.cpp
5*c2c66affSColin Finck  * PURPOSE:         Power support functions
6*c2c66affSColin Finck  * PROGRAMMER:      Johannes Anderwald
7*c2c66affSColin Finck  */
8*c2c66affSColin Finck 
9*c2c66affSColin Finck #include "private.hpp"
10*c2c66affSColin Finck 
11*c2c66affSColin Finck #define NDEBUG
12*c2c66affSColin Finck #include <debug.h>
13*c2c66affSColin Finck 
14*c2c66affSColin Finck NTSTATUS
15*c2c66affSColin Finck NTAPI
PcRegisterAdapterPowerManagement(IN PUNKNOWN pUnknown,IN PVOID pvContext)16*c2c66affSColin Finck PcRegisterAdapterPowerManagement(
17*c2c66affSColin Finck     IN  PUNKNOWN pUnknown,
18*c2c66affSColin Finck     IN  PVOID pvContext)
19*c2c66affSColin Finck {
20*c2c66affSColin Finck     NTSTATUS Status;
21*c2c66affSColin Finck     PDEVICE_OBJECT pDeviceObject;
22*c2c66affSColin Finck     PPCLASS_DEVICE_EXTENSION DeviceExt;
23*c2c66affSColin Finck     IAdapterPowerManagement * pPower;
24*c2c66affSColin Finck 
25*c2c66affSColin Finck     DPRINT("PcRegisterAdapterPowerManagement pUnknown %p pvContext %p\n", pUnknown, pvContext);
26*c2c66affSColin Finck     PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
27*c2c66affSColin Finck 
28*c2c66affSColin Finck     if (!pUnknown || !pvContext)
29*c2c66affSColin Finck         return STATUS_INVALID_PARAMETER;
30*c2c66affSColin Finck 
31*c2c66affSColin Finck     pDeviceObject = (PDEVICE_OBJECT)pvContext;
32*c2c66affSColin Finck     DeviceExt = (PPCLASS_DEVICE_EXTENSION)pDeviceObject->DeviceExtension;
33*c2c66affSColin Finck 
34*c2c66affSColin Finck     Status = pUnknown->QueryInterface(IID_IAdapterPowerManagement, (PVOID*)&pPower);
35*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
36*c2c66affSColin Finck     {
37*c2c66affSColin Finck         DPRINT1("PcRegisterAdapterPowerManagement no IAdapterPowerManagement interface %x\n", Status);
38*c2c66affSColin Finck         DeviceExt->AdapterPowerManagement = NULL;
39*c2c66affSColin Finck         return STATUS_SUCCESS;
40*c2c66affSColin Finck     }
41*c2c66affSColin Finck 
42*c2c66affSColin Finck     DeviceExt->AdapterPowerManagement = pPower;
43*c2c66affSColin Finck     DPRINT("PcRegisterAdapterPowerManagement success %x\n", Status);
44*c2c66affSColin Finck     return STATUS_SUCCESS;
45*c2c66affSColin Finck }
46*c2c66affSColin Finck 
47*c2c66affSColin Finck NTSTATUS
48*c2c66affSColin Finck NTAPI
PcUnregisterAdapterPowerManagement(IN PDEVICE_OBJECT DeviceObject)49*c2c66affSColin Finck PcUnregisterAdapterPowerManagement(
50*c2c66affSColin Finck     IN PDEVICE_OBJECT DeviceObject)
51*c2c66affSColin Finck {
52*c2c66affSColin Finck     PPCLASS_DEVICE_EXTENSION DeviceExt;
53*c2c66affSColin Finck 
54*c2c66affSColin Finck     DPRINT("PcUnregisterAdapterPowerManagement pUnknown %p pvContext %p\n", DeviceObject);
55*c2c66affSColin Finck     PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
56*c2c66affSColin Finck 
57*c2c66affSColin Finck     if (!DeviceObject)
58*c2c66affSColin Finck         return STATUS_INVALID_PARAMETER;
59*c2c66affSColin Finck 
60*c2c66affSColin Finck     DeviceExt = (PPCLASS_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
61*c2c66affSColin Finck 
62*c2c66affSColin Finck     if (DeviceExt->AdapterPowerManagement)
63*c2c66affSColin Finck     {
64*c2c66affSColin Finck         DeviceExt->AdapterPowerManagement->Release();
65*c2c66affSColin Finck     }
66*c2c66affSColin Finck     DeviceExt->AdapterPowerManagement = NULL;
67*c2c66affSColin Finck     return STATUS_SUCCESS;
68*c2c66affSColin Finck }
69*c2c66affSColin Finck 
70*c2c66affSColin Finck static
71*c2c66affSColin Finck VOID
72*c2c66affSColin Finck NTAPI
PwrCompletionCallback(IN PDEVICE_OBJECT DeviceObject,IN UCHAR MinorFunction,IN POWER_STATE PowerState,IN PVOID Context,IN PIO_STATUS_BLOCK IoStatus)73*c2c66affSColin Finck PwrCompletionCallback(
74*c2c66affSColin Finck     IN PDEVICE_OBJECT DeviceObject,
75*c2c66affSColin Finck     IN UCHAR MinorFunction,
76*c2c66affSColin Finck     IN POWER_STATE PowerState,
77*c2c66affSColin Finck     IN PVOID Context,
78*c2c66affSColin Finck     IN PIO_STATUS_BLOCK IoStatus)
79*c2c66affSColin Finck {
80*c2c66affSColin Finck     KeSetEvent((PRKEVENT)Context, IO_NO_INCREMENT, FALSE);
81*c2c66affSColin Finck }
82*c2c66affSColin Finck 
83*c2c66affSColin Finck NTSTATUS
84*c2c66affSColin Finck NTAPI
PcRequestNewPowerState(IN PDEVICE_OBJECT DeviceObject,IN DEVICE_POWER_STATE RequestedNewState)85*c2c66affSColin Finck PcRequestNewPowerState(
86*c2c66affSColin Finck     IN  PDEVICE_OBJECT DeviceObject,
87*c2c66affSColin Finck     IN  DEVICE_POWER_STATE RequestedNewState)
88*c2c66affSColin Finck {
89*c2c66affSColin Finck     KEVENT Event;
90*c2c66affSColin Finck     NTSTATUS Status;
91*c2c66affSColin Finck     POWER_STATE PowerState;
92*c2c66affSColin Finck     PPCLASS_DEVICE_EXTENSION DeviceExt;
93*c2c66affSColin Finck 
94*c2c66affSColin Finck     PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
95*c2c66affSColin Finck 
96*c2c66affSColin Finck     if (!DeviceObject || !RequestedNewState)
97*c2c66affSColin Finck         return STATUS_INVALID_PARAMETER;
98*c2c66affSColin Finck 
99*c2c66affSColin Finck     DeviceExt = (PPCLASS_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
100*c2c66affSColin Finck     KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
101*c2c66affSColin Finck 
102*c2c66affSColin Finck     PowerState.DeviceState = RequestedNewState;
103*c2c66affSColin Finck     PowerState.SystemState = PowerSystemUnspecified;
104*c2c66affSColin Finck 
105*c2c66affSColin Finck     Status = PoRequestPowerIrp(DeviceExt->PhysicalDeviceObject, IRP_MN_SET_POWER, PowerState, PwrCompletionCallback, (PVOID)&Event, NULL);
106*c2c66affSColin Finck     if (NT_SUCCESS(Status))
107*c2c66affSColin Finck     {
108*c2c66affSColin Finck         KeWaitForSingleObject((PVOID)&Event, Executive, KernelMode, FALSE, NULL);
109*c2c66affSColin Finck     }
110*c2c66affSColin Finck 
111*c2c66affSColin Finck     return Status;
112*c2c66affSColin Finck }
113