Skip to content

Commit

Permalink
[NTOS:PNP][UMPNPMGR] GUID_DEVICE_ENUMERATED should be a DeviceInstall…
Browse files Browse the repository at this point in the history
…Event

- Move the GUID_DEVICE_ENUMERATED event from the TargetDeviceChangeEvent category to the DeviceInstallEvent category
- Create a new function that handles DeviceInstallEvent category events
  • Loading branch information
EricKohl committed Dec 3, 2023
1 parent 30b9be0 commit d8ba592
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 36 deletions.
72 changes: 42 additions & 30 deletions base/services/umpnpmgr/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,9 @@ ProcessTargetDeviceEvent(
{
RPC_STATUS RpcStatus;

if (UuidEqual(&PnpEvent->EventGuid, (UUID*)&GUID_DEVICE_ENUMERATED, &RpcStatus))
{
DeviceInstallParams* Params;
DWORD len;
DWORD DeviceIdLength;

DPRINT("Device enumerated: %S\n", PnpEvent->TargetDevice.DeviceIds);
DPRINT("ProcessTargetDeviceEvent(%p)\n", PnpEvent);

DeviceIdLength = lstrlenW(PnpEvent->TargetDevice.DeviceIds);
if (DeviceIdLength)
{
/* Allocate a new device-install event */
len = FIELD_OFFSET(DeviceInstallParams, DeviceIds) + (DeviceIdLength + 1) * sizeof(WCHAR);
Params = HeapAlloc(GetProcessHeap(), 0, len);
if (Params)
{
wcscpy(Params->DeviceIds, PnpEvent->TargetDevice.DeviceIds);

/* Queue the event (will be dequeued by DeviceInstallThread) */
WaitForSingleObject(hDeviceInstallListMutex, INFINITE);
InsertTailList(&DeviceInstallListHead, &Params->ListEntry);
ReleaseMutex(hDeviceInstallListMutex);

SetEvent(hDeviceInstallListNotEmpty);
}
}
}
else if (UuidEqual(&PnpEvent->EventGuid, (UUID*)&GUID_DEVICE_ARRIVAL, &RpcStatus))
if (UuidEqual(&PnpEvent->EventGuid, (UUID*)&GUID_DEVICE_ARRIVAL, &RpcStatus))
{
// DWORD dwRecipient;

Expand Down Expand Up @@ -143,8 +118,8 @@ VOID
ProcessDeviceClassChangeEvent(
_In_ PPLUGPLAY_EVENT_BLOCK PnpEvent)
{
DPRINT("DeviceClassChangeEvent: %S\n", PnpEvent->DeviceClass.SymbolicLinkName);

DPRINT("ProcessDeviceClassChangeEvent(%p)\n", PnpEvent);
DPRINT("SymbolicLink: %S\n", PnpEvent->DeviceClass.SymbolicLinkName);
DPRINT("ClassGuid: {%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n",
PnpEvent->DeviceClass.ClassGuid.Data1, PnpEvent->DeviceClass.ClassGuid.Data2, PnpEvent->DeviceClass.ClassGuid.Data3,
PnpEvent->DeviceClass.ClassGuid.Data4[0], PnpEvent->DeviceClass.ClassGuid.Data4[1], PnpEvent->DeviceClass.ClassGuid.Data4[2],
Expand All @@ -153,6 +128,39 @@ ProcessDeviceClassChangeEvent(
}


static
VOID
ProcessDeviceInstallEvent(
_In_ PPLUGPLAY_EVENT_BLOCK PnpEvent)
{
DeviceInstallParams* Params;
DWORD len;
DWORD DeviceIdLength;

DPRINT("ProcessDeviceInstallEvent(%p)\n", PnpEvent);
DPRINT("Device enumerated: %S\n", PnpEvent->InstallDevice.DeviceId);

DeviceIdLength = lstrlenW(PnpEvent->InstallDevice.DeviceId);
if (DeviceIdLength)
{
/* Allocate a new device-install event */
len = FIELD_OFFSET(DeviceInstallParams, DeviceIds) + (DeviceIdLength + 1) * sizeof(WCHAR);
Params = HeapAlloc(GetProcessHeap(), 0, len);
if (Params)
{
wcscpy(Params->DeviceIds, PnpEvent->InstallDevice.DeviceId);

/* Queue the event (will be dequeued by DeviceInstallThread) */
WaitForSingleObject(hDeviceInstallListMutex, INFINITE);
InsertTailList(&DeviceInstallListHead, &Params->ListEntry);
ReleaseMutex(hDeviceInstallListMutex);

SetEvent(hDeviceInstallListNotEmpty);
}
}
}


DWORD
WINAPI
PnpEventThread(
Expand Down Expand Up @@ -213,7 +221,11 @@ PnpEventThread(
break;

// case CustomDeviceEvent:
// case DeviceInstallEvent:

case DeviceInstallEvent:
ProcessDeviceInstallEvent(PnpEvent);
break;

// case DeviceArrivalEvent:
// case PowerEvent:
// case VetoEvent:
Expand Down
10 changes: 7 additions & 3 deletions ntoskrnl/include/internal/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,13 @@ IopQueueDeviceChangeEvent(

NTSTATUS
IopQueueTargetDeviceEvent(
const GUID *Guid,
PUNICODE_STRING DeviceIds
);
_In_ const GUID *Guid,
_In_ PUNICODE_STRING DeviceIds);

NTSTATUS
IopQueueDeviceInstallEvent(
_In_ const GUID *Guid,
_In_ PUNICODE_STRING DeviceId);

NTSTATUS
NTAPI
Expand Down
8 changes: 6 additions & 2 deletions ntoskrnl/io/pnpmgr/devaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,8 @@ PiInitializeDevNode(
if (!IopDeviceNodeHasFlag(DeviceNode, DNF_LEGACY_DRIVER))
{
/* Report the device to the user-mode pnp manager */
IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED,
&DeviceNode->InstancePath);
IopQueueDeviceInstallEvent(&GUID_DEVICE_ENUMERATED,
&DeviceNode->InstancePath);
}

return STATUS_SUCCESS;
Expand Down Expand Up @@ -2580,6 +2580,10 @@ PipDeviceActionWorker(
case PiActionResetDevice:
// TODO: the operation is a no-op for everything except removed nodes
// for removed nodes, it returns them back to DeviceNodeUninitialized
if (deviceNode->State == DeviceNodeRemoved)
{
deviceNode->State = DeviceNodeUninitialized;
}
status = STATUS_SUCCESS;
break;

Expand Down
43 changes: 42 additions & 1 deletion ntoskrnl/io/pnpmgr/plugplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ IopQueueDeviceChangeEvent(
return STATUS_SUCCESS;
}

NTSTATUS
IopQueueDeviceInstallEvent(
_In_ const GUID *EventGuid,
_In_ PUNICODE_STRING DeviceId)
{
PPNP_EVENT_ENTRY EventEntry;
UNICODE_STRING Copy;
ULONG TotalSize;

/* Allocate a big enough buffer */
Copy.Length = 0;
Copy.MaximumLength = DeviceId->Length + sizeof(UNICODE_NULL);
TotalSize =
FIELD_OFFSET(PLUGPLAY_EVENT_BLOCK, InstallDevice.DeviceId) +
Copy.MaximumLength;

EventEntry = ExAllocatePool(NonPagedPool,
TotalSize + FIELD_OFFSET(PNP_EVENT_ENTRY, Event));
if (!EventEntry)
return STATUS_INSUFFICIENT_RESOURCES;
RtlZeroMemory(EventEntry, TotalSize + FIELD_OFFSET(PNP_EVENT_ENTRY, Event));

/* Fill the buffer with the event GUID */
RtlCopyMemory(&EventEntry->Event.EventGuid, EventGuid, sizeof(GUID));
EventEntry->Event.EventCategory = DeviceInstallEvent;
EventEntry->Event.TotalSize = TotalSize;

/* Fill the symbolic link name */
RtlCopyMemory(&EventEntry->Event.InstallDevice.DeviceId,
DeviceId->Buffer, DeviceId->Length);
EventEntry->Event.InstallDevice.DeviceId[DeviceId->Length / sizeof(WCHAR)] = UNICODE_NULL;

InsertHeadList(&IopPnpEventQueueHead, &EventEntry->ListEntry);

KeSetEvent(&IopPnpNotifyEvent, 0, FALSE);

return STATUS_SUCCESS;
}


NTSTATUS
IopQueueTargetDeviceEvent(const GUID *Guid,
PUNICODE_STRING DeviceIds)
Expand Down Expand Up @@ -331,7 +371,8 @@ PiControlInitializeDevice(
/* Insert as a root enumerated device node */
PiInsertDevNode(DeviceNode, IopRootDeviceNode);

IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED, &DeviceInstance);
/* Report the device to the user-mode pnp manager */
IopQueueDeviceInstallEvent(&GUID_DEVICE_ENUMERATED, &DeviceNode->InstancePath);

ZwClose(InstanceKey);
done:
Expand Down

0 comments on commit d8ba592

Please sign in to comment.