2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/po/events.c
5 * PURPOSE: Power Manager
6 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* GLOBALS *******************************************************************/
17 typedef struct _SYS_BUTTON_CONTEXT
19 PDEVICE_OBJECT DeviceObject
;
20 PIO_WORKITEM WorkItem
;
22 IO_STATUS_BLOCK IoStatusBlock
;
24 } SYS_BUTTON_CONTEXT
, *PSYS_BUTTON_CONTEXT
;
29 IN PDEVICE_OBJECT DeviceObject
,
32 PKWIN32_POWEREVENT_CALLOUT PopEventCallout
;
33 extern PCALLBACK_OBJECT SetSystemTimeCallback
;
35 /* FUNCTIONS *****************************************************************/
39 PoNotifySystemTimeSet(VOID
)
43 /* Check if Win32k registered a notification callback */
46 /* Raise to dispatch */
47 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
49 /* Notify the callback */
50 ExNotifyCallback(SetSystemTimeCallback
, NULL
, NULL
);
59 PopGetSysButtonCompletion(
60 IN PDEVICE_OBJECT DeviceObject
,
64 PSYS_BUTTON_CONTEXT SysButtonContext
= Context
;
67 /* The DeviceObject can be NULL, so use the one we stored */
68 DeviceObject
= SysButtonContext
->DeviceObject
;
70 /* FIXME: What do do with the sys button event? */
71 SysButton
= *(PULONG
)Irp
->AssociatedIrp
.SystemBuffer
;
73 DPRINT1("A device reported the event 0x%x (", SysButton
);
74 if (SysButton
& SYS_BUTTON_POWER
) DbgPrint(" POWER");
75 if (SysButton
& SYS_BUTTON_SLEEP
) DbgPrint(" SLEEP");
76 if (SysButton
& SYS_BUTTON_LID
) DbgPrint(" LID");
77 if (SysButton
== 0) DbgPrint(" WAKE");
80 if (SysButton
& SYS_BUTTON_POWER
)
82 /* FIXME: Read registry for the action we should perform here */
83 DPRINT1("Initiating shutdown after power button event\n");
85 ZwShutdownSystem(ShutdownNoReboot
);
89 /* Allocate a new workitem to send the next IOCTL_GET_SYS_BUTTON_EVENT */
90 SysButtonContext
->WorkItem
= IoAllocateWorkItem(DeviceObject
);
91 if (!SysButtonContext
->WorkItem
)
93 DPRINT("IoAllocateWorkItem() failed\n");
94 ExFreePoolWithTag(SysButtonContext
, 'IWOP');
95 return STATUS_SUCCESS
;
97 IoQueueWorkItem(SysButtonContext
->WorkItem
,
102 return STATUS_SUCCESS
/* STATUS_CONTINUE_COMPLETION */;
108 IN PDEVICE_OBJECT DeviceObject
,
111 PSYS_BUTTON_CONTEXT SysButtonContext
= Context
;
112 PIO_WORKITEM CurrentWorkItem
= SysButtonContext
->WorkItem
;
115 /* Get button pressed (IOCTL_GET_SYS_BUTTON_EVENT) */
116 KeInitializeEvent(&SysButtonContext
->Event
, NotificationEvent
, FALSE
);
117 Irp
= IoBuildDeviceIoControlRequest(IOCTL_GET_SYS_BUTTON_EVENT
,
121 &SysButtonContext
->SysButton
,
122 sizeof(SysButtonContext
->SysButton
),
124 &SysButtonContext
->Event
,
125 &SysButtonContext
->IoStatusBlock
);
128 IoSetCompletionRoutine(Irp
,
129 PopGetSysButtonCompletion
,
134 IoCallDriver(DeviceObject
, Irp
);
138 DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
139 ExFreePoolWithTag(SysButtonContext
, 'IWOP');
142 IoFreeWorkItem(CurrentWorkItem
);
147 PopAddRemoveSysCapsCallback(IN PVOID NotificationStructure
,
150 PDEVICE_INTERFACE_CHANGE_NOTIFICATION Notification
;
151 PSYS_BUTTON_CONTEXT SysButtonContext
;
152 OBJECT_ATTRIBUTES ObjectAttributes
;
154 PDEVICE_OBJECT DeviceObject
;
155 PFILE_OBJECT FileObject
;
157 IO_STATUS_BLOCK IoStatusBlock
;
163 DPRINT("PopAddRemoveSysCapsCallback(%p %p)\n",
164 NotificationStructure
, Context
);
166 Notification
= (PDEVICE_INTERFACE_CHANGE_NOTIFICATION
)NotificationStructure
;
167 if (Notification
->Version
!= 1)
168 return STATUS_REVISION_MISMATCH
;
169 if (Notification
->Size
!= sizeof(DEVICE_INTERFACE_CHANGE_NOTIFICATION
))
170 return STATUS_INVALID_PARAMETER
;
171 if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_ARRIVAL
, sizeof(GUID
)) == sizeof(GUID
))
173 else if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_REMOVAL
, sizeof(GUID
)) == sizeof(GUID
))
176 return STATUS_INVALID_PARAMETER
;
180 DPRINT("Arrival of %wZ\n", Notification
->SymbolicLinkName
);
182 /* Open the device */
183 InitializeObjectAttributes(&ObjectAttributes
,
184 Notification
->SymbolicLinkName
,
188 Status
= ZwOpenFile(&FileHandle
,
192 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
194 if (!NT_SUCCESS(Status
))
196 DPRINT1("ZwOpenFile() failed with status 0x%08lx\n", Status
);
199 Status
= ObReferenceObjectByHandle(FileHandle
,
205 if (!NT_SUCCESS(Status
))
207 DPRINT1("ObReferenceObjectByHandle() failed with status 0x%08lx\n", Status
);
211 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
212 ObDereferenceObject(FileObject
);
214 /* Get capabilities (IOCTL_GET_SYS_BUTTON_CAPS) */
215 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
216 Irp
= IoBuildDeviceIoControlRequest(IOCTL_GET_SYS_BUTTON_CAPS
,
227 DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
229 return STATUS_INSUFFICIENT_RESOURCES
;
231 Status
= IoCallDriver(DeviceObject
, Irp
);
232 if (Status
== STATUS_PENDING
)
234 DPRINT("IOCTL_GET_SYS_BUTTON_CAPS pending\n");
235 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
236 Status
= IoStatusBlock
.Status
;
238 if (!NT_SUCCESS(Status
))
240 DPRINT1("Sending IOCTL_GET_SYS_BUTTON_CAPS failed with status 0x%08x\n", Status
);
242 return STATUS_INSUFFICIENT_RESOURCES
;
245 /* FIXME: What do do with the capabilities? */
247 DPRINT("Device capabilities: 0x%x (", Caps
);
248 if (Caps
& SYS_BUTTON_POWER
) DPRINT(" POWER");
249 if (Caps
& SYS_BUTTON_SLEEP
) DPRINT(" SLEEP");
250 if (Caps
& SYS_BUTTON_LID
) DPRINT(" LID");
254 SysButtonContext
= ExAllocatePoolWithTag(NonPagedPool
,
255 sizeof(SYS_BUTTON_CONTEXT
),
257 if (!SysButtonContext
)
259 DPRINT1("ExAllocatePoolWithTag() failed\n");
261 return STATUS_INSUFFICIENT_RESOURCES
;
264 /* Queue a work item to get sys button event */
265 SysButtonContext
->WorkItem
= IoAllocateWorkItem(DeviceObject
);
266 SysButtonContext
->DeviceObject
= DeviceObject
;
267 if (!SysButtonContext
->WorkItem
)
269 DPRINT1("IoAllocateWorkItem() failed\n");
271 ExFreePoolWithTag(SysButtonContext
, 'IWOP');
272 return STATUS_INSUFFICIENT_RESOURCES
;
274 IoQueueWorkItem(SysButtonContext
->WorkItem
,
280 return STATUS_SUCCESS
;
284 DPRINT1("Removal of a power capable device not implemented\n");
285 return STATUS_NOT_IMPLEMENTED
;