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 ExFreePool(SysButtonContext
);
95 return STATUS_SUCCESS
;
98 SysButtonContext
->WorkItem
,
103 return STATUS_SUCCESS
/* STATUS_CONTINUE_COMPLETION */;
109 IN PDEVICE_OBJECT DeviceObject
,
112 PSYS_BUTTON_CONTEXT SysButtonContext
= Context
;
113 PIO_WORKITEM CurrentWorkItem
= SysButtonContext
->WorkItem
;
116 /* Get button pressed (IOCTL_GET_SYS_BUTTON_EVENT) */
117 KeInitializeEvent(&SysButtonContext
->Event
, NotificationEvent
, FALSE
);
118 Irp
= IoBuildDeviceIoControlRequest(
119 IOCTL_GET_SYS_BUTTON_EVENT
,
123 &SysButtonContext
->SysButton
,
124 sizeof(SysButtonContext
->SysButton
),
126 &SysButtonContext
->Event
,
127 &SysButtonContext
->IoStatusBlock
);
130 IoSetCompletionRoutine(
132 PopGetSysButtonCompletion
,
137 IoCallDriver(DeviceObject
, Irp
);
141 DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
142 ExFreePool(SysButtonContext
);
145 IoFreeWorkItem(CurrentWorkItem
);
150 PopAddRemoveSysCapsCallback(IN PVOID NotificationStructure
,
153 PDEVICE_INTERFACE_CHANGE_NOTIFICATION Notification
;
154 PSYS_BUTTON_CONTEXT SysButtonContext
;
155 OBJECT_ATTRIBUTES ObjectAttributes
;
157 PDEVICE_OBJECT DeviceObject
;
158 PFILE_OBJECT FileObject
;
160 IO_STATUS_BLOCK IoStatusBlock
;
165 UNICODE_STRING DeviceName
;
166 UNICODE_STRING DeviceNamePrefix
= RTL_CONSTANT_STRING(L
"\\??\\");
168 DPRINT("PopAddRemoveSysCapsCallback(%p %p)\n",
169 NotificationStructure
, Context
);
171 Notification
= (PDEVICE_INTERFACE_CHANGE_NOTIFICATION
)NotificationStructure
;
172 if (Notification
->Version
!= 1)
173 return STATUS_REVISION_MISMATCH
;
174 if (Notification
->Size
!= sizeof(DEVICE_INTERFACE_CHANGE_NOTIFICATION
))
175 return STATUS_INVALID_PARAMETER
;
176 if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_ARRIVAL
, sizeof(GUID
)) == sizeof(GUID
))
178 else if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_REMOVAL
, sizeof(GUID
)) == sizeof(GUID
))
181 return STATUS_INVALID_PARAMETER
;
185 DPRINT("Arrival of %wZ\n", Notification
->SymbolicLinkName
);
187 DeviceName
.Length
= 0;
188 DeviceName
.MaximumLength
= Notification
->SymbolicLinkName
->MaximumLength
+ DeviceNamePrefix
.MaximumLength
;
189 DeviceName
.Buffer
= ExAllocatePool(PagedPool
, DeviceName
.MaximumLength
);
190 if (!DeviceName
.Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
192 RtlCopyUnicodeString(&DeviceName
, &DeviceNamePrefix
);
193 RtlAppendUnicodeStringToString(&DeviceName
, Notification
->SymbolicLinkName
);
195 DPRINT("Opening handle to %wZ\n", &DeviceName
);
197 /* Open the device */
198 InitializeObjectAttributes(
209 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
211 RtlFreeUnicodeString(&DeviceName
);
212 if (!NT_SUCCESS(Status
))
214 DPRINT1("ZwOpenFile() failed with status 0x%08lx\n", Status
);
217 Status
= ObReferenceObjectByHandle(
224 if (!NT_SUCCESS(Status
))
226 DPRINT1("ObReferenceObjectByHandle() failed with status 0x%08lx\n", Status
);
230 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
231 ObDereferenceObject(FileObject
);
233 /* Get capabilities (IOCTL_GET_SYS_BUTTON_CAPS) */
234 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
235 Irp
= IoBuildDeviceIoControlRequest(
236 IOCTL_GET_SYS_BUTTON_CAPS
,
247 DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
249 return STATUS_INSUFFICIENT_RESOURCES
;
251 Status
= IoCallDriver(DeviceObject
, Irp
);
252 if (Status
== STATUS_PENDING
)
254 DPRINT("IOCTL_GET_SYS_BUTTON_CAPS pending\n");
255 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
256 Status
= IoStatusBlock
.Status
;
258 if (!NT_SUCCESS(Status
))
260 DPRINT1("Sending IOCTL_GET_SYS_BUTTON_CAPS failed with status 0x%08x\n", Status
);
262 return STATUS_INSUFFICIENT_RESOURCES
;
265 /* FIXME: What do do with the capabilities? */
267 DPRINT("Device capabilities: 0x%x (", Caps
);
268 if (Caps
& SYS_BUTTON_POWER
) DPRINT(" POWER");
269 if (Caps
& SYS_BUTTON_SLEEP
) DPRINT(" SLEEP");
270 if (Caps
& SYS_BUTTON_LID
) DPRINT(" LID");
274 SysButtonContext
= ExAllocatePool(NonPagedPool
, sizeof(SYS_BUTTON_CONTEXT
));
275 if (!SysButtonContext
)
277 DPRINT1("ExAllocatePool() failed\n");
279 return STATUS_INSUFFICIENT_RESOURCES
;
282 /* Queue a work item to get sys button event */
283 SysButtonContext
->WorkItem
= IoAllocateWorkItem(DeviceObject
);
284 SysButtonContext
->DeviceObject
= DeviceObject
;
285 if (!SysButtonContext
->WorkItem
)
287 DPRINT1("IoAllocateWorkItem() failed\n");
289 ExFreePool(SysButtonContext
);
290 return STATUS_INSUFFICIENT_RESOURCES
;
293 SysButtonContext
->WorkItem
,
299 return STATUS_SUCCESS
;
303 DPRINT1("Removal of a power capable device not implemented\n");
304 return STATUS_NOT_IMPLEMENTED
;