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
;
161 DPRINT("PopAddRemoveSysCapsCallback(%p %p)\n",
162 NotificationStructure
, Context
);
164 Notification
= (PDEVICE_INTERFACE_CHANGE_NOTIFICATION
)NotificationStructure
;
165 if (Notification
->Version
!= 1)
166 return STATUS_REVISION_MISMATCH
;
167 if (Notification
->Size
!= sizeof(DEVICE_INTERFACE_CHANGE_NOTIFICATION
))
168 return STATUS_INVALID_PARAMETER
;
169 if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_ARRIVAL
, sizeof(GUID
) == sizeof(GUID
)))
171 else if (RtlCompareMemory(&Notification
->Event
, &GUID_DEVICE_INTERFACE_REMOVAL
, sizeof(GUID
) == sizeof(GUID
)))
174 return STATUS_INVALID_PARAMETER
;
178 DPRINT("Arrival of %wZ\n", Notification
->SymbolicLinkName
);
180 /* Open the device */
181 InitializeObjectAttributes(
183 Notification
->SymbolicLinkName
,
192 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
194 if (!NT_SUCCESS(Status
))
196 DPRINT("ZwOpenFile() failed with status 0x%08lx\n", Status
);
199 Status
= ObReferenceObjectByHandle(
206 if (!NT_SUCCESS(Status
))
208 DPRINT("ObReferenceObjectByHandle() failed with status 0x%08lx\n", Status
);
212 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
213 ObDereferenceObject(FileObject
);
215 /* Get capabilities (IOCTL_GET_SYS_BUTTON_CAPS) */
216 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
217 Irp
= IoBuildDeviceIoControlRequest(
218 IOCTL_GET_SYS_BUTTON_CAPS
,
229 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
231 return STATUS_INSUFFICIENT_RESOURCES
;
233 Status
= IoCallDriver(DeviceObject
, Irp
);
234 if (Status
== STATUS_PENDING
)
236 DPRINT("IOCTL_GET_SYS_BUTTON_CAPS pending\n");
237 KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
238 Status
= IoStatusBlock
.Status
;
240 if (!NT_SUCCESS(Status
))
242 DPRINT("Sending IOCTL_GET_SYS_BUTTON_CAPS failed with status 0x%08x\n", Status
);
244 return STATUS_INSUFFICIENT_RESOURCES
;
247 /* FIXME: What do do with the capabilities? */
249 DPRINT1("Device capabilities: 0x%x (", Caps
);
250 if (Caps
& SYS_BUTTON_POWER
) DbgPrint(" POWER");
251 if (Caps
& SYS_BUTTON_SLEEP
) DbgPrint(" SLEEP");
252 if (Caps
& SYS_BUTTON_LID
) DbgPrint(" LID");
256 SysButtonContext
= ExAllocatePool(NonPagedPool
, sizeof(SYS_BUTTON_CONTEXT
));
257 if (!SysButtonContext
)
259 DPRINT("ExAllocatePool() 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 DPRINT("IoAllocateWorkItem() failed\n");
271 ExFreePool(SysButtonContext
);
272 return STATUS_INSUFFICIENT_RESOURCES
;
275 SysButtonContext
->WorkItem
,
281 return STATUS_SUCCESS
;
285 DPRINT1("Removal of a power capable device not implemented\n");
286 return STATUS_NOT_IMPLEMENTED
;