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
;
166 DPRINT("PopAddRemoveSysCapsCallback(%p %p)\n",
167 NotificationStructure
, Context
);
169 Notification
= (PDEVICE_INTERFACE_CHANGE_NOTIFICATION
)NotificationStructure
;
170 if (Notification
->Version
!= 1)
171 return STATUS_REVISION_MISMATCH
;
172 if (Notification
->Size
!= sizeof(DEVICE_INTERFACE_CHANGE_NOTIFICATION
))
173 return STATUS_INVALID_PARAMETER
;
174 if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_ARRIVAL
, sizeof(GUID
)) == sizeof(GUID
))
176 else if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_REMOVAL
, sizeof(GUID
)) == sizeof(GUID
))
179 return STATUS_INVALID_PARAMETER
;
183 DPRINT("Arrival of %wZ\n", Notification
->SymbolicLinkName
);
185 /* Open the device */
186 InitializeObjectAttributes(
188 Notification
->SymbolicLinkName
,
197 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
199 if (!NT_SUCCESS(Status
))
201 DPRINT1("ZwOpenFile() failed with status 0x%08lx\n", Status
);
204 Status
= ObReferenceObjectByHandle(
211 if (!NT_SUCCESS(Status
))
213 DPRINT1("ObReferenceObjectByHandle() failed with status 0x%08lx\n", Status
);
217 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
218 ObDereferenceObject(FileObject
);
220 /* Get capabilities (IOCTL_GET_SYS_BUTTON_CAPS) */
221 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
222 Irp
= IoBuildDeviceIoControlRequest(
223 IOCTL_GET_SYS_BUTTON_CAPS
,
234 DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
236 return STATUS_INSUFFICIENT_RESOURCES
;
238 Status
= IoCallDriver(DeviceObject
, Irp
);
239 if (Status
== STATUS_PENDING
)
241 DPRINT("IOCTL_GET_SYS_BUTTON_CAPS pending\n");
242 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
243 Status
= IoStatusBlock
.Status
;
245 if (!NT_SUCCESS(Status
))
247 DPRINT1("Sending IOCTL_GET_SYS_BUTTON_CAPS failed with status 0x%08x\n", Status
);
249 return STATUS_INSUFFICIENT_RESOURCES
;
252 /* FIXME: What do do with the capabilities? */
254 DPRINT1("Device capabilities: 0x%x (", Caps
);
255 if (Caps
& SYS_BUTTON_POWER
) DbgPrint(" POWER");
256 if (Caps
& SYS_BUTTON_SLEEP
) DbgPrint(" SLEEP");
257 if (Caps
& SYS_BUTTON_LID
) DbgPrint(" LID");
261 SysButtonContext
= ExAllocatePool(NonPagedPool
, sizeof(SYS_BUTTON_CONTEXT
));
262 if (!SysButtonContext
)
264 DPRINT1("ExAllocatePool() failed\n");
266 return STATUS_INSUFFICIENT_RESOURCES
;
269 /* Queue a work item to get sys button event */
270 SysButtonContext
->WorkItem
= IoAllocateWorkItem(DeviceObject
);
271 SysButtonContext
->DeviceObject
= DeviceObject
;
272 if (!SysButtonContext
->WorkItem
)
274 DPRINT1("IoAllocateWorkItem() failed\n");
276 ExFreePool(SysButtonContext
);
277 return STATUS_INSUFFICIENT_RESOURCES
;
280 SysButtonContext
->WorkItem
,
286 return STATUS_SUCCESS
;
290 DPRINT1("Removal of a power capable device not implemented\n");
291 return STATUS_NOT_IMPLEMENTED
;