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 if (Irp
->PendingReturned
)
68 IoMarkIrpPending(Irp
);
70 /* The DeviceObject can be NULL, so use the one we stored */
71 DeviceObject
= SysButtonContext
->DeviceObject
;
73 /* FIXME: What do do with the sys button event? */
74 SysButton
= *(PULONG
)Irp
->AssociatedIrp
.SystemBuffer
;
76 DPRINT1("A device reported the event 0x%x (", SysButton
);
77 if (SysButton
& SYS_BUTTON_POWER
) DbgPrint(" POWER");
78 if (SysButton
& SYS_BUTTON_SLEEP
) DbgPrint(" SLEEP");
79 if (SysButton
& SYS_BUTTON_LID
) DbgPrint(" LID");
80 if (SysButton
== 0) DbgPrint(" WAKE");
84 /* Allocate a new workitem to send the next IOCTL_GET_SYS_BUTTON_EVENT */
85 SysButtonContext
->WorkItem
= IoAllocateWorkItem(DeviceObject
);
86 if (!SysButtonContext
->WorkItem
)
88 DPRINT("IoAllocateWorkItem() failed\n");
89 ExFreePool(SysButtonContext
);
90 return STATUS_SUCCESS
;
93 SysButtonContext
->WorkItem
,
98 return STATUS_SUCCESS
/* STATUS_CONTINUE_COMPLETION */;
104 IN PDEVICE_OBJECT DeviceObject
,
107 PSYS_BUTTON_CONTEXT SysButtonContext
= Context
;
108 PIO_WORKITEM CurrentWorkItem
= SysButtonContext
->WorkItem
;
111 /* Get button pressed (IOCTL_GET_SYS_BUTTON_EVENT) */
112 KeInitializeEvent(&SysButtonContext
->Event
, NotificationEvent
, FALSE
);
113 Irp
= IoBuildDeviceIoControlRequest(
114 IOCTL_GET_SYS_BUTTON_EVENT
,
118 &SysButtonContext
->SysButton
,
119 sizeof(SysButtonContext
->SysButton
),
121 &SysButtonContext
->Event
,
122 &SysButtonContext
->IoStatusBlock
);
125 IoSetCompletionRoutine(
127 PopGetSysButtonCompletion
,
132 IoCallDriver(DeviceObject
, Irp
);
136 DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
137 ExFreePool(SysButtonContext
);
140 IoFreeWorkItem(CurrentWorkItem
);
145 PopAddRemoveSysCapsCallback(IN PVOID NotificationStructure
,
148 PDEVICE_INTERFACE_CHANGE_NOTIFICATION Notification
;
149 PSYS_BUTTON_CONTEXT SysButtonContext
;
150 OBJECT_ATTRIBUTES ObjectAttributes
;
152 PDEVICE_OBJECT DeviceObject
;
153 PFILE_OBJECT FileObject
;
155 IO_STATUS_BLOCK IoStatusBlock
;
160 UNICODE_STRING DeviceName
;
161 UNICODE_STRING DeviceNamePrefix
= RTL_CONSTANT_STRING(L
"\\??\\");
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 DeviceName
.Length
= 0;
183 DeviceName
.MaximumLength
= Notification
->SymbolicLinkName
->MaximumLength
+ DeviceNamePrefix
.MaximumLength
;
184 DeviceName
.Buffer
= ExAllocatePool(PagedPool
, DeviceName
.MaximumLength
);
185 if (!DeviceName
.Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
187 RtlCopyUnicodeString(&DeviceName
, &DeviceNamePrefix
);
188 RtlAppendUnicodeStringToString(&DeviceName
, Notification
->SymbolicLinkName
);
190 DPRINT("Opening handle to %wZ\n", &DeviceName
);
192 /* Open the device */
193 InitializeObjectAttributes(
204 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
206 if (!NT_SUCCESS(Status
))
208 DPRINT("ZwOpenFile() failed with status 0x%08lx\n", Status
);
211 Status
= ObReferenceObjectByHandle(
218 if (!NT_SUCCESS(Status
))
220 DPRINT("ObReferenceObjectByHandle() failed with status 0x%08lx\n", Status
);
224 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
225 ObDereferenceObject(FileObject
);
227 /* Get capabilities (IOCTL_GET_SYS_BUTTON_CAPS) */
228 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
229 Irp
= IoBuildDeviceIoControlRequest(
230 IOCTL_GET_SYS_BUTTON_CAPS
,
241 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
243 return STATUS_INSUFFICIENT_RESOURCES
;
245 Status
= IoCallDriver(DeviceObject
, Irp
);
246 if (Status
== STATUS_PENDING
)
248 DPRINT("IOCTL_GET_SYS_BUTTON_CAPS pending\n");
249 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
250 Status
= IoStatusBlock
.Status
;
252 if (!NT_SUCCESS(Status
))
254 DPRINT("Sending IOCTL_GET_SYS_BUTTON_CAPS failed with status 0x%08x\n", Status
);
256 return STATUS_INSUFFICIENT_RESOURCES
;
259 /* FIXME: What do do with the capabilities? */
261 DPRINT1("Device capabilities: 0x%x (", Caps
);
262 if (Caps
& SYS_BUTTON_POWER
) DbgPrint(" POWER");
263 if (Caps
& SYS_BUTTON_SLEEP
) DbgPrint(" SLEEP");
264 if (Caps
& SYS_BUTTON_LID
) DbgPrint(" LID");
268 SysButtonContext
= ExAllocatePool(NonPagedPool
, sizeof(SYS_BUTTON_CONTEXT
));
269 if (!SysButtonContext
)
271 DPRINT("ExAllocatePool() failed\n");
273 return STATUS_INSUFFICIENT_RESOURCES
;
276 /* Queue a work item to get sys button event */
277 SysButtonContext
->WorkItem
= IoAllocateWorkItem(DeviceObject
);
278 SysButtonContext
->DeviceObject
= DeviceObject
;
279 if (!SysButtonContext
->WorkItem
)
281 DPRINT("IoAllocateWorkItem() failed\n");
283 ExFreePool(SysButtonContext
);
284 return STATUS_INSUFFICIENT_RESOURCES
;
287 SysButtonContext
->WorkItem
,
293 return STATUS_SUCCESS
;
297 DPRINT1("Removal of a power capable device not implemented\n");
298 return STATUS_NOT_IMPLEMENTED
;