2006-09-12 15:19:51 +00:00
|
|
|
|
/*
|
2008-08-18 09:49:28 +00:00
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2006-09-12 15:19:51 +00:00
|
|
|
|
* FILE: ntoskrnl/po/events.c
|
|
|
|
|
* PURPOSE: Power Manager
|
|
|
|
|
* PROGRAMMERS: Herv<EFBFBD> Poussineau (hpoussin@reactos.org)
|
|
|
|
|
*/
|
|
|
|
|
|
2008-08-18 09:49:28 +00:00
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
2006-09-12 15:19:51 +00:00
|
|
|
|
#include <ntoskrnl.h>
|
2007-07-05 14:23:16 +00:00
|
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
|
#include <debug.h>
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2008-08-18 09:49:28 +00:00
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
|
|
|
|
|
typedef struct _SYS_BUTTON_CONTEXT
|
|
|
|
|
{
|
2016-01-07 20:00:05 +00:00
|
|
|
|
PDEVICE_OBJECT DeviceObject;
|
|
|
|
|
PIO_WORKITEM WorkItem;
|
|
|
|
|
KEVENT Event;
|
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
|
ULONG SysButton;
|
2008-08-18 09:49:28 +00:00
|
|
|
|
} SYS_BUTTON_CONTEXT, *PSYS_BUTTON_CONTEXT;
|
2007-05-09 00:29:38 +00:00
|
|
|
|
|
2007-07-05 14:23:16 +00:00
|
|
|
|
static VOID
|
|
|
|
|
NTAPI
|
|
|
|
|
PopGetSysButton(
|
2016-01-07 20:00:05 +00:00
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
|
IN PVOID Context);
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2008-08-18 09:49:28 +00:00
|
|
|
|
PKWIN32_POWEREVENT_CALLOUT PopEventCallout;
|
|
|
|
|
extern PCALLBACK_OBJECT SetSystemTimeCallback;
|
|
|
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
|
2007-05-09 00:29:38 +00:00
|
|
|
|
VOID
|
|
|
|
|
NTAPI
|
|
|
|
|
PoNotifySystemTimeSet(VOID)
|
|
|
|
|
{
|
|
|
|
|
KIRQL OldIrql;
|
|
|
|
|
|
|
|
|
|
/* Check if Win32k registered a notification callback */
|
|
|
|
|
if (PopEventCallout)
|
|
|
|
|
{
|
|
|
|
|
/* Raise to dispatch */
|
|
|
|
|
KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
|
|
|
|
|
|
|
|
|
|
/* Notify the callback */
|
|
|
|
|
ExNotifyCallback(SetSystemTimeCallback, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
/* Lower IRQL back */
|
|
|
|
|
KeLowerIrql(OldIrql);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-05 14:23:16 +00:00
|
|
|
|
static NTSTATUS
|
|
|
|
|
NTAPI
|
|
|
|
|
PopGetSysButtonCompletion(
|
2016-01-07 20:00:05 +00:00
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
|
IN PIRP Irp,
|
|
|
|
|
IN PVOID Context)
|
2007-07-05 14:23:16 +00:00
|
|
|
|
{
|
2016-01-07 20:00:05 +00:00
|
|
|
|
PSYS_BUTTON_CONTEXT SysButtonContext = Context;
|
|
|
|
|
ULONG SysButton;
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* The DeviceObject can be NULL, so use the one we stored */
|
|
|
|
|
DeviceObject = SysButtonContext->DeviceObject;
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* FIXME: What do do with the sys button event? */
|
|
|
|
|
SysButton = *(PULONG)Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("A device reported the event 0x%x (", SysButton);
|
|
|
|
|
if (SysButton & SYS_BUTTON_POWER) DbgPrint(" POWER");
|
|
|
|
|
if (SysButton & SYS_BUTTON_SLEEP) DbgPrint(" SLEEP");
|
|
|
|
|
if (SysButton & SYS_BUTTON_LID) DbgPrint(" LID");
|
|
|
|
|
if (SysButton == 0) DbgPrint(" WAKE");
|
|
|
|
|
DbgPrint(" )\n");
|
2010-08-20 02:27:05 +00:00
|
|
|
|
|
|
|
|
|
if (SysButton & SYS_BUTTON_POWER)
|
|
|
|
|
{
|
|
|
|
|
/* FIXME: Read registry for the action we should perform here */
|
|
|
|
|
DPRINT1("Initiating shutdown after power button event\n");
|
|
|
|
|
|
|
|
|
|
ZwShutdownSystem(ShutdownNoReboot);
|
|
|
|
|
}
|
2016-01-07 20:00:05 +00:00
|
|
|
|
}
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* Allocate a new workitem to send the next IOCTL_GET_SYS_BUTTON_EVENT */
|
|
|
|
|
SysButtonContext->WorkItem = IoAllocateWorkItem(DeviceObject);
|
|
|
|
|
if (!SysButtonContext->WorkItem)
|
|
|
|
|
{
|
|
|
|
|
DPRINT("IoAllocateWorkItem() failed\n");
|
|
|
|
|
ExFreePoolWithTag(SysButtonContext, 'IWOP');
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
IoQueueWorkItem(SysButtonContext->WorkItem,
|
|
|
|
|
PopGetSysButton,
|
|
|
|
|
DelayedWorkQueue,
|
|
|
|
|
SysButtonContext);
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
return STATUS_SUCCESS /* STATUS_CONTINUE_COMPLETION */;
|
2007-07-05 14:23:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
|
NTAPI
|
|
|
|
|
PopGetSysButton(
|
2016-01-07 20:00:05 +00:00
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
|
IN PVOID Context)
|
2007-07-05 14:23:16 +00:00
|
|
|
|
{
|
2016-01-07 20:00:05 +00:00
|
|
|
|
PSYS_BUTTON_CONTEXT SysButtonContext = Context;
|
|
|
|
|
PIO_WORKITEM CurrentWorkItem = SysButtonContext->WorkItem;
|
|
|
|
|
PIRP Irp;
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* Get button pressed (IOCTL_GET_SYS_BUTTON_EVENT) */
|
|
|
|
|
KeInitializeEvent(&SysButtonContext->Event, NotificationEvent, FALSE);
|
|
|
|
|
Irp = IoBuildDeviceIoControlRequest(IOCTL_GET_SYS_BUTTON_EVENT,
|
|
|
|
|
DeviceObject,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
&SysButtonContext->SysButton,
|
|
|
|
|
sizeof(SysButtonContext->SysButton),
|
|
|
|
|
FALSE,
|
|
|
|
|
&SysButtonContext->Event,
|
|
|
|
|
&SysButtonContext->IoStatusBlock);
|
|
|
|
|
if (Irp)
|
|
|
|
|
{
|
|
|
|
|
IoSetCompletionRoutine(Irp,
|
|
|
|
|
PopGetSysButtonCompletion,
|
|
|
|
|
SysButtonContext,
|
|
|
|
|
TRUE,
|
|
|
|
|
FALSE,
|
|
|
|
|
FALSE);
|
|
|
|
|
IoCallDriver(DeviceObject, Irp);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
|
|
|
|
|
ExFreePoolWithTag(SysButtonContext, 'IWOP');
|
|
|
|
|
}
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
IoFreeWorkItem(CurrentWorkItem);
|
2007-07-05 14:23:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-02 18:25:23 +00:00
|
|
|
|
NTSTATUS
|
|
|
|
|
NTAPI
|
2008-08-18 09:49:28 +00:00
|
|
|
|
PopAddRemoveSysCapsCallback(IN PVOID NotificationStructure,
|
|
|
|
|
IN PVOID Context)
|
2006-09-12 15:19:51 +00:00
|
|
|
|
{
|
2016-01-07 20:00:05 +00:00
|
|
|
|
PDEVICE_INTERFACE_CHANGE_NOTIFICATION Notification;
|
|
|
|
|
PSYS_BUTTON_CONTEXT SysButtonContext;
|
|
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
|
|
|
|
HANDLE FileHandle;
|
|
|
|
|
PDEVICE_OBJECT DeviceObject;
|
|
|
|
|
PFILE_OBJECT FileObject;
|
|
|
|
|
PIRP Irp;
|
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
|
KEVENT Event;
|
|
|
|
|
BOOLEAN Arrival;
|
|
|
|
|
ULONG Caps;
|
|
|
|
|
NTSTATUS Status;
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
DPRINT("PopAddRemoveSysCapsCallback(%p %p)\n",
|
|
|
|
|
NotificationStructure, Context);
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
Notification = (PDEVICE_INTERFACE_CHANGE_NOTIFICATION)NotificationStructure;
|
|
|
|
|
if (Notification->Version != 1)
|
|
|
|
|
return STATUS_REVISION_MISMATCH;
|
|
|
|
|
if (Notification->Size != sizeof(DEVICE_INTERFACE_CHANGE_NOTIFICATION))
|
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
if (RtlCompareMemory(&Notification->Event, &GUID_DEVICE_INTERFACE_ARRIVAL, sizeof(GUID)) == sizeof(GUID))
|
|
|
|
|
Arrival = TRUE;
|
|
|
|
|
else if (RtlCompareMemory(&Notification->Event, &GUID_DEVICE_INTERFACE_REMOVAL, sizeof(GUID)) == sizeof(GUID))
|
|
|
|
|
Arrival = FALSE;
|
|
|
|
|
else
|
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
if (Arrival)
|
|
|
|
|
{
|
|
|
|
|
DPRINT("Arrival of %wZ\n", Notification->SymbolicLinkName);
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* Open the device */
|
|
|
|
|
InitializeObjectAttributes(&ObjectAttributes,
|
|
|
|
|
Notification->SymbolicLinkName,
|
|
|
|
|
OBJ_KERNEL_HANDLE,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL);
|
|
|
|
|
Status = ZwOpenFile(&FileHandle,
|
|
|
|
|
FILE_READ_DATA,
|
|
|
|
|
&ObjectAttributes,
|
|
|
|
|
&IoStatusBlock,
|
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
|
0);
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("ZwOpenFile() failed with status 0x%08lx\n", Status);
|
|
|
|
|
return Status;
|
|
|
|
|
}
|
|
|
|
|
Status = ObReferenceObjectByHandle(FileHandle,
|
|
|
|
|
FILE_READ_DATA,
|
|
|
|
|
IoFileObjectType,
|
|
|
|
|
KernelMode,
|
|
|
|
|
(PVOID*)&FileObject,
|
|
|
|
|
NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("ObReferenceObjectByHandle() failed with status 0x%08lx\n", Status);
|
|
|
|
|
ZwClose(FileHandle);
|
|
|
|
|
return Status;
|
|
|
|
|
}
|
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
|
ObDereferenceObject(FileObject);
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* Get capabilities (IOCTL_GET_SYS_BUTTON_CAPS) */
|
|
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
Irp = IoBuildDeviceIoControlRequest(IOCTL_GET_SYS_BUTTON_CAPS,
|
|
|
|
|
DeviceObject,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
&Caps,
|
|
|
|
|
sizeof(Caps),
|
|
|
|
|
FALSE,
|
|
|
|
|
&Event,
|
|
|
|
|
&IoStatusBlock);
|
|
|
|
|
if (!Irp)
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("IoBuildDeviceIoControlRequest() failed\n");
|
|
|
|
|
ZwClose(FileHandle);
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
}
|
|
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
|
{
|
|
|
|
|
DPRINT("IOCTL_GET_SYS_BUTTON_CAPS pending\n");
|
|
|
|
|
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
|
Status = IoStatusBlock.Status;
|
|
|
|
|
}
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("Sending IOCTL_GET_SYS_BUTTON_CAPS failed with status 0x%08x\n", Status);
|
|
|
|
|
ZwClose(FileHandle);
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
}
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2019-04-07 14:41:56 +00:00
|
|
|
|
DPRINT("Device capabilities: 0x%x\n", Caps);
|
|
|
|
|
if (Caps & SYS_BUTTON_POWER)
|
2016-01-07 20:00:05 +00:00
|
|
|
|
{
|
2019-04-07 14:41:56 +00:00
|
|
|
|
DPRINT("POWER button present\n");
|
|
|
|
|
PopCapabilities.PowerButtonPresent = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Caps & SYS_BUTTON_SLEEP)
|
|
|
|
|
{
|
|
|
|
|
DPRINT("SLEEP button present\n");
|
|
|
|
|
PopCapabilities.SleepButtonPresent = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Caps & SYS_BUTTON_LID)
|
|
|
|
|
{
|
|
|
|
|
DPRINT("LID present\n");
|
|
|
|
|
PopCapabilities.LidPresent = TRUE;
|
2016-01-07 20:00:05 +00:00
|
|
|
|
}
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
SysButtonContext = ExAllocatePoolWithTag(NonPagedPool,
|
2015-03-29 06:04:19 +00:00
|
|
|
|
sizeof(SYS_BUTTON_CONTEXT),
|
|
|
|
|
'IWOP');
|
2016-01-07 20:00:05 +00:00
|
|
|
|
if (!SysButtonContext)
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("ExAllocatePoolWithTag() failed\n");
|
|
|
|
|
ZwClose(FileHandle);
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
}
|
2007-07-05 14:23:16 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
/* Queue a work item to get sys button event */
|
|
|
|
|
SysButtonContext->WorkItem = IoAllocateWorkItem(DeviceObject);
|
|
|
|
|
SysButtonContext->DeviceObject = DeviceObject;
|
|
|
|
|
if (!SysButtonContext->WorkItem)
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("IoAllocateWorkItem() failed\n");
|
|
|
|
|
ZwClose(FileHandle);
|
|
|
|
|
ExFreePoolWithTag(SysButtonContext, 'IWOP');
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
}
|
|
|
|
|
IoQueueWorkItem(SysButtonContext->WorkItem,
|
|
|
|
|
PopGetSysButton,
|
|
|
|
|
DelayedWorkQueue,
|
|
|
|
|
SysButtonContext);
|
2006-09-12 15:19:51 +00:00
|
|
|
|
|
2016-01-07 20:00:05 +00:00
|
|
|
|
ZwClose(FileHandle);
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DPRINT1("Removal of a power capable device not implemented\n");
|
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
2006-09-12 15:19:51 +00:00
|
|
|
|
}
|