2 * PROJECT: ReactOS Kernel
3 * COPYRIGHT: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/pnpmgr/pnpmgr.c
5 * PURPOSE: Initializes the PnP manager
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Copyright 2007 Hervé Poussineau (hpoussin@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* GLOBALS *******************************************************************/
18 PDEVICE_NODE IopRootDeviceNode
;
19 KSPIN_LOCK IopDeviceTreeLock
;
20 ERESOURCE PpRegistryDeviceResource
;
21 KGUARDED_MUTEX PpDeviceReferenceTableLock
;
22 RTL_AVL_TABLE PpDeviceReferenceTable
;
24 extern ULONG ExpInitializationPhase
;
25 extern BOOLEAN ExpInTextModeSetup
;
26 extern BOOLEAN PnpSystemInit
;
28 /* DATA **********************************************************************/
30 PDRIVER_OBJECT IopRootDriverObject
;
31 PIO_BUS_TYPE_GUID_LIST PnpBusTypeGuidList
= NULL
;
33 typedef struct _INVALIDATE_DEVICE_RELATION_DATA
35 PDEVICE_OBJECT DeviceObject
;
36 DEVICE_RELATION_TYPE Type
;
37 PIO_WORKITEM WorkItem
;
38 } INVALIDATE_DEVICE_RELATION_DATA
, *PINVALIDATE_DEVICE_RELATION_DATA
;
40 /* FUNCTIONS *****************************************************************/
43 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
44 IN ULONG CreateOptions
,
48 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
);
51 IopPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
);
55 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject
)
57 return ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
62 IopInitializeDevice(PDEVICE_NODE DeviceNode
,
63 PDRIVER_OBJECT DriverObject
)
70 /* Special case for bus driven devices */
71 DeviceNode
->Flags
|= DNF_ADDED
;
72 return STATUS_SUCCESS
;
75 if (!DriverObject
->DriverExtension
->AddDevice
)
77 DeviceNode
->Flags
|= DNF_LEGACY_DRIVER
;
80 if (DeviceNode
->Flags
& DNF_LEGACY_DRIVER
)
82 DeviceNode
->Flags
|= DNF_ADDED
+ DNF_STARTED
;
83 return STATUS_SUCCESS
;
86 /* This is a Plug and Play driver */
87 DPRINT("Plug and Play driver found\n");
88 ASSERT(DeviceNode
->PhysicalDeviceObject
);
90 DPRINT("Calling %wZ->AddDevice(%wZ)\n",
91 &DriverObject
->DriverName
,
92 &DeviceNode
->InstancePath
);
93 Status
= DriverObject
->DriverExtension
->AddDevice(
94 DriverObject
, DeviceNode
->PhysicalDeviceObject
);
95 if (!NT_SUCCESS(Status
))
97 DPRINT1("%wZ->AddDevice(%wZ) failed with status 0x%x\n",
98 &DriverObject
->DriverName
,
99 &DeviceNode
->InstancePath
,
101 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
105 /* Check if driver added a FDO above the PDO */
106 Fdo
= IoGetAttachedDeviceReference(DeviceNode
->PhysicalDeviceObject
);
107 if (Fdo
== DeviceNode
->PhysicalDeviceObject
)
109 /* FIXME: What do we do? Unload the driver or just disable the device? */
110 DPRINT1("An FDO was not attached\n");
111 ObDereferenceObject(Fdo
);
112 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
113 return STATUS_UNSUCCESSFUL
;
116 /* Check if we have a ACPI device (needed for power management) */
117 if (Fdo
->DeviceType
== FILE_DEVICE_ACPI
)
119 static BOOLEAN SystemPowerDeviceNodeCreated
= FALSE
;
121 /* There can be only one system power device */
122 if (!SystemPowerDeviceNodeCreated
)
124 PopSystemPowerDeviceNode
= DeviceNode
;
125 ObReferenceObject(PopSystemPowerDeviceNode
);
126 SystemPowerDeviceNodeCreated
= TRUE
;
130 ObDereferenceObject(Fdo
);
132 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
134 return STATUS_SUCCESS
;
140 IopSendEject(IN PDEVICE_OBJECT DeviceObject
)
142 IO_STACK_LOCATION Stack
;
145 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
146 Stack
.MajorFunction
= IRP_MJ_PNP
;
147 Stack
.MinorFunction
= IRP_MN_EJECT
;
149 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
155 IopSendSurpriseRemoval(IN PDEVICE_OBJECT DeviceObject
)
157 IO_STACK_LOCATION Stack
;
160 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
161 Stack
.MajorFunction
= IRP_MJ_PNP
;
162 Stack
.MinorFunction
= IRP_MN_SURPRISE_REMOVAL
;
164 /* Drivers should never fail a IRP_MN_SURPRISE_REMOVAL request */
165 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
171 IopQueryRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
173 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
174 IO_STACK_LOCATION Stack
;
180 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVE_PENDING
,
181 &DeviceNode
->InstancePath
);
183 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
184 Stack
.MajorFunction
= IRP_MJ_PNP
;
185 Stack
.MinorFunction
= IRP_MN_QUERY_REMOVE_DEVICE
;
187 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
189 IopNotifyPlugPlayNotification(DeviceObject
,
190 EventCategoryTargetDeviceChange
,
191 &GUID_TARGET_DEVICE_QUERY_REMOVE
,
195 if (!NT_SUCCESS(Status
))
197 DPRINT1("Removal vetoed by %wZ\n", &DeviceNode
->InstancePath
);
198 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVAL_VETOED
,
199 &DeviceNode
->InstancePath
);
208 IopQueryStopDevice(IN PDEVICE_OBJECT DeviceObject
)
210 IO_STACK_LOCATION Stack
;
213 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
214 Stack
.MajorFunction
= IRP_MJ_PNP
;
215 Stack
.MinorFunction
= IRP_MN_QUERY_STOP_DEVICE
;
217 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
223 IopSendRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
225 IO_STACK_LOCATION Stack
;
228 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
229 Stack
.MajorFunction
= IRP_MJ_PNP
;
230 Stack
.MinorFunction
= IRP_MN_REMOVE_DEVICE
;
232 /* Drivers should never fail a IRP_MN_REMOVE_DEVICE request */
233 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
235 IopNotifyPlugPlayNotification(DeviceObject
,
236 EventCategoryTargetDeviceChange
,
237 &GUID_TARGET_DEVICE_REMOVE_COMPLETE
,
245 IopCancelRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
247 IO_STACK_LOCATION Stack
;
250 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
251 Stack
.MajorFunction
= IRP_MJ_PNP
;
252 Stack
.MinorFunction
= IRP_MN_CANCEL_REMOVE_DEVICE
;
254 /* Drivers should never fail a IRP_MN_CANCEL_REMOVE_DEVICE request */
255 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
257 IopNotifyPlugPlayNotification(DeviceObject
,
258 EventCategoryTargetDeviceChange
,
259 &GUID_TARGET_DEVICE_REMOVE_CANCELLED
,
267 IopSendStopDevice(IN PDEVICE_OBJECT DeviceObject
)
269 IO_STACK_LOCATION Stack
;
272 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
273 Stack
.MajorFunction
= IRP_MJ_PNP
;
274 Stack
.MinorFunction
= IRP_MN_STOP_DEVICE
;
276 /* Drivers should never fail a IRP_MN_STOP_DEVICE request */
277 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
282 IopStartDevice2(IN PDEVICE_OBJECT DeviceObject
)
284 IO_STACK_LOCATION Stack
;
285 PDEVICE_NODE DeviceNode
;
288 DEVICE_CAPABILITIES DeviceCapabilities
;
290 /* Get the device node */
291 DeviceNode
= IopGetDeviceNode(DeviceObject
);
293 ASSERT(!(DeviceNode
->Flags
& DNF_DISABLED
));
295 /* Build the I/O stack locaiton */
296 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
297 Stack
.MajorFunction
= IRP_MJ_PNP
;
298 Stack
.MinorFunction
= IRP_MN_START_DEVICE
;
300 Stack
.Parameters
.StartDevice
.AllocatedResources
=
301 DeviceNode
->ResourceList
;
302 Stack
.Parameters
.StartDevice
.AllocatedResourcesTranslated
=
303 DeviceNode
->ResourceListTranslated
;
306 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
307 if (!NT_SUCCESS(Status
))
309 /* Send an IRP_MN_REMOVE_DEVICE request */
310 IopRemoveDevice(DeviceNode
);
312 /* Set the appropriate flag */
313 DeviceNode
->Flags
|= DNF_START_FAILED
;
315 DPRINT1("Warning: PnP Start failed (%wZ) [Status: 0x%x]\n", &DeviceNode
->InstancePath
, Status
);
319 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after start)\n");
321 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
322 if (!NT_SUCCESS(Status
))
324 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
327 /* Invalidate device state so IRP_MN_QUERY_PNP_DEVICE_STATE is sent */
328 IoInvalidateDeviceState(DeviceObject
);
330 /* Otherwise, mark us as started */
331 DeviceNode
->Flags
|= DNF_STARTED
;
332 DeviceNode
->Flags
&= ~DNF_STOPPED
;
334 /* We now need enumeration */
335 DeviceNode
->Flags
|= DNF_NEED_ENUMERATION_ONLY
;
340 IopStartAndEnumerateDevice(IN PDEVICE_NODE DeviceNode
)
342 PDEVICE_OBJECT DeviceObject
;
347 ASSERT((DeviceNode
->Flags
& DNF_ADDED
));
348 ASSERT((DeviceNode
->Flags
& (DNF_RESOURCE_ASSIGNED
|
349 DNF_RESOURCE_REPORTED
|
350 DNF_NO_RESOURCE_REQUIRED
)));
352 /* Get the device object */
353 DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
355 /* Check if we're not started yet */
356 if (!(DeviceNode
->Flags
& DNF_STARTED
))
359 IopStartDevice2(DeviceObject
);
362 /* Do we need to query IDs? This happens in the case of manual reporting */
364 if (DeviceNode
->Flags
& DNF_NEED_QUERY_IDS
)
366 DPRINT1("Warning: Device node has DNF_NEED_QUERY_IDS\n");
367 /* And that case shouldn't happen yet */
372 /* Make sure we're started, and check if we need enumeration */
373 if ((DeviceNode
->Flags
& DNF_STARTED
) &&
374 (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
))
377 IoSynchronousInvalidateDeviceRelations(DeviceObject
, BusRelations
);
378 Status
= STATUS_SUCCESS
;
383 Status
= STATUS_SUCCESS
;
392 PDEVICE_NODE DeviceNode
)
396 DPRINT("Stopping device: %wZ\n", &DeviceNode
->InstancePath
);
398 Status
= IopQueryStopDevice(DeviceNode
->PhysicalDeviceObject
);
399 if (NT_SUCCESS(Status
))
401 IopSendStopDevice(DeviceNode
->PhysicalDeviceObject
);
403 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
404 DeviceNode
->Flags
|= DNF_STOPPED
;
406 return STATUS_SUCCESS
;
414 PDEVICE_NODE DeviceNode
)
417 HANDLE InstanceHandle
= INVALID_HANDLE_VALUE
, ControlHandle
= INVALID_HANDLE_VALUE
;
418 UNICODE_STRING KeyName
;
419 OBJECT_ATTRIBUTES ObjectAttributes
;
421 if (DeviceNode
->Flags
& DNF_DISABLED
)
422 return STATUS_SUCCESS
;
424 Status
= IopAssignDeviceResources(DeviceNode
);
425 if (!NT_SUCCESS(Status
))
429 IopStartAndEnumerateDevice(DeviceNode
);
431 /* FIX: Should be done in new device instance code */
432 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceHandle
);
433 if (!NT_SUCCESS(Status
))
436 /* FIX: Should be done in IoXxxPrepareDriverLoading */
438 RtlInitUnicodeString(&KeyName
, L
"Control");
439 InitializeObjectAttributes(&ObjectAttributes
,
441 OBJ_CASE_INSENSITIVE
,
444 Status
= ZwCreateKey(&ControlHandle
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
445 if (!NT_SUCCESS(Status
))
448 RtlInitUnicodeString(&KeyName
, L
"ActiveService");
449 Status
= ZwSetValueKey(ControlHandle
, &KeyName
, 0, REG_SZ
, DeviceNode
->ServiceName
.Buffer
, DeviceNode
->ServiceName
.Length
);
453 if (ControlHandle
!= INVALID_HANDLE_VALUE
)
454 ZwClose(ControlHandle
);
456 if (InstanceHandle
!= INVALID_HANDLE_VALUE
)
457 ZwClose(InstanceHandle
);
464 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode
,
465 PDEVICE_CAPABILITIES DeviceCaps
)
467 IO_STATUS_BLOCK StatusBlock
;
468 IO_STACK_LOCATION Stack
;
471 UNICODE_STRING ValueName
;
473 /* Set up the Header */
474 RtlZeroMemory(DeviceCaps
, sizeof(DEVICE_CAPABILITIES
));
475 DeviceCaps
->Size
= sizeof(DEVICE_CAPABILITIES
);
476 DeviceCaps
->Version
= 1;
477 DeviceCaps
->Address
= -1;
478 DeviceCaps
->UINumber
= -1;
480 /* Set up the Stack */
481 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
482 Stack
.Parameters
.DeviceCapabilities
.Capabilities
= DeviceCaps
;
485 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
487 IRP_MN_QUERY_CAPABILITIES
,
489 if (!NT_SUCCESS(Status
))
491 DPRINT1("IRP_MN_QUERY_CAPABILITIES failed with status 0x%x\n", Status
);
495 DeviceNode
->CapabilityFlags
= *(PULONG
)((ULONG_PTR
)&DeviceCaps
->Version
+ sizeof(DeviceCaps
->Version
));
497 if (DeviceCaps
->NoDisplayInUI
)
498 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
500 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
502 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
503 if (NT_SUCCESS(Status
))
505 /* Set 'Capabilities' value */
506 RtlInitUnicodeString(&ValueName
, L
"Capabilities");
507 Status
= ZwSetValueKey(InstanceKey
,
511 (PVOID
)&DeviceNode
->CapabilityFlags
,
514 /* Set 'UINumber' value */
515 if (DeviceCaps
->UINumber
!= MAXULONG
)
517 RtlInitUnicodeString(&ValueName
, L
"UINumber");
518 Status
= ZwSetValueKey(InstanceKey
,
522 &DeviceCaps
->UINumber
,
531 IopAsynchronousInvalidateDeviceRelations(
532 IN PDEVICE_OBJECT DeviceObject
,
533 IN PVOID InvalidateContext
)
535 PINVALIDATE_DEVICE_RELATION_DATA Data
= InvalidateContext
;
537 IoSynchronousInvalidateDeviceRelations(
541 ObDereferenceObject(Data
->DeviceObject
);
542 IoFreeWorkItem(Data
->WorkItem
);
547 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT
*DeviceObject
)
551 if (PopSystemPowerDeviceNode
)
553 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
554 *DeviceObject
= PopSystemPowerDeviceNode
->PhysicalDeviceObject
;
555 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
557 return STATUS_SUCCESS
;
560 return STATUS_UNSUCCESSFUL
;
565 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid
)
567 USHORT i
= 0, FoundIndex
= 0xFFFF;
571 /* Acquire the lock */
572 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
574 /* Loop all entries */
575 while (i
< PnpBusTypeGuidList
->GuidCount
)
577 /* Try to find a match */
578 if (RtlCompareMemory(BusTypeGuid
,
579 &PnpBusTypeGuidList
->Guids
[i
],
580 sizeof(GUID
)) == sizeof(GUID
))
589 /* Check if we have to grow the list */
590 if (PnpBusTypeGuidList
->GuidCount
)
592 /* Calculate the new size */
593 NewSize
= sizeof(IO_BUS_TYPE_GUID_LIST
) +
594 (sizeof(GUID
) * PnpBusTypeGuidList
->GuidCount
);
596 /* Allocate the new copy */
597 NewList
= ExAllocatePool(PagedPool
, NewSize
);
601 ExFreePool(PnpBusTypeGuidList
);
605 /* Now copy them, decrease the size too */
606 NewSize
-= sizeof(GUID
);
607 RtlCopyMemory(NewList
, PnpBusTypeGuidList
, NewSize
);
609 /* Free the old list */
610 ExFreePool(PnpBusTypeGuidList
);
612 /* Use the new buffer */
613 PnpBusTypeGuidList
= NewList
;
616 /* Copy the new GUID */
617 RtlCopyMemory(&PnpBusTypeGuidList
->Guids
[PnpBusTypeGuidList
->GuidCount
],
621 /* The new entry is the index */
622 FoundIndex
= (USHORT
)PnpBusTypeGuidList
->GuidCount
;
623 PnpBusTypeGuidList
->GuidCount
++;
626 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
632 * Creates a device node
635 * ParentNode = Pointer to parent device node
636 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
637 * to have the root device node create one
638 * (eg. for legacy drivers)
639 * DeviceNode = Pointer to storage for created device node
645 IopCreateDeviceNode(PDEVICE_NODE ParentNode
,
646 PDEVICE_OBJECT PhysicalDeviceObject
,
647 PUNICODE_STRING ServiceName
,
648 PDEVICE_NODE
*DeviceNode
)
653 UNICODE_STRING FullServiceName
;
654 UNICODE_STRING LegacyPrefix
= RTL_CONSTANT_STRING(L
"LEGACY_");
655 UNICODE_STRING UnknownDeviceName
= RTL_CONSTANT_STRING(L
"UNKNOWN");
656 UNICODE_STRING KeyName
, ClassName
;
657 PUNICODE_STRING ServiceName1
;
660 UNICODE_STRING ClassGUID
;
662 HANDLE InstanceHandle
;
664 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p ServiceName %wZ\n",
665 ParentNode
, PhysicalDeviceObject
, ServiceName
);
667 Node
= (PDEVICE_NODE
)ExAllocatePool(NonPagedPool
, sizeof(DEVICE_NODE
));
670 return STATUS_INSUFFICIENT_RESOURCES
;
673 RtlZeroMemory(Node
, sizeof(DEVICE_NODE
));
676 ServiceName1
= &UnknownDeviceName
;
678 ServiceName1
= ServiceName
;
680 if (!PhysicalDeviceObject
)
682 FullServiceName
.MaximumLength
= LegacyPrefix
.Length
+ ServiceName1
->Length
;
683 FullServiceName
.Length
= 0;
684 FullServiceName
.Buffer
= ExAllocatePool(PagedPool
, FullServiceName
.MaximumLength
);
685 if (!FullServiceName
.Buffer
)
688 return STATUS_INSUFFICIENT_RESOURCES
;
691 RtlAppendUnicodeStringToString(&FullServiceName
, &LegacyPrefix
);
692 RtlAppendUnicodeStringToString(&FullServiceName
, ServiceName1
);
694 Status
= PnpRootCreateDevice(&FullServiceName
, NULL
, &PhysicalDeviceObject
, &Node
->InstancePath
);
695 if (!NT_SUCCESS(Status
))
697 DPRINT1("PnpRootCreateDevice() failed with status 0x%08X\n", Status
);
702 /* Create the device key for legacy drivers */
703 Status
= IopCreateDeviceKeyPath(&Node
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceHandle
);
704 if (!NT_SUCCESS(Status
))
706 ZwClose(InstanceHandle
);
708 ExFreePool(FullServiceName
.Buffer
);
712 Node
->ServiceName
.Buffer
= ExAllocatePool(PagedPool
, ServiceName1
->Length
);
713 if (!Node
->ServiceName
.Buffer
)
715 ZwClose(InstanceHandle
);
717 ExFreePool(FullServiceName
.Buffer
);
721 Node
->ServiceName
.MaximumLength
= ServiceName1
->Length
;
722 Node
->ServiceName
.Length
= 0;
724 RtlAppendUnicodeStringToString(&Node
->ServiceName
, ServiceName1
);
728 RtlInitUnicodeString(&KeyName
, L
"Service");
729 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ServiceName
->Buffer
, ServiceName
->Length
);
732 if (NT_SUCCESS(Status
))
734 RtlInitUnicodeString(&KeyName
, L
"Legacy");
737 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_DWORD
, &LegacyValue
, sizeof(LegacyValue
));
738 if (NT_SUCCESS(Status
))
740 RtlInitUnicodeString(&KeyName
, L
"Class");
742 RtlInitUnicodeString(&ClassName
, L
"LegacyDriver");
743 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassName
.Buffer
, ClassName
.Length
);
745 if (NT_SUCCESS(Status
))
747 RtlInitUnicodeString(&KeyName
, L
"ClassGUID");
749 RtlInitUnicodeString(&ClassGUID
, L
"{8ECC055D-047F-11D1-A537-0000F8753ED1}");
750 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassGUID
.Buffer
, ClassGUID
.Length
);
756 ZwClose(InstanceHandle
);
757 ExFreePool(FullServiceName
.Buffer
);
759 if (!NT_SUCCESS(Status
))
765 IopDeviceNodeSetFlag(Node
, DNF_LEGACY_DRIVER
);
766 IopDeviceNodeSetFlag(Node
, DNF_PROCESSED
);
767 IopDeviceNodeSetFlag(Node
, DNF_ADDED
);
768 IopDeviceNodeSetFlag(Node
, DNF_STARTED
);
771 Node
->PhysicalDeviceObject
= PhysicalDeviceObject
;
773 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= Node
;
777 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
778 Node
->Parent
= ParentNode
;
779 Node
->Sibling
= ParentNode
->Child
;
780 ParentNode
->Child
= Node
;
781 if (ParentNode
->LastChild
== NULL
)
782 ParentNode
->LastChild
= Node
;
783 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
784 Node
->Level
= ParentNode
->Level
+ 1;
787 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
791 return STATUS_SUCCESS
;
795 IopFreeDeviceNode(PDEVICE_NODE DeviceNode
)
798 PDEVICE_NODE PrevSibling
= NULL
;
800 /* All children must be deleted before a parent is deleted */
801 ASSERT(!DeviceNode
->Child
);
803 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
805 ASSERT(DeviceNode
->PhysicalDeviceObject
);
807 ObDereferenceObject(DeviceNode
->PhysicalDeviceObject
);
809 /* Get previous sibling */
810 if (DeviceNode
->Parent
&& DeviceNode
->Parent
->Child
!= DeviceNode
)
812 PrevSibling
= DeviceNode
->Parent
->Child
;
813 while (PrevSibling
->Sibling
!= DeviceNode
)
814 PrevSibling
= PrevSibling
->Sibling
;
817 /* Unlink from parent if it exists */
818 if (DeviceNode
->Parent
)
820 if (DeviceNode
->Parent
->LastChild
== DeviceNode
)
822 DeviceNode
->Parent
->LastChild
= PrevSibling
;
824 PrevSibling
->Sibling
= NULL
;
826 if (DeviceNode
->Parent
->Child
== DeviceNode
)
827 DeviceNode
->Parent
->Child
= DeviceNode
->Sibling
;
830 /* Unlink from sibling list */
832 PrevSibling
->Sibling
= DeviceNode
->Sibling
;
834 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
836 RtlFreeUnicodeString(&DeviceNode
->InstancePath
);
838 RtlFreeUnicodeString(&DeviceNode
->ServiceName
);
840 if (DeviceNode
->ResourceList
)
842 ExFreePool(DeviceNode
->ResourceList
);
845 if (DeviceNode
->ResourceListTranslated
)
847 ExFreePool(DeviceNode
->ResourceListTranslated
);
850 if (DeviceNode
->ResourceRequirements
)
852 ExFreePool(DeviceNode
->ResourceRequirements
);
855 if (DeviceNode
->BootResources
)
857 ExFreePool(DeviceNode
->BootResources
);
860 ExFreePool(DeviceNode
);
862 return STATUS_SUCCESS
;
867 IopSynchronousCall(IN PDEVICE_OBJECT DeviceObject
,
868 IN PIO_STACK_LOCATION IoStackLocation
,
869 OUT PVOID
*Information
)
872 PIO_STACK_LOCATION IrpStack
;
873 IO_STATUS_BLOCK IoStatusBlock
;
876 PDEVICE_OBJECT TopDeviceObject
;
879 /* Call the top of the device stack */
880 TopDeviceObject
= IoGetAttachedDeviceReference(DeviceObject
);
882 /* Allocate an IRP */
883 Irp
= IoAllocateIrp(TopDeviceObject
->StackSize
, FALSE
);
884 if (!Irp
) return STATUS_INSUFFICIENT_RESOURCES
;
886 /* Initialize to failure */
887 Irp
->IoStatus
.Status
= IoStatusBlock
.Status
= STATUS_NOT_SUPPORTED
;
888 Irp
->IoStatus
.Information
= IoStatusBlock
.Information
= 0;
890 /* Special case for IRP_MN_FILTER_RESOURCE_REQUIREMENTS */
891 if (IoStackLocation
->MinorFunction
== IRP_MN_FILTER_RESOURCE_REQUIREMENTS
)
893 /* Copy the resource requirements list into the IOSB */
894 Irp
->IoStatus
.Information
=
895 IoStatusBlock
.Information
= (ULONG_PTR
)IoStackLocation
->Parameters
.FilterResourceRequirements
.IoResourceRequirementList
;
898 /* Initialize the event */
899 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
902 Irp
->UserIosb
= &IoStatusBlock
;
903 Irp
->UserEvent
= &Event
;
906 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
907 IoQueueThreadIrp(Irp
);
909 /* Copy-in the stack */
910 IrpStack
= IoGetNextIrpStackLocation(Irp
);
911 *IrpStack
= *IoStackLocation
;
913 /* Call the driver */
914 Status
= IoCallDriver(TopDeviceObject
, Irp
);
915 if (Status
== STATUS_PENDING
)
918 KeWaitForSingleObject(&Event
,
923 Status
= IoStatusBlock
.Status
;
926 /* Return the information */
927 *Information
= (PVOID
)IoStatusBlock
.Information
;
933 IopInitiatePnpIrp(IN PDEVICE_OBJECT DeviceObject
,
934 IN OUT PIO_STATUS_BLOCK IoStatusBlock
,
935 IN UCHAR MinorFunction
,
936 IN PIO_STACK_LOCATION Stack OPTIONAL
)
938 IO_STACK_LOCATION IoStackLocation
;
940 /* Fill out the stack information */
941 RtlZeroMemory(&IoStackLocation
, sizeof(IO_STACK_LOCATION
));
942 IoStackLocation
.MajorFunction
= IRP_MJ_PNP
;
943 IoStackLocation
.MinorFunction
= MinorFunction
;
947 RtlCopyMemory(&IoStackLocation
.Parameters
,
949 sizeof(Stack
->Parameters
));
952 /* Do the PnP call */
953 IoStatusBlock
->Status
= IopSynchronousCall(DeviceObject
,
955 (PVOID
)&IoStatusBlock
->Information
);
956 return IoStatusBlock
->Status
;
960 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context
)
962 PDEVICE_NODE ParentDeviceNode
;
963 PDEVICE_NODE ChildDeviceNode
;
966 /* Copy context data so we don't overwrite it in subsequent calls to this function */
967 ParentDeviceNode
= Context
->DeviceNode
;
969 /* Call the action routine */
970 Status
= (Context
->Action
)(ParentDeviceNode
, Context
->Context
);
971 if (!NT_SUCCESS(Status
))
976 /* Traversal of all children nodes */
977 for (ChildDeviceNode
= ParentDeviceNode
->Child
;
978 ChildDeviceNode
!= NULL
;
979 ChildDeviceNode
= ChildDeviceNode
->Sibling
)
981 /* Pass the current device node to the action routine */
982 Context
->DeviceNode
= ChildDeviceNode
;
984 Status
= IopTraverseDeviceTreeNode(Context
);
985 if (!NT_SUCCESS(Status
))
996 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context
)
1000 DPRINT("Context 0x%p\n", Context
);
1002 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
1003 Context
->DeviceNode
, Context
->FirstDeviceNode
, Context
->Action
, Context
->Context
);
1005 /* Start from the specified device node */
1006 Context
->DeviceNode
= Context
->FirstDeviceNode
;
1008 /* Recursively traverse the device tree */
1009 Status
= IopTraverseDeviceTreeNode(Context
);
1010 if (Status
== STATUS_UNSUCCESSFUL
)
1012 /* The action routine just wanted to terminate the traversal with status
1013 code STATUS_SUCCESS */
1014 Status
= STATUS_SUCCESS
;
1022 * IopCreateDeviceKeyPath
1024 * Creates a registry key
1028 * Name of the key to be created.
1030 * Handle to the newly created key
1033 * This method can create nested trees, so parent of RegistryPath can
1034 * be not existant, and will be created if needed.
1038 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
1039 IN ULONG CreateOptions
,
1042 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(ENUM_ROOT
);
1043 HANDLE hParent
= NULL
, hKey
;
1044 OBJECT_ATTRIBUTES ObjectAttributes
;
1045 UNICODE_STRING KeyName
;
1046 LPCWSTR Current
, Last
;
1050 /* Assume failure */
1053 /* Create a volatile device tree in 1st stage so we have a clean slate
1054 * for enumeration using the correct HAL (chosen in 1st stage setup) */
1055 if (ExpInTextModeSetup
) CreateOptions
|= REG_OPTION_VOLATILE
;
1057 /* Open root key for device instances */
1058 Status
= IopOpenRegistryKeyEx(&hParent
, NULL
, &EnumU
, KEY_CREATE_SUB_KEY
);
1059 if (!NT_SUCCESS(Status
))
1061 DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
1065 Current
= KeyName
.Buffer
= RegistryPath
->Buffer
;
1066 Last
= &RegistryPath
->Buffer
[RegistryPath
->Length
/ sizeof(WCHAR
)];
1068 /* Go up to the end of the string */
1069 while (Current
<= Last
)
1071 if (Current
!= Last
&& *Current
!= '\\')
1073 /* Not the end of the string and not a separator */
1078 /* Prepare relative key name */
1079 Length
= (USHORT
)((ULONG_PTR
)Current
- (ULONG_PTR
)KeyName
.Buffer
);
1080 KeyName
.MaximumLength
= KeyName
.Length
= Length
;
1081 DPRINT("Create '%wZ'\n", &KeyName
);
1084 InitializeObjectAttributes(&ObjectAttributes
,
1086 OBJ_CASE_INSENSITIVE
,
1089 Status
= ZwCreateKey(&hKey
,
1090 Current
== Last
? KEY_ALL_ACCESS
: KEY_CREATE_SUB_KEY
,
1097 /* Close parent key handle, we don't need it anymore */
1101 /* Key opening/creating failed? */
1102 if (!NT_SUCCESS(Status
))
1104 DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName
, Status
);
1108 /* Check if it is the end of the string */
1109 if (Current
== Last
)
1111 /* Yes, return success */
1113 return STATUS_SUCCESS
;
1116 /* Start with this new parent key */
1119 KeyName
.Buffer
= (LPWSTR
)Current
;
1122 return STATUS_UNSUCCESSFUL
;
1126 IopSetDeviceInstanceData(HANDLE InstanceKey
,
1127 PDEVICE_NODE DeviceNode
)
1129 OBJECT_ATTRIBUTES ObjectAttributes
;
1130 UNICODE_STRING KeyName
;
1135 HANDLE ControlHandle
;
1137 DPRINT("IopSetDeviceInstanceData() called\n");
1139 /* Create the 'LogConf' key */
1140 RtlInitUnicodeString(&KeyName
, L
"LogConf");
1141 InitializeObjectAttributes(&ObjectAttributes
,
1143 OBJ_CASE_INSENSITIVE
,
1146 Status
= ZwCreateKey(&LogConfKey
,
1151 REG_OPTION_VOLATILE
,
1153 if (NT_SUCCESS(Status
))
1155 /* Set 'BootConfig' value */
1156 if (DeviceNode
->BootResources
!= NULL
)
1158 ResCount
= DeviceNode
->BootResources
->Count
;
1161 RtlInitUnicodeString(&KeyName
, L
"BootConfig");
1162 Status
= ZwSetValueKey(LogConfKey
,
1166 DeviceNode
->BootResources
,
1167 PnpDetermineResourceListSize(DeviceNode
->BootResources
));
1171 /* Set 'BasicConfigVector' value */
1172 if (DeviceNode
->ResourceRequirements
!= NULL
&&
1173 DeviceNode
->ResourceRequirements
->ListSize
!= 0)
1175 RtlInitUnicodeString(&KeyName
, L
"BasicConfigVector");
1176 Status
= ZwSetValueKey(LogConfKey
,
1179 REG_RESOURCE_REQUIREMENTS_LIST
,
1180 DeviceNode
->ResourceRequirements
,
1181 DeviceNode
->ResourceRequirements
->ListSize
);
1184 ZwClose(LogConfKey
);
1187 /* Set the 'ConfigFlags' value */
1188 RtlInitUnicodeString(&KeyName
, L
"ConfigFlags");
1189 Status
= ZwQueryValueKey(InstanceKey
,
1191 KeyValueBasicInformation
,
1195 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1197 /* Write the default value */
1198 ULONG DefaultConfigFlags
= 0;
1199 Status
= ZwSetValueKey(InstanceKey
,
1203 &DefaultConfigFlags
,
1204 sizeof(DefaultConfigFlags
));
1207 /* Create the 'Control' key */
1208 RtlInitUnicodeString(&KeyName
, L
"Control");
1209 InitializeObjectAttributes(&ObjectAttributes
,
1211 OBJ_CASE_INSENSITIVE
,
1214 Status
= ZwCreateKey(&ControlHandle
, 0, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
1216 if (NT_SUCCESS(Status
))
1217 ZwClose(ControlHandle
);
1219 DPRINT("IopSetDeviceInstanceData() done\n");
1225 * IopGetParentIdPrefix
1227 * Retrieve (or create) a string which identifies a device.
1231 * Pointer to device node.
1233 * Pointer to the string where is returned the parent node identifier
1236 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1237 * valid and its Buffer field is NULL-terminated. The caller needs to
1238 * to free the string with RtlFreeUnicodeString when it is no longer
1243 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode
,
1244 PUNICODE_STRING ParentIdPrefix
)
1246 ULONG KeyNameBufferLength
;
1247 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation
= NULL
;
1248 UNICODE_STRING KeyName
;
1249 UNICODE_STRING KeyValue
;
1250 UNICODE_STRING ValueName
;
1255 /* HACK: As long as some devices have a NULL device
1256 * instance path, the following test is required :(
1258 if (DeviceNode
->Parent
->InstancePath
.Length
== 0)
1260 DPRINT1("Parent of %wZ has NULL Instance path, please report!\n",
1261 &DeviceNode
->InstancePath
);
1262 return STATUS_UNSUCCESSFUL
;
1265 /* 1. Try to retrieve ParentIdPrefix from registry */
1266 KeyNameBufferLength
= FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
[0]) + MAX_PATH
* sizeof(WCHAR
);
1267 ParentIdPrefixInformation
= ExAllocatePool(PagedPool
, KeyNameBufferLength
+ sizeof(WCHAR
));
1268 if (!ParentIdPrefixInformation
)
1270 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1275 KeyName
.Buffer
= ExAllocatePool(PagedPool
, (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
);
1276 if (!KeyName
.Buffer
)
1278 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1282 KeyName
.MaximumLength
= (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
;
1284 RtlAppendUnicodeToString(&KeyName
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1285 RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->Parent
->InstancePath
);
1287 Status
= IopOpenRegistryKeyEx(&hKey
, NULL
, &KeyName
, KEY_QUERY_VALUE
| KEY_SET_VALUE
);
1288 if (!NT_SUCCESS(Status
))
1290 RtlInitUnicodeString(&ValueName
, L
"ParentIdPrefix");
1291 Status
= ZwQueryValueKey(
1293 KeyValuePartialInformation
, ParentIdPrefixInformation
,
1294 KeyNameBufferLength
, &KeyNameBufferLength
);
1295 if (NT_SUCCESS(Status
))
1297 if (ParentIdPrefixInformation
->Type
!= REG_SZ
)
1298 Status
= STATUS_UNSUCCESSFUL
;
1301 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1302 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1306 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1308 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1309 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1313 /* 2. Create the ParentIdPrefix value */
1314 crc32
= RtlComputeCrc32(0,
1315 (PUCHAR
)DeviceNode
->Parent
->InstancePath
.Buffer
,
1316 DeviceNode
->Parent
->InstancePath
.Length
);
1318 swprintf((PWSTR
)ParentIdPrefixInformation
->Data
, L
"%lx&%lx", DeviceNode
->Parent
->Level
, crc32
);
1319 RtlInitUnicodeString(&KeyValue
, (PWSTR
)ParentIdPrefixInformation
->Data
);
1321 /* 3. Try to write the ParentIdPrefix to registry */
1322 Status
= ZwSetValueKey(hKey
,
1326 (PVOID
)KeyValue
.Buffer
,
1327 ((ULONG
)wcslen(KeyValue
.Buffer
) + 1) * sizeof(WCHAR
));
1330 if (NT_SUCCESS(Status
))
1332 /* Duplicate the string to return it */
1333 Status
= RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
, &KeyValue
, ParentIdPrefix
);
1335 ExFreePool(ParentIdPrefixInformation
);
1336 RtlFreeUnicodeString(&KeyName
);
1343 IopQueryHardwareIds(PDEVICE_NODE DeviceNode
,
1346 IO_STACK_LOCATION Stack
;
1347 IO_STATUS_BLOCK IoStatusBlock
;
1349 UNICODE_STRING ValueName
;
1351 ULONG Length
, TotalLength
;
1353 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1355 RtlZeroMemory(&Stack
, sizeof(Stack
));
1356 Stack
.Parameters
.QueryId
.IdType
= BusQueryHardwareIDs
;
1357 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1361 if (NT_SUCCESS(Status
))
1364 * FIXME: Check for valid characters, if there is invalid characters
1368 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
1369 DPRINT("Hardware IDs:\n");
1372 DPRINT(" %S\n", Ptr
);
1373 Length
= (ULONG
)wcslen(Ptr
) + 1;
1376 TotalLength
+= Length
;
1378 DPRINT("TotalLength: %hu\n", TotalLength
);
1381 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
1382 Status
= ZwSetValueKey(InstanceKey
,
1386 (PVOID
)IoStatusBlock
.Information
,
1387 (TotalLength
+ 1) * sizeof(WCHAR
));
1388 if (!NT_SUCCESS(Status
))
1390 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
1395 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1402 IopQueryCompatibleIds(PDEVICE_NODE DeviceNode
,
1405 IO_STACK_LOCATION Stack
;
1406 IO_STATUS_BLOCK IoStatusBlock
;
1408 UNICODE_STRING ValueName
;
1410 ULONG Length
, TotalLength
;
1412 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1414 RtlZeroMemory(&Stack
, sizeof(Stack
));
1415 Stack
.Parameters
.QueryId
.IdType
= BusQueryCompatibleIDs
;
1416 Status
= IopInitiatePnpIrp(
1417 DeviceNode
->PhysicalDeviceObject
,
1421 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1424 * FIXME: Check for valid characters, if there is invalid characters
1428 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
1429 DPRINT("Compatible IDs:\n");
1432 DPRINT(" %S\n", Ptr
);
1433 Length
= (ULONG
)wcslen(Ptr
) + 1;
1436 TotalLength
+= Length
;
1438 DPRINT("TotalLength: %hu\n", TotalLength
);
1441 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
1442 Status
= ZwSetValueKey(InstanceKey
,
1446 (PVOID
)IoStatusBlock
.Information
,
1447 (TotalLength
+ 1) * sizeof(WCHAR
));
1448 if (!NT_SUCCESS(Status
))
1450 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status
);
1455 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1463 * IopActionInterrogateDeviceStack
1465 * Retrieve information for all (direct) child nodes of a parent node.
1469 * Pointer to device node.
1471 * Pointer to parent node to retrieve child node information for.
1474 * Any errors that occur are logged instead so that all child services have a chance
1475 * of being interrogated.
1479 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode
,
1482 IO_STATUS_BLOCK IoStatusBlock
;
1483 PDEVICE_NODE ParentDeviceNode
;
1484 WCHAR InstancePath
[MAX_PATH
];
1485 IO_STACK_LOCATION Stack
;
1487 ULONG RequiredLength
;
1489 HANDLE InstanceKey
= NULL
;
1490 UNICODE_STRING ValueName
;
1491 UNICODE_STRING ParentIdPrefix
= { 0, 0, NULL
};
1492 DEVICE_CAPABILITIES DeviceCapabilities
;
1494 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode
, Context
);
1495 DPRINT("PDO 0x%p\n", DeviceNode
->PhysicalDeviceObject
);
1497 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
1500 * We are called for the parent too, but we don't need to do special
1501 * handling for this node
1504 if (DeviceNode
== ParentDeviceNode
)
1506 DPRINT("Success\n");
1507 return STATUS_SUCCESS
;
1511 * Make sure this device node is a direct child of the parent device node
1512 * that is given as an argument
1515 if (DeviceNode
->Parent
!= ParentDeviceNode
)
1517 DPRINT("Skipping 2+ level child\n");
1518 return STATUS_SUCCESS
;
1521 /* Skip processing if it was already completed before */
1522 if (DeviceNode
->Flags
& DNF_PROCESSED
)
1525 return STATUS_SUCCESS
;
1529 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
1530 if (!NT_SUCCESS(Status
))
1532 DPRINT1("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status
);
1537 * FIXME: For critical errors, cleanup and disable device, but always
1538 * return STATUS_SUCCESS.
1541 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1543 Stack
.Parameters
.QueryId
.IdType
= BusQueryDeviceID
;
1544 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1548 if (NT_SUCCESS(Status
))
1550 /* Copy the device id string */
1551 wcscpy(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
1554 * FIXME: Check for valid characters, if there is invalid characters
1560 DPRINT1("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1562 /* We have to return success otherwise we abort the traverse operation */
1563 return STATUS_SUCCESS
;
1566 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after enumeration)\n");
1568 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
1569 if (!NT_SUCCESS(Status
))
1571 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
1573 /* We have to return success otherwise we abort the traverse operation */
1574 return STATUS_SUCCESS
;
1577 /* This bit is only check after enumeration */
1578 if (DeviceCapabilities
.HardwareDisabled
)
1580 /* FIXME: Cleanup device */
1581 DeviceNode
->Flags
|= DNF_DISABLED
;
1582 return STATUS_SUCCESS
;
1585 DeviceNode
->Flags
&= ~DNF_DISABLED
;
1587 if (!DeviceCapabilities
.UniqueID
)
1589 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1590 DPRINT("Instance ID is not unique\n");
1591 Status
= IopGetParentIdPrefix(DeviceNode
, &ParentIdPrefix
);
1592 if (!NT_SUCCESS(Status
))
1594 DPRINT1("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status
);
1596 /* We have to return success otherwise we abort the traverse operation */
1597 return STATUS_SUCCESS
;
1601 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1603 Stack
.Parameters
.QueryId
.IdType
= BusQueryInstanceID
;
1604 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1608 if (NT_SUCCESS(Status
))
1610 /* Append the instance id string */
1611 wcscat(InstancePath
, L
"\\");
1612 if (ParentIdPrefix
.Length
> 0)
1614 /* Add information from parent bus device to InstancePath */
1615 wcscat(InstancePath
, ParentIdPrefix
.Buffer
);
1616 if (IoStatusBlock
.Information
&& *(PWSTR
)IoStatusBlock
.Information
)
1617 wcscat(InstancePath
, L
"&");
1619 if (IoStatusBlock
.Information
)
1620 wcscat(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
1623 * FIXME: Check for valid characters, if there is invalid characters
1629 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1631 RtlFreeUnicodeString(&ParentIdPrefix
);
1633 if (!RtlCreateUnicodeString(&DeviceNode
->InstancePath
, InstancePath
))
1635 DPRINT("No resources\n");
1636 /* FIXME: Cleanup and disable device */
1639 DPRINT("InstancePath is %S\n", DeviceNode
->InstancePath
.Buffer
);
1642 * Create registry key for the instance id, if it doesn't exist yet
1644 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
1645 if (!NT_SUCCESS(Status
))
1647 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status
);
1649 /* We have to return success otherwise we abort the traverse operation */
1650 return STATUS_SUCCESS
;
1653 IopQueryHardwareIds(DeviceNode
, InstanceKey
);
1655 IopQueryCompatibleIds(DeviceNode
, InstanceKey
);
1657 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1659 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextDescription
;
1660 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
1661 Status
= IopInitiatePnpIrp(
1662 DeviceNode
->PhysicalDeviceObject
,
1664 IRP_MN_QUERY_DEVICE_TEXT
,
1666 /* This key is mandatory, so even if the Irp fails, we still write it */
1667 RtlInitUnicodeString(&ValueName
, L
"DeviceDesc");
1668 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
1670 if (NT_SUCCESS(Status
) &&
1671 IoStatusBlock
.Information
&&
1672 (*(PWSTR
)IoStatusBlock
.Information
!= 0))
1674 /* This key is overriden when a driver is installed. Don't write the
1675 * new description if another one already exists */
1676 Status
= ZwSetValueKey(InstanceKey
,
1680 (PVOID
)IoStatusBlock
.Information
,
1681 ((ULONG
)wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
1685 UNICODE_STRING DeviceDesc
= RTL_CONSTANT_STRING(L
"Unknown device");
1686 DPRINT("Driver didn't return DeviceDesc (Status 0x%08lx), so place unknown device there\n", Status
);
1688 Status
= ZwSetValueKey(InstanceKey
,
1693 DeviceDesc
.MaximumLength
);
1695 if (!NT_SUCCESS(Status
))
1697 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status
);
1703 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1705 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextLocationInformation
;
1706 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
1707 Status
= IopInitiatePnpIrp(
1708 DeviceNode
->PhysicalDeviceObject
,
1710 IRP_MN_QUERY_DEVICE_TEXT
,
1712 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1714 DPRINT("LocationInformation: %S\n", (PWSTR
)IoStatusBlock
.Information
);
1715 RtlInitUnicodeString(&ValueName
, L
"LocationInformation");
1716 Status
= ZwSetValueKey(InstanceKey
,
1720 (PVOID
)IoStatusBlock
.Information
,
1721 ((ULONG
)wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
1722 if (!NT_SUCCESS(Status
))
1724 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
1729 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
1732 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1734 Status
= IopInitiatePnpIrp(
1735 DeviceNode
->PhysicalDeviceObject
,
1737 IRP_MN_QUERY_BUS_INFORMATION
,
1739 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1741 PPNP_BUS_INFORMATION BusInformation
=
1742 (PPNP_BUS_INFORMATION
)IoStatusBlock
.Information
;
1744 DeviceNode
->ChildBusNumber
= BusInformation
->BusNumber
;
1745 DeviceNode
->ChildInterfaceType
= BusInformation
->LegacyBusType
;
1746 DeviceNode
->ChildBusTypeIndex
= IopGetBusTypeGuidIndex(&BusInformation
->BusTypeGuid
);
1747 ExFreePool(BusInformation
);
1751 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
1753 DeviceNode
->ChildBusNumber
= 0xFFFFFFF0;
1754 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
1755 DeviceNode
->ChildBusTypeIndex
= -1;
1758 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1760 Status
= IopInitiatePnpIrp(
1761 DeviceNode
->PhysicalDeviceObject
,
1763 IRP_MN_QUERY_RESOURCES
,
1765 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1767 DeviceNode
->BootResources
=
1768 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
1769 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
1773 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
1774 DeviceNode
->BootResources
= NULL
;
1777 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1779 Status
= IopInitiatePnpIrp(
1780 DeviceNode
->PhysicalDeviceObject
,
1782 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
1784 if (NT_SUCCESS(Status
))
1786 DeviceNode
->ResourceRequirements
=
1787 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
1791 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
1792 DeviceNode
->ResourceRequirements
= NULL
;
1795 if (InstanceKey
!= NULL
)
1797 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
1800 ZwClose(InstanceKey
);
1802 IopDeviceNodeSetFlag(DeviceNode
, DNF_PROCESSED
);
1804 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_LEGACY_DRIVER
))
1806 /* Report the device to the user-mode pnp manager */
1807 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
1808 &DeviceNode
->InstancePath
);
1811 return STATUS_SUCCESS
;
1816 IopHandleDeviceRemoval(
1817 IN PDEVICE_NODE DeviceNode
,
1818 IN PDEVICE_RELATIONS DeviceRelations
)
1820 PDEVICE_NODE Child
= DeviceNode
->Child
, NextChild
;
1824 while (Child
!= NULL
)
1826 NextChild
= Child
->Sibling
;
1829 for (i
= 0; DeviceRelations
&& i
< DeviceRelations
->Count
; i
++)
1831 if (IopGetDeviceNode(DeviceRelations
->Objects
[i
]) == Child
)
1840 IopSendSurpriseRemoval(Child
->PhysicalDeviceObject
);
1842 /* Tell the user-mode PnP manager that a device was removed */
1843 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
1844 &Child
->InstancePath
);
1846 IopSendRemoveDevice(Child
->PhysicalDeviceObject
);
1855 IN PDEVICE_OBJECT DeviceObject
)
1857 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
1858 DEVICETREE_TRAVERSE_CONTEXT Context
;
1859 PDEVICE_RELATIONS DeviceRelations
;
1860 PDEVICE_OBJECT ChildDeviceObject
;
1861 IO_STATUS_BLOCK IoStatusBlock
;
1862 PDEVICE_NODE ChildDeviceNode
;
1863 IO_STACK_LOCATION Stack
;
1867 DPRINT("DeviceObject 0x%p\n", DeviceObject
);
1869 if (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
)
1871 DeviceNode
->Flags
&= ~DNF_NEED_ENUMERATION_ONLY
;
1873 DPRINT("Sending GUID_DEVICE_ARRIVAL\n");
1874 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
1875 &DeviceNode
->InstancePath
);
1878 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
1880 Stack
.Parameters
.QueryDeviceRelations
.Type
= BusRelations
;
1882 Status
= IopInitiatePnpIrp(
1885 IRP_MN_QUERY_DEVICE_RELATIONS
,
1887 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1889 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
1893 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
1896 * Send removal IRPs for devices that have disappeared
1897 * NOTE: This code handles the case where no relations are specified
1899 IopHandleDeviceRemoval(DeviceNode
, DeviceRelations
);
1901 /* Now we bail if nothing was returned */
1902 if (!DeviceRelations
)
1904 /* We're all done */
1905 DPRINT("No PDOs\n");
1906 return STATUS_SUCCESS
;
1909 DPRINT("Got %u PDOs\n", DeviceRelations
->Count
);
1912 * Create device nodes for all discovered devices
1914 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
1916 ChildDeviceObject
= DeviceRelations
->Objects
[i
];
1917 ASSERT((ChildDeviceObject
->Flags
& DO_DEVICE_INITIALIZING
) == 0);
1919 ChildDeviceNode
= IopGetDeviceNode(ChildDeviceObject
);
1920 if (!ChildDeviceNode
)
1922 /* One doesn't exist, create it */
1923 Status
= IopCreateDeviceNode(
1928 if (NT_SUCCESS(Status
))
1930 /* Mark the node as enumerated */
1931 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
1933 /* Mark the DO as bus enumerated */
1934 ChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
1938 /* Ignore this DO */
1939 DPRINT1("IopCreateDeviceNode() failed with status 0x%08x. Skipping PDO %u\n", Status
, i
);
1940 ObDereferenceObject(ChildDeviceNode
);
1945 /* Mark it as enumerated */
1946 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
1947 ObDereferenceObject(ChildDeviceObject
);
1950 ExFreePool(DeviceRelations
);
1953 * Retrieve information about all discovered children from the bus driver
1955 IopInitDeviceTreeTraverseContext(
1958 IopActionInterrogateDeviceStack
,
1961 Status
= IopTraverseDeviceTree(&Context
);
1962 if (!NT_SUCCESS(Status
))
1964 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
1969 * Retrieve configuration from the registry for discovered children
1971 IopInitDeviceTreeTraverseContext(
1974 IopActionConfigureChildServices
,
1977 Status
= IopTraverseDeviceTree(&Context
);
1978 if (!NT_SUCCESS(Status
))
1980 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
1985 * Initialize services for discovered children.
1987 Status
= IopInitializePnpServices(DeviceNode
);
1988 if (!NT_SUCCESS(Status
))
1990 DPRINT("IopInitializePnpServices() failed with status 0x%08lx\n", Status
);
1994 DPRINT("IopEnumerateDevice() finished\n");
1995 return STATUS_SUCCESS
;
2000 * IopActionConfigureChildServices
2002 * Retrieve configuration for all (direct) child nodes of a parent node.
2006 * Pointer to device node.
2008 * Pointer to parent node to retrieve child node configuration for.
2011 * Any errors that occur are logged instead so that all child services have a chance of beeing
2016 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode
,
2019 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
2020 PDEVICE_NODE ParentDeviceNode
;
2021 PUNICODE_STRING Service
;
2022 UNICODE_STRING ClassGUID
;
2024 DEVICE_CAPABILITIES DeviceCaps
;
2026 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode
, Context
);
2028 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2031 * We are called for the parent too, but we don't need to do special
2032 * handling for this node
2034 if (DeviceNode
== ParentDeviceNode
)
2036 DPRINT("Success\n");
2037 return STATUS_SUCCESS
;
2041 * Make sure this device node is a direct child of the parent device node
2042 * that is given as an argument
2045 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2047 DPRINT("Skipping 2+ level child\n");
2048 return STATUS_SUCCESS
;
2051 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2053 DPRINT1("Child not ready to be configured\n");
2054 return STATUS_SUCCESS
;
2057 if (!(DeviceNode
->Flags
& (DNF_DISABLED
| DNF_STARTED
| DNF_ADDED
)))
2059 WCHAR RegKeyBuffer
[MAX_PATH
];
2060 UNICODE_STRING RegKey
;
2063 RegKey
.MaximumLength
= sizeof(RegKeyBuffer
);
2064 RegKey
.Buffer
= RegKeyBuffer
;
2067 * Retrieve configuration from Enum key
2070 Service
= &DeviceNode
->ServiceName
;
2072 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
2073 RtlInitUnicodeString(Service
, NULL
);
2074 RtlInitUnicodeString(&ClassGUID
, NULL
);
2076 QueryTable
[0].Name
= L
"Service";
2077 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2078 QueryTable
[0].EntryContext
= Service
;
2080 QueryTable
[1].Name
= L
"ClassGUID";
2081 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2082 QueryTable
[1].EntryContext
= &ClassGUID
;
2083 QueryTable
[1].DefaultType
= REG_SZ
;
2084 QueryTable
[1].DefaultData
= L
"";
2085 QueryTable
[1].DefaultLength
= 0;
2087 RtlAppendUnicodeToString(&RegKey
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2088 RtlAppendUnicodeStringToString(&RegKey
, &DeviceNode
->InstancePath
);
2090 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
2091 RegKey
.Buffer
, QueryTable
, NULL
, NULL
);
2093 if (!NT_SUCCESS(Status
))
2095 /* FIXME: Log the error */
2096 DPRINT("Could not retrieve configuration for device %wZ (Status 0x%08x)\n",
2097 &DeviceNode
->InstancePath
, Status
);
2098 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2099 return STATUS_SUCCESS
;
2102 if (Service
->Buffer
== NULL
)
2104 if (NT_SUCCESS(IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
)) &&
2105 DeviceCaps
.RawDeviceOK
)
2107 DPRINT1("%wZ is using parent bus driver (%wZ)\n", &DeviceNode
->InstancePath
, &ParentDeviceNode
->ServiceName
);
2109 DeviceNode
->ServiceName
.Length
= 0;
2110 DeviceNode
->ServiceName
.MaximumLength
= 0;
2111 DeviceNode
->ServiceName
.Buffer
= NULL
;
2113 else if (ClassGUID
.Length
!= 0)
2115 /* Device has a ClassGUID value, but no Service value.
2116 * Suppose it is using the NULL driver, so state the
2117 * device is started */
2118 DPRINT1("%wZ is using NULL driver\n", &DeviceNode
->InstancePath
);
2119 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
2123 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2125 return STATUS_SUCCESS
;
2128 DPRINT("Got Service %S\n", Service
->Buffer
);
2131 return STATUS_SUCCESS
;
2135 * IopActionInitChildServices
2137 * Initialize the service for all (direct) child nodes of a parent node
2141 * Pointer to device node.
2143 * Pointer to parent node to initialize child node services for.
2146 * If the driver image for a service is not loaded and initialized
2147 * it is done here too. Any errors that occur are logged instead so
2148 * that all child services have a chance of being initialized.
2152 IopActionInitChildServices(PDEVICE_NODE DeviceNode
,
2155 PDEVICE_NODE ParentDeviceNode
;
2157 BOOLEAN BootDrivers
= !PnpSystemInit
;
2159 DPRINT("IopActionInitChildServices(%p, %p)\n", DeviceNode
, Context
);
2161 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2164 * We are called for the parent too, but we don't need to do special
2165 * handling for this node
2167 if (DeviceNode
== ParentDeviceNode
)
2169 DPRINT("Success\n");
2170 return STATUS_SUCCESS
;
2174 * Make sure this device node is a direct child of the parent device node
2175 * that is given as an argument
2178 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2180 DPRINT("Skipping 2+ level child\n");
2181 return STATUS_SUCCESS
;
2184 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2186 DPRINT1("Child not ready to be added\n");
2187 return STATUS_SUCCESS
;
2190 if (IopDeviceNodeHasFlag(DeviceNode
, DNF_STARTED
) ||
2191 IopDeviceNodeHasFlag(DeviceNode
, DNF_ADDED
) ||
2192 IopDeviceNodeHasFlag(DeviceNode
, DNF_DISABLED
))
2193 return STATUS_SUCCESS
;
2195 if (DeviceNode
->ServiceName
.Buffer
== NULL
)
2197 /* We don't need to worry about loading the driver because we're
2198 * being driven in raw mode so our parent must be loaded to get here */
2199 Status
= IopInitializeDevice(DeviceNode
, NULL
);
2200 if (NT_SUCCESS(Status
))
2202 Status
= IopStartDevice(DeviceNode
);
2203 if (!NT_SUCCESS(Status
))
2205 DPRINT1("IopStartDevice(%wZ) failed with status 0x%08x\n",
2206 &DeviceNode
->InstancePath
, Status
);
2212 PLDR_DATA_TABLE_ENTRY ModuleObject
;
2213 PDRIVER_OBJECT DriverObject
;
2215 /* Get existing DriverObject pointer (in case the driver has
2216 already been loaded and initialized) */
2217 Status
= IopGetDriverObject(
2219 &DeviceNode
->ServiceName
,
2222 if (!NT_SUCCESS(Status
))
2224 /* Driver is not initialized, try to load it */
2225 Status
= IopLoadServiceModule(&DeviceNode
->ServiceName
, &ModuleObject
);
2227 if (NT_SUCCESS(Status
) || Status
== STATUS_IMAGE_ALREADY_LOADED
)
2229 /* STATUS_IMAGE_ALREADY_LOADED means this driver
2230 was loaded by the bootloader */
2231 if ((Status
!= STATUS_IMAGE_ALREADY_LOADED
) ||
2232 (Status
== STATUS_IMAGE_ALREADY_LOADED
&& !DriverObject
))
2234 /* Initialize the driver */
2235 Status
= IopInitializeDriverModule(DeviceNode
, ModuleObject
,
2236 &DeviceNode
->ServiceName
, FALSE
, &DriverObject
);
2240 Status
= STATUS_SUCCESS
;
2245 DPRINT1("IopLoadServiceModule(%wZ) failed with status 0x%08x\n",
2246 &DeviceNode
->ServiceName
, Status
);
2250 /* Driver is loaded and initialized at this point */
2251 if (NT_SUCCESS(Status
))
2253 /* Initialize the device, including all filters */
2254 Status
= PipCallDriverAddDevice(DeviceNode
, FALSE
, DriverObject
);
2259 * Don't disable when trying to load only boot drivers
2263 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2264 IopDeviceNodeSetFlag(DeviceNode
, DNF_START_FAILED
);
2265 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2266 DPRINT1("Initialization of service %S failed (Status %x)\n",
2267 DeviceNode
->ServiceName
.Buffer
, Status
);
2272 return STATUS_SUCCESS
;
2276 * IopInitializePnpServices
2278 * Initialize services for discovered children
2282 * Top device node to start initializing services.
2288 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode
)
2290 DEVICETREE_TRAVERSE_CONTEXT Context
;
2292 DPRINT("IopInitializePnpServices(%p)\n", DeviceNode
);
2294 IopInitDeviceTreeTraverseContext(
2297 IopActionInitChildServices
,
2300 return IopTraverseDeviceTree(&Context
);
2303 static NTSTATUS INIT_FUNCTION
2304 IopEnumerateDetectedDevices(
2306 IN PUNICODE_STRING RelativePath OPTIONAL
,
2308 IN BOOLEAN EnumerateSubKeys
,
2309 IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources
,
2310 IN ULONG ParentBootResourcesLength
)
2312 UNICODE_STRING IdentifierU
= RTL_CONSTANT_STRING(L
"Identifier");
2313 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
2314 UNICODE_STRING ConfigurationDataU
= RTL_CONSTANT_STRING(L
"Configuration Data");
2315 UNICODE_STRING BootConfigU
= RTL_CONSTANT_STRING(L
"BootConfig");
2316 UNICODE_STRING LogConfU
= RTL_CONSTANT_STRING(L
"LogConf");
2317 OBJECT_ATTRIBUTES ObjectAttributes
;
2318 HANDLE hDevicesKey
= NULL
;
2319 HANDLE hDeviceKey
= NULL
;
2320 HANDLE hLevel1Key
, hLevel2Key
= NULL
, hLogConf
;
2321 UNICODE_STRING Level2NameU
;
2322 WCHAR Level2Name
[5];
2323 ULONG IndexDevice
= 0;
2325 PKEY_BASIC_INFORMATION pDeviceInformation
= NULL
;
2326 ULONG DeviceInfoLength
= sizeof(KEY_BASIC_INFORMATION
) + 50 * sizeof(WCHAR
);
2327 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation
= NULL
;
2328 ULONG ValueInfoLength
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 50 * sizeof(WCHAR
);
2329 UNICODE_STRING DeviceName
, ValueName
;
2331 PCM_FULL_RESOURCE_DESCRIPTOR BootResources
= NULL
;
2332 ULONG BootResourcesLength
;
2335 const UNICODE_STRING IdentifierPci
= RTL_CONSTANT_STRING(L
"PCI");
2336 UNICODE_STRING HardwareIdPci
= RTL_CONSTANT_STRING(L
"*PNP0A03\0");
2337 static ULONG DeviceIndexPci
= 0;
2338 const UNICODE_STRING IdentifierSerial
= RTL_CONSTANT_STRING(L
"SerialController");
2339 UNICODE_STRING HardwareIdSerial
= RTL_CONSTANT_STRING(L
"*PNP0501\0");
2340 static ULONG DeviceIndexSerial
= 0;
2341 const UNICODE_STRING IdentifierKeyboard
= RTL_CONSTANT_STRING(L
"KeyboardController");
2342 UNICODE_STRING HardwareIdKeyboard
= RTL_CONSTANT_STRING(L
"*PNP0303\0");
2343 static ULONG DeviceIndexKeyboard
= 0;
2344 const UNICODE_STRING IdentifierMouse
= RTL_CONSTANT_STRING(L
"PointerController");
2345 UNICODE_STRING HardwareIdMouse
= RTL_CONSTANT_STRING(L
"*PNP0F13\0");
2346 static ULONG DeviceIndexMouse
= 0;
2347 const UNICODE_STRING IdentifierParallel
= RTL_CONSTANT_STRING(L
"ParallelController");
2348 UNICODE_STRING HardwareIdParallel
= RTL_CONSTANT_STRING(L
"*PNP0400\0");
2349 static ULONG DeviceIndexParallel
= 0;
2350 const UNICODE_STRING IdentifierFloppy
= RTL_CONSTANT_STRING(L
"FloppyDiskPeripheral");
2351 UNICODE_STRING HardwareIdFloppy
= RTL_CONSTANT_STRING(L
"*PNP0700\0");
2352 static ULONG DeviceIndexFloppy
= 0;
2353 const UNICODE_STRING IdentifierIsa
= RTL_CONSTANT_STRING(L
"ISA");
2354 UNICODE_STRING HardwareIdIsa
= RTL_CONSTANT_STRING(L
"*PNP0A00\0");
2355 static ULONG DeviceIndexIsa
= 0;
2356 UNICODE_STRING HardwareIdKey
;
2357 PUNICODE_STRING pHardwareId
;
2358 ULONG DeviceIndex
= 0;
2359 PUCHAR CmResourceList
;
2364 Status
= IopOpenRegistryKeyEx(&hDevicesKey
, hBaseKey
, RelativePath
, KEY_ENUMERATE_SUB_KEYS
);
2365 if (!NT_SUCCESS(Status
))
2367 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2372 hDevicesKey
= hBaseKey
;
2374 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2375 if (!pDeviceInformation
)
2377 DPRINT("ExAllocatePool() failed\n");
2378 Status
= STATUS_NO_MEMORY
;
2382 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2383 if (!pValueInformation
)
2385 DPRINT("ExAllocatePool() failed\n");
2386 Status
= STATUS_NO_MEMORY
;
2392 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2393 if (Status
== STATUS_NO_MORE_ENTRIES
)
2395 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2397 ExFreePool(pDeviceInformation
);
2398 DeviceInfoLength
= RequiredSize
;
2399 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2400 if (!pDeviceInformation
)
2402 DPRINT("ExAllocatePool() failed\n");
2403 Status
= STATUS_NO_MEMORY
;
2406 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2408 if (!NT_SUCCESS(Status
))
2410 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
2415 /* Open device key */
2416 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
2417 DeviceName
.Buffer
= pDeviceInformation
->Name
;
2419 Status
= IopOpenRegistryKeyEx(&hDeviceKey
, hDevicesKey
, &DeviceName
,
2420 KEY_QUERY_VALUE
+ (EnumerateSubKeys
? KEY_ENUMERATE_SUB_KEYS
: 0));
2421 if (!NT_SUCCESS(Status
))
2423 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2427 /* Read boot resources, and add then to parent ones */
2428 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2429 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2431 ExFreePool(pValueInformation
);
2432 ValueInfoLength
= RequiredSize
;
2433 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2434 if (!pValueInformation
)
2436 DPRINT("ExAllocatePool() failed\n");
2437 ZwDeleteKey(hLevel2Key
);
2438 Status
= STATUS_NO_MEMORY
;
2441 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2443 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
2445 BootResources
= ParentBootResources
;
2446 BootResourcesLength
= ParentBootResourcesLength
;
2448 else if (!NT_SUCCESS(Status
))
2450 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
2453 else if (pValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
)
2455 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_FULL_RESOURCE_DESCRIPTOR
);
2460 static const ULONG Header
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
);
2462 /* Concatenate current resources and parent ones */
2463 if (ParentBootResourcesLength
== 0)
2464 BootResourcesLength
= pValueInformation
->DataLength
;
2466 BootResourcesLength
= ParentBootResourcesLength
2467 + pValueInformation
->DataLength
2469 BootResources
= ExAllocatePool(PagedPool
, BootResourcesLength
);
2472 DPRINT("ExAllocatePool() failed\n");
2475 if (ParentBootResourcesLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
2477 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
2479 else if (ParentBootResources
->PartialResourceList
.PartialDescriptors
[ParentBootResources
->PartialResourceList
.Count
- 1].Type
== CmResourceTypeDeviceSpecific
)
2481 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
2483 (PVOID
)((ULONG_PTR
)BootResources
+ pValueInformation
->DataLength
),
2484 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
2485 ParentBootResourcesLength
- Header
);
2486 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
2490 RtlCopyMemory(BootResources
, pValueInformation
->Data
, Header
);
2492 (PVOID
)((ULONG_PTR
)BootResources
+ Header
),
2493 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
2494 ParentBootResourcesLength
- Header
);
2496 (PVOID
)((ULONG_PTR
)BootResources
+ ParentBootResourcesLength
),
2497 pValueInformation
->Data
+ Header
,
2498 pValueInformation
->DataLength
- Header
);
2499 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
2503 if (EnumerateSubKeys
)
2508 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2509 if (Status
== STATUS_NO_MORE_ENTRIES
)
2511 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2513 ExFreePool(pDeviceInformation
);
2514 DeviceInfoLength
= RequiredSize
;
2515 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2516 if (!pDeviceInformation
)
2518 DPRINT("ExAllocatePool() failed\n");
2519 Status
= STATUS_NO_MEMORY
;
2522 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2524 if (!NT_SUCCESS(Status
))
2526 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
2530 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
2531 DeviceName
.Buffer
= pDeviceInformation
->Name
;
2533 Status
= IopEnumerateDetectedDevices(
2539 BootResourcesLength
);
2540 if (!NT_SUCCESS(Status
))
2545 /* Read identifier */
2546 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2547 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2549 ExFreePool(pValueInformation
);
2550 ValueInfoLength
= RequiredSize
;
2551 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2552 if (!pValueInformation
)
2554 DPRINT("ExAllocatePool() failed\n");
2555 Status
= STATUS_NO_MEMORY
;
2558 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2560 if (!NT_SUCCESS(Status
))
2562 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
2564 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
2567 ValueName
.Length
= ValueName
.MaximumLength
= 0;
2569 else if (pValueInformation
->Type
!= REG_SZ
)
2571 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_SZ
);
2576 /* Assign hardware id to this device */
2577 ValueName
.Length
= ValueName
.MaximumLength
= (USHORT
)pValueInformation
->DataLength
;
2578 ValueName
.Buffer
= (PWCHAR
)pValueInformation
->Data
;
2579 if (ValueName
.Length
>= sizeof(WCHAR
) && ValueName
.Buffer
[ValueName
.Length
/ sizeof(WCHAR
) - 1] == UNICODE_NULL
)
2580 ValueName
.Length
-= sizeof(WCHAR
);
2583 if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierSerial
, FALSE
) == 0)
2585 pHardwareId
= &HardwareIdSerial
;
2586 DeviceIndex
= DeviceIndexSerial
++;
2588 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierKeyboard
, FALSE
) == 0)
2590 pHardwareId
= &HardwareIdKeyboard
;
2591 DeviceIndex
= DeviceIndexKeyboard
++;
2593 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierMouse
, FALSE
) == 0)
2595 pHardwareId
= &HardwareIdMouse
;
2596 DeviceIndex
= DeviceIndexMouse
++;
2598 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierParallel
, FALSE
) == 0)
2600 pHardwareId
= &HardwareIdParallel
;
2601 DeviceIndex
= DeviceIndexParallel
++;
2603 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierFloppy
, FALSE
) == 0)
2605 pHardwareId
= &HardwareIdFloppy
;
2606 DeviceIndex
= DeviceIndexFloppy
++;
2608 else if (NT_SUCCESS(Status
))
2610 /* Try to also match the device identifier */
2611 if (RtlCompareUnicodeString(&ValueName
, &IdentifierPci
, FALSE
) == 0)
2613 pHardwareId
= &HardwareIdPci
;
2614 DeviceIndex
= DeviceIndexPci
++;
2616 else if (RtlCompareUnicodeString(&ValueName
, &IdentifierIsa
, FALSE
) == 0)
2618 pHardwareId
= &HardwareIdIsa
;
2619 DeviceIndex
= DeviceIndexIsa
++;
2623 DPRINT("Unknown device '%wZ'\n", &ValueName
);
2629 /* Unknown key path */
2630 DPRINT("Unknown key path '%wZ'\n", RelativePath
);
2634 /* Prepare hardware id key (hardware id value without final \0) */
2635 HardwareIdKey
= *pHardwareId
;
2636 HardwareIdKey
.Length
-= sizeof(UNICODE_NULL
);
2638 /* Add the detected device to Root key */
2639 InitializeObjectAttributes(&ObjectAttributes
, &HardwareIdKey
, OBJ_KERNEL_HANDLE
, hRootKey
, NULL
);
2640 Status
= ZwCreateKey(
2646 ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0,
2648 if (!NT_SUCCESS(Status
))
2650 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2653 swprintf(Level2Name
, L
"%04lu", DeviceIndex
);
2654 RtlInitUnicodeString(&Level2NameU
, Level2Name
);
2655 InitializeObjectAttributes(&ObjectAttributes
, &Level2NameU
, OBJ_KERNEL_HANDLE
, hLevel1Key
, NULL
);
2656 Status
= ZwCreateKey(
2658 KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
,
2662 ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0,
2664 ZwClose(hLevel1Key
);
2665 if (!NT_SUCCESS(Status
))
2667 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2670 DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName
, DeviceIndex
, &HardwareIdKey
);
2671 Status
= ZwSetValueKey(hLevel2Key
, &HardwareIDU
, 0, REG_MULTI_SZ
, pHardwareId
->Buffer
, pHardwareId
->MaximumLength
);
2672 if (!NT_SUCCESS(Status
))
2674 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
2675 ZwDeleteKey(hLevel2Key
);
2678 /* Create 'LogConf' subkey */
2679 InitializeObjectAttributes(&ObjectAttributes
, &LogConfU
, OBJ_KERNEL_HANDLE
, hLevel2Key
, NULL
);
2680 Status
= ZwCreateKey(
2686 REG_OPTION_VOLATILE
,
2688 if (!NT_SUCCESS(Status
))
2690 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2691 ZwDeleteKey(hLevel2Key
);
2694 if (BootResourcesLength
>= sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
2696 CmResourceList
= ExAllocatePool(PagedPool
, BootResourcesLength
+ sizeof(ULONG
));
2697 if (!CmResourceList
)
2700 ZwDeleteKey(hLevel2Key
);
2704 /* Add the list count (1st member of CM_RESOURCE_LIST) */
2706 RtlCopyMemory(CmResourceList
,
2710 /* Now add the actual list (2nd member of CM_RESOURCE_LIST) */
2711 RtlCopyMemory(CmResourceList
+ sizeof(ULONG
),
2713 BootResourcesLength
);
2715 /* Save boot resources to 'LogConf\BootConfig' */
2716 Status
= ZwSetValueKey(hLogConf
, &BootConfigU
, 0, REG_RESOURCE_LIST
, CmResourceList
, BootResourcesLength
+ sizeof(ULONG
));
2717 if (!NT_SUCCESS(Status
))
2719 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
2721 ZwDeleteKey(hLevel2Key
);
2728 if (BootResources
&& BootResources
!= ParentBootResources
)
2730 ExFreePool(BootResources
);
2731 BootResources
= NULL
;
2735 ZwClose(hLevel2Key
);
2740 ZwClose(hDeviceKey
);
2745 Status
= STATUS_SUCCESS
;
2748 if (hDevicesKey
&& hDevicesKey
!= hBaseKey
)
2749 ZwClose(hDevicesKey
);
2751 ZwClose(hDeviceKey
);
2752 if (pDeviceInformation
)
2753 ExFreePool(pDeviceInformation
);
2754 if (pValueInformation
)
2755 ExFreePool(pValueInformation
);
2759 static BOOLEAN INIT_FUNCTION
2760 IopIsFirmwareMapperDisabled(VOID
)
2762 UNICODE_STRING KeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CURRENTCONTROLSET\\Control\\Pnp");
2763 UNICODE_STRING KeyNameU
= RTL_CONSTANT_STRING(L
"DisableFirmwareMapper");
2764 OBJECT_ATTRIBUTES ObjectAttributes
;
2766 PKEY_VALUE_PARTIAL_INFORMATION KeyInformation
;
2767 ULONG DesiredLength
, Length
, KeyValue
;
2770 InitializeObjectAttributes(&ObjectAttributes
, &KeyPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
2771 Status
= ZwOpenKey(&hPnpKey
, KEY_QUERY_VALUE
, &ObjectAttributes
);
2772 if (NT_SUCCESS(Status
))
2774 Status
= ZwQueryValueKey(hPnpKey
,
2776 KeyValuePartialInformation
,
2780 if ((Status
== STATUS_BUFFER_TOO_SMALL
) ||
2781 (Status
== STATUS_BUFFER_OVERFLOW
))
2783 Length
= DesiredLength
;
2784 KeyInformation
= ExAllocatePool(PagedPool
, Length
);
2787 Status
= ZwQueryValueKey(hPnpKey
,
2789 KeyValuePartialInformation
,
2793 if (NT_SUCCESS(Status
) && KeyInformation
->DataLength
== sizeof(ULONG
))
2795 KeyValue
= (ULONG
)(*KeyInformation
->Data
);
2799 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed\n", &KeyPathU
, &KeyNameU
);
2803 ExFreePool(KeyInformation
);
2807 DPRINT1("Failed to allocate memory for registry query\n");
2813 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed with status 0x%08lx\n", &KeyPathU
, &KeyNameU
, Status
);
2821 DPRINT1("ZwOpenKey(%wZ) failed with status 0x%08lx\n", &KeyPathU
, Status
);
2824 DPRINT1("Firmware mapper is %s\n", KeyValue
!= 0 ? "disabled" : "enabled");
2826 return (KeyValue
!= 0) ? TRUE
: FALSE
;
2832 IopUpdateRootKey(VOID
)
2834 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
2835 UNICODE_STRING RootPathU
= RTL_CONSTANT_STRING(L
"Root");
2836 UNICODE_STRING MultiKeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
2837 OBJECT_ATTRIBUTES ObjectAttributes
;
2838 HANDLE hEnum
, hRoot
;
2841 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
2842 Status
= ZwCreateKey(&hEnum
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
2843 if (!NT_SUCCESS(Status
))
2845 DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2849 InitializeObjectAttributes(&ObjectAttributes
, &RootPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hEnum
, NULL
);
2850 Status
= ZwCreateKey(&hRoot
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
2852 if (!NT_SUCCESS(Status
))
2854 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2858 if (!IopIsFirmwareMapperDisabled())
2860 Status
= IopOpenRegistryKeyEx(&hEnum
, NULL
, &MultiKeyPathU
, KEY_ENUMERATE_SUB_KEYS
);
2861 if (!NT_SUCCESS(Status
))
2863 /* Nothing to do, don't return with an error status */
2864 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2866 return STATUS_SUCCESS
;
2868 Status
= IopEnumerateDetectedDevices(
2879 /* Enumeration is disabled */
2880 Status
= STATUS_SUCCESS
;
2890 IopOpenRegistryKeyEx(PHANDLE KeyHandle
,
2892 PUNICODE_STRING Name
,
2893 ACCESS_MASK DesiredAccess
)
2895 OBJECT_ATTRIBUTES ObjectAttributes
;
2902 InitializeObjectAttributes(&ObjectAttributes
,
2904 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2908 Status
= ZwOpenKey(KeyHandle
, DesiredAccess
, &ObjectAttributes
);
2915 IopCreateRegistryKeyEx(OUT PHANDLE Handle
,
2916 IN HANDLE RootHandle OPTIONAL
,
2917 IN PUNICODE_STRING KeyName
,
2918 IN ACCESS_MASK DesiredAccess
,
2919 IN ULONG CreateOptions
,
2920 OUT PULONG Disposition OPTIONAL
)
2922 OBJECT_ATTRIBUTES ObjectAttributes
;
2923 ULONG KeyDisposition
, RootHandleIndex
= 0, i
= 1, NestedCloseLevel
= 0;
2925 HANDLE HandleArray
[2];
2926 BOOLEAN Recursing
= TRUE
;
2928 UNICODE_STRING KeyString
;
2929 NTSTATUS Status
= STATUS_SUCCESS
;
2932 /* P1 is start, pp is end */
2933 p1
= KeyName
->Buffer
;
2934 pp
= (PVOID
)((ULONG_PTR
)p1
+ KeyName
->Length
);
2936 /* Create the target key */
2937 InitializeObjectAttributes(&ObjectAttributes
,
2939 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2942 Status
= ZwCreateKey(&HandleArray
[i
],
2950 /* Now we check if this failed */
2951 if ((Status
== STATUS_OBJECT_NAME_NOT_FOUND
) && (RootHandle
))
2953 /* Target key failed, so we'll need to create its parent. Setup array */
2954 HandleArray
[0] = NULL
;
2955 HandleArray
[1] = RootHandle
;
2957 /* Keep recursing for each missing parent */
2960 /* And if we're deep enough, close the last handle */
2961 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
2963 /* We're setup to ping-pong between the two handle array entries */
2964 RootHandleIndex
= i
;
2967 /* Clear the one we're attempting to open now */
2968 HandleArray
[i
] = NULL
;
2970 /* Process the parent key name */
2971 for (p
= p1
; ((p
< pp
) && (*p
!= OBJ_NAME_PATH_SEPARATOR
)); p
++);
2972 Length
= (USHORT
)(p
- p1
) * sizeof(WCHAR
);
2974 /* Is there a parent name? */
2977 /* Build the unicode string for it */
2978 KeyString
.Buffer
= p1
;
2979 KeyString
.Length
= KeyString
.MaximumLength
= Length
;
2981 /* Now try opening the parent */
2982 InitializeObjectAttributes(&ObjectAttributes
,
2984 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2985 HandleArray
[RootHandleIndex
],
2987 Status
= ZwCreateKey(&HandleArray
[i
],
2994 if (NT_SUCCESS(Status
))
2996 /* It worked, we have one more handle */
3001 /* Parent key creation failed, abandon loop */
3008 /* We don't have a parent name, probably corrupted key name */
3009 Status
= STATUS_INVALID_PARAMETER
;
3014 /* Now see if there's more parents to create */
3016 if ((p
== pp
) || (p1
== pp
))
3018 /* We're done, hopefully successfully, so stop */
3023 /* Outer loop check for handle nesting that requires closing the top handle */
3024 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
3027 /* Check if we broke out of the loop due to success */
3028 if (NT_SUCCESS(Status
))
3030 /* Return the target handle (we closed all the parent ones) and disposition */
3031 *Handle
= HandleArray
[i
];
3032 if (Disposition
) *Disposition
= KeyDisposition
;
3035 /* Return the success state */
3041 IopGetRegistryValue(IN HANDLE Handle
,
3043 OUT PKEY_VALUE_FULL_INFORMATION
*Information
)
3045 UNICODE_STRING ValueString
;
3047 PKEY_VALUE_FULL_INFORMATION FullInformation
;
3051 RtlInitUnicodeString(&ValueString
, ValueName
);
3053 Status
= ZwQueryValueKey(Handle
,
3055 KeyValueFullInformation
,
3059 if ((Status
!= STATUS_BUFFER_OVERFLOW
) &&
3060 (Status
!= STATUS_BUFFER_TOO_SMALL
))
3065 FullInformation
= ExAllocatePool(NonPagedPool
, Size
);
3066 if (!FullInformation
) return STATUS_INSUFFICIENT_RESOURCES
;
3068 Status
= ZwQueryValueKey(Handle
,
3070 KeyValueFullInformation
,
3074 if (!NT_SUCCESS(Status
))
3076 ExFreePool(FullInformation
);
3080 *Information
= FullInformation
;
3081 return STATUS_SUCCESS
;
3084 RTL_GENERIC_COMPARE_RESULTS
3086 PiCompareInstancePath(IN PRTL_AVL_TABLE Table
,
3087 IN PVOID FirstStruct
,
3088 IN PVOID SecondStruct
)
3096 // The allocation function is called by the generic table package whenever
3097 // it needs to allocate memory for the table.
3102 PiAllocateGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3112 PiFreeGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3121 PpInitializeDeviceReferenceTable(VOID
)
3123 /* Setup the guarded mutex and AVL table */
3124 KeInitializeGuardedMutex(&PpDeviceReferenceTableLock
);
3125 RtlInitializeGenericTableAvl(
3126 &PpDeviceReferenceTable
,
3127 (PRTL_AVL_COMPARE_ROUTINE
)PiCompareInstancePath
,
3128 (PRTL_AVL_ALLOCATE_ROUTINE
)PiAllocateGenericTableEntry
,
3129 (PRTL_AVL_FREE_ROUTINE
)PiFreeGenericTableEntry
,
3137 /* Initialize the resource when accessing device registry data */
3138 ExInitializeResourceLite(&PpRegistryDeviceResource
);
3140 /* Setup the device reference AVL table */
3141 PpInitializeDeviceReferenceTable();
3149 /* Check the initialization phase */
3150 switch (ExpInitializationPhase
)
3155 return PiInitPhase0();
3161 //return PiInitPhase1();
3165 /* Don't know any other phase! Bugcheck! */
3166 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
3171 LONG IopNumberDeviceNodes
;
3175 PipAllocateDeviceNode(IN PDEVICE_OBJECT PhysicalDeviceObject
)
3177 PDEVICE_NODE DeviceNode
;
3181 DeviceNode
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(DEVICE_NODE
), 'donD');
3182 if (!DeviceNode
) return DeviceNode
;
3185 InterlockedIncrement(&IopNumberDeviceNodes
);
3188 RtlZeroMemory(DeviceNode
, sizeof(DEVICE_NODE
));
3189 DeviceNode
->InterfaceType
= InterfaceTypeUndefined
;
3190 DeviceNode
->BusNumber
= -1;
3191 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
3192 DeviceNode
->ChildBusNumber
= -1;
3193 DeviceNode
->ChildBusTypeIndex
= -1;
3194 // KeInitializeEvent(&DeviceNode->EnumerationMutex, SynchronizationEvent, TRUE);
3195 InitializeListHead(&DeviceNode
->DeviceArbiterList
);
3196 InitializeListHead(&DeviceNode
->DeviceTranslatorList
);
3197 InitializeListHead(&DeviceNode
->TargetDeviceNotify
);
3198 InitializeListHead(&DeviceNode
->DockInfo
.ListEntry
);
3199 InitializeListHead(&DeviceNode
->PendedSetInterfaceState
);
3201 /* Check if there is a PDO */
3202 if (PhysicalDeviceObject
)
3204 /* Link it and remove the init flag */
3205 DeviceNode
->PhysicalDeviceObject
= PhysicalDeviceObject
;
3206 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= DeviceNode
;
3207 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
3210 /* Return the node */
3214 /* PUBLIC FUNCTIONS **********************************************************/
3218 PnpBusTypeGuidGet(IN USHORT Index
,
3219 IN LPGUID BusTypeGuid
)
3221 NTSTATUS Status
= STATUS_SUCCESS
;
3223 /* Acquire the lock */
3224 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
3227 if (Index
< PnpBusTypeGuidList
->GuidCount
)
3230 RtlCopyMemory(BusTypeGuid
, &PnpBusTypeGuidList
->Guids
[Index
], sizeof(GUID
));
3235 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
3238 /* Release lock and return status */
3239 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
3245 PnpDeviceObjectToDeviceInstance(IN PDEVICE_OBJECT DeviceObject
,
3246 IN PHANDLE DeviceInstanceHandle
,
3247 IN ACCESS_MASK DesiredAccess
)
3251 PDEVICE_NODE DeviceNode
;
3252 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\ENUM");
3255 /* Open the enum key */
3256 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
3260 if (!NT_SUCCESS(Status
)) return Status
;
3262 /* Make sure we have an instance path */
3263 DeviceNode
= IopGetDeviceNode(DeviceObject
);
3264 if ((DeviceNode
) && (DeviceNode
->InstancePath
.Length
))
3266 /* Get the instance key */
3267 Status
= IopOpenRegistryKeyEx(DeviceInstanceHandle
,
3269 &DeviceNode
->InstancePath
,
3275 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3278 /* Close the handle and return status */
3285 PnpDetermineResourceListSize(IN PCM_RESOURCE_LIST ResourceList
)
3287 ULONG FinalSize
, PartialSize
, EntrySize
, i
, j
;
3288 PCM_FULL_RESOURCE_DESCRIPTOR FullDescriptor
;
3289 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
3291 /* If we don't have one, that's easy */
3292 if (!ResourceList
) return 0;
3294 /* Start with the minimum size possible */
3295 FinalSize
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
3297 /* Loop each full descriptor */
3298 FullDescriptor
= ResourceList
->List
;
3299 for (i
= 0; i
< ResourceList
->Count
; i
++)
3301 /* Start with the minimum size possible */
3302 PartialSize
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
) +
3303 FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST
, PartialDescriptors
);
3305 /* Loop each partial descriptor */
3306 PartialDescriptor
= FullDescriptor
->PartialResourceList
.PartialDescriptors
;
3307 for (j
= 0; j
< FullDescriptor
->PartialResourceList
.Count
; j
++)
3309 /* Start with the minimum size possible */
3310 EntrySize
= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
3312 /* Check if there is extra data */
3313 if (PartialDescriptor
->Type
== CmResourceTypeDeviceSpecific
)
3316 EntrySize
+= PartialDescriptor
->u
.DeviceSpecificData
.DataSize
;
3319 /* The size of partial descriptors is bigger */
3320 PartialSize
+= EntrySize
;
3322 /* Go to the next partial descriptor */
3323 PartialDescriptor
= (PVOID
)((ULONG_PTR
)PartialDescriptor
+ EntrySize
);
3326 /* The size of full descriptors is bigger */
3327 FinalSize
+= PartialSize
;
3329 /* Go to the next full descriptor */
3330 FullDescriptor
= (PVOID
)((ULONG_PTR
)FullDescriptor
+ PartialSize
);
3333 /* Return the final size */
3339 PiGetDeviceRegistryProperty(IN PDEVICE_OBJECT DeviceObject
,
3344 IN PULONG BufferLength
)
3347 HANDLE KeyHandle
, SubHandle
;
3348 UNICODE_STRING KeyString
;
3349 PKEY_VALUE_FULL_INFORMATION KeyValueInfo
= NULL
;
3353 /* Find the instance key */
3354 Status
= PnpDeviceObjectToDeviceInstance(DeviceObject
, &KeyHandle
, KEY_READ
);
3355 if (NT_SUCCESS(Status
))
3357 /* Check for name given by caller */
3361 RtlInitUnicodeString(&KeyString
, KeyName
);
3362 Status
= IopOpenRegistryKeyEx(&SubHandle
,
3366 if (NT_SUCCESS(Status
))
3368 /* And use this handle instead */
3370 KeyHandle
= SubHandle
;
3374 /* Check if sub-key handle succeeded (or no-op if no key name given) */
3375 if (NT_SUCCESS(Status
))
3377 /* Now get the size of the property */
3378 Status
= IopGetRegistryValue(KeyHandle
,
3387 /* Fail if any of the registry operations failed */
3388 if (!NT_SUCCESS(Status
)) return Status
;
3390 /* Check how much data we have to copy */
3391 Length
= KeyValueInfo
->DataLength
;
3392 if (*BufferLength
>= Length
)
3394 /* Check for a match in the value type */
3395 if (KeyValueInfo
->Type
== ValueType
)
3398 RtlCopyMemory(Buffer
,
3399 (PVOID
)((ULONG_PTR
)KeyValueInfo
+
3400 KeyValueInfo
->DataOffset
),
3405 /* Invalid registry property type, fail */
3406 Status
= STATUS_INVALID_PARAMETER_2
;
3411 /* Buffer is too small to hold data */
3412 Status
= STATUS_BUFFER_TOO_SMALL
;
3415 /* Return the required buffer length, free the buffer, and return status */
3416 *BufferLength
= Length
;
3417 ExFreePool(KeyValueInfo
);
3421 #define PIP_RETURN_DATA(x, y) {ReturnLength = x; Data = y; Status = STATUS_SUCCESS; break;}
3422 #define PIP_REGISTRY_DATA(x, y) {ValueName = x; ValueType = y; break;}
3423 #define PIP_UNIMPLEMENTED() {UNIMPLEMENTED; while(TRUE); break;}
3430 IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject
,
3431 IN DEVICE_REGISTRY_PROPERTY DeviceProperty
,
3432 IN ULONG BufferLength
,
3433 OUT PVOID PropertyBuffer
,
3434 OUT PULONG ResultLength
)
3436 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
3437 DEVICE_CAPABILITIES DeviceCaps
;
3438 ULONG ReturnLength
= 0, Length
= 0, ValueType
;
3439 PWCHAR ValueName
= NULL
, EnumeratorNameEnd
, DeviceInstanceName
;
3441 NTSTATUS Status
= STATUS_BUFFER_TOO_SMALL
;
3443 POBJECT_NAME_INFORMATION ObjectNameInfo
= NULL
;
3444 BOOLEAN NullTerminate
= FALSE
;
3446 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject
, DeviceProperty
);
3448 /* Assume failure */
3451 /* Only PDOs can call this */
3452 if (!DeviceNode
) return STATUS_INVALID_DEVICE_REQUEST
;
3454 /* Handle all properties */
3455 switch (DeviceProperty
)
3457 case DevicePropertyBusTypeGuid
:
3459 /* Get the GUID from the internal cache */
3460 Status
= PnpBusTypeGuidGet(DeviceNode
->ChildBusTypeIndex
, &BusTypeGuid
);
3461 if (!NT_SUCCESS(Status
)) return Status
;
3463 /* This is the format of the returned data */
3464 PIP_RETURN_DATA(sizeof(GUID
), &BusTypeGuid
);
3466 case DevicePropertyLegacyBusType
:
3468 /* Validate correct interface type */
3469 if (DeviceNode
->ChildInterfaceType
== InterfaceTypeUndefined
)
3470 return STATUS_OBJECT_NAME_NOT_FOUND
;
3472 /* This is the format of the returned data */
3473 PIP_RETURN_DATA(sizeof(INTERFACE_TYPE
), &DeviceNode
->ChildInterfaceType
);
3475 case DevicePropertyBusNumber
:
3477 /* Validate correct bus number */
3478 if ((DeviceNode
->ChildBusNumber
& 0x80000000) == 0x80000000)
3479 return STATUS_OBJECT_NAME_NOT_FOUND
;
3481 /* This is the format of the returned data */
3482 PIP_RETURN_DATA(sizeof(ULONG
), &DeviceNode
->ChildBusNumber
);
3484 case DevicePropertyEnumeratorName
:
3486 /* Get the instance path */
3487 DeviceInstanceName
= DeviceNode
->InstancePath
.Buffer
;
3490 ASSERT((BufferLength
& 1) == 0);
3491 ASSERT(DeviceInstanceName
!= NULL
);
3493 /* Get the name from the path */
3494 EnumeratorNameEnd
= wcschr(DeviceInstanceName
, OBJ_NAME_PATH_SEPARATOR
);
3495 ASSERT(EnumeratorNameEnd
);
3497 /* This string needs to be NULL-terminated */
3498 NullTerminate
= TRUE
;
3500 /* This is the format of the returned data */
3501 PIP_RETURN_DATA((ULONG
)(EnumeratorNameEnd
- DeviceInstanceName
) * sizeof(WCHAR
),
3502 DeviceInstanceName
);
3504 case DevicePropertyAddress
:
3506 /* Query the device caps */
3507 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
);
3508 if (!NT_SUCCESS(Status
) || (DeviceCaps
.Address
== MAXULONG
))
3509 return STATUS_OBJECT_NAME_NOT_FOUND
;
3511 /* This is the format of the returned data */
3512 PIP_RETURN_DATA(sizeof(ULONG
), &DeviceCaps
.Address
);
3514 case DevicePropertyBootConfigurationTranslated
:
3516 /* Validate we have resources */
3517 if (!DeviceNode
->BootResources
)
3518 // if (!DeviceNode->BootResourcesTranslated) // FIXFIX: Need this field
3520 /* No resources will still fake success, but with 0 bytes */
3522 return STATUS_SUCCESS
;
3525 /* This is the format of the returned data */
3526 PIP_RETURN_DATA(PnpDetermineResourceListSize(DeviceNode
->BootResources
), // FIXFIX: Should use BootResourcesTranslated
3527 DeviceNode
->BootResources
); // FIXFIX: Should use BootResourcesTranslated
3529 case DevicePropertyPhysicalDeviceObjectName
:
3531 /* Sanity check for Unicode-sized string */
3532 ASSERT((BufferLength
& 1) == 0);
3534 /* Allocate name buffer */
3535 Length
= BufferLength
+ sizeof(OBJECT_NAME_INFORMATION
);
3536 ObjectNameInfo
= ExAllocatePool(PagedPool
, Length
);
3537 if (!ObjectNameInfo
) return STATUS_INSUFFICIENT_RESOURCES
;
3539 /* Query the PDO name */
3540 Status
= ObQueryNameString(DeviceObject
,
3544 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
3546 /* It's up to the caller to try again */
3547 Status
= STATUS_BUFFER_TOO_SMALL
;
3550 /* This string needs to be NULL-terminated */
3551 NullTerminate
= TRUE
;
3553 /* Return if successful */
3554 if (NT_SUCCESS(Status
)) PIP_RETURN_DATA(ObjectNameInfo
->Name
.Length
,
3555 ObjectNameInfo
->Name
.Buffer
);
3557 /* Let the caller know how big the name is */
3558 *ResultLength
-= sizeof(OBJECT_NAME_INFORMATION
);
3561 /* Handle the registry-based properties */
3562 case DevicePropertyUINumber
:
3563 PIP_REGISTRY_DATA(REGSTR_VAL_UI_NUMBER
, REG_DWORD
);
3564 case DevicePropertyLocationInformation
:
3565 PIP_REGISTRY_DATA(REGSTR_VAL_LOCATION_INFORMATION
, REG_SZ
);
3566 case DevicePropertyDeviceDescription
:
3567 PIP_REGISTRY_DATA(REGSTR_VAL_DEVDESC
, REG_SZ
);
3568 case DevicePropertyHardwareID
:
3569 PIP_REGISTRY_DATA(REGSTR_VAL_HARDWAREID
, REG_MULTI_SZ
);
3570 case DevicePropertyCompatibleIDs
:
3571 PIP_REGISTRY_DATA(REGSTR_VAL_COMPATIBLEIDS
, REG_MULTI_SZ
);
3572 case DevicePropertyBootConfiguration
:
3573 PIP_REGISTRY_DATA(REGSTR_VAL_BOOTCONFIG
, REG_RESOURCE_LIST
);
3574 case DevicePropertyClassName
:
3575 PIP_REGISTRY_DATA(REGSTR_VAL_CLASS
, REG_SZ
);
3576 case DevicePropertyClassGuid
:
3577 PIP_REGISTRY_DATA(REGSTR_VAL_CLASSGUID
, REG_SZ
);
3578 case DevicePropertyDriverKeyName
:
3579 PIP_REGISTRY_DATA(REGSTR_VAL_DRIVER
, REG_SZ
);
3580 case DevicePropertyManufacturer
:
3581 PIP_REGISTRY_DATA(REGSTR_VAL_MFG
, REG_SZ
);
3582 case DevicePropertyFriendlyName
:
3583 PIP_REGISTRY_DATA(REGSTR_VAL_FRIENDLYNAME
, REG_SZ
);
3584 case DevicePropertyContainerID
:
3585 //PIP_REGISTRY_DATA(REGSTR_VAL_CONTAINERID, REG_SZ); // Win7
3586 PIP_UNIMPLEMENTED();
3587 case DevicePropertyRemovalPolicy
:
3588 PIP_UNIMPLEMENTED();
3589 case DevicePropertyInstallState
:
3590 PIP_UNIMPLEMENTED();
3591 case DevicePropertyResourceRequirements
:
3592 PIP_UNIMPLEMENTED();
3593 case DevicePropertyAllocatedResources
:
3594 PIP_UNIMPLEMENTED();
3596 return STATUS_INVALID_PARAMETER_2
;
3599 /* Having a registry value name implies registry data */
3602 /* We know up-front how much data to expect */
3603 *ResultLength
= BufferLength
;
3605 /* Go get the data, use the LogConf subkey if necessary */
3606 Status
= PiGetDeviceRegistryProperty(DeviceObject
,
3610 DevicePropertyBootConfiguration
) ?
3615 else if (NT_SUCCESS(Status
))
3617 /* We know up-front how much data to expect, check the caller's buffer */
3618 *ResultLength
= ReturnLength
+ (NullTerminate
? sizeof(UNICODE_NULL
) : 0);
3619 if (*ResultLength
<= BufferLength
)
3621 /* Buffer is all good, copy the data */
3622 RtlCopyMemory(PropertyBuffer
, Data
, ReturnLength
);
3624 /* Check if we need to NULL-terminate the string */
3627 /* Terminate the string */
3628 ((PWCHAR
)PropertyBuffer
)[ReturnLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
3631 /* This is the success path */
3632 Status
= STATUS_SUCCESS
;
3637 Status
= STATUS_BUFFER_TOO_SMALL
;
3641 /* Free any allocation we may have made, and return the status code */
3642 if (ObjectNameInfo
) ExFreePool(ObjectNameInfo
);
3651 IoInvalidateDeviceState(IN PDEVICE_OBJECT PhysicalDeviceObject
)
3653 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(PhysicalDeviceObject
);
3654 IO_STACK_LOCATION Stack
;
3657 IO_STATUS_BLOCK IoStatusBlock
;
3659 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
3660 Stack
.MajorFunction
= IRP_MJ_PNP
;
3661 Stack
.MinorFunction
= IRP_MN_QUERY_PNP_DEVICE_STATE
;
3663 Status
= IopSynchronousCall(PhysicalDeviceObject
, &Stack
, (PVOID
*)&PnPFlags
);
3664 if (!NT_SUCCESS(Status
))
3666 DPRINT1("IRP_MN_QUERY_PNP_DEVICE_STATE failed with status 0x%x\n", Status
);
3670 if (PnPFlags
& PNP_DEVICE_NOT_DISABLEABLE
)
3671 DeviceNode
->UserFlags
|= DNUF_NOT_DISABLEABLE
;
3673 DeviceNode
->UserFlags
&= ~DNUF_NOT_DISABLEABLE
;
3675 if (PnPFlags
& PNP_DEVICE_DONT_DISPLAY_IN_UI
)
3676 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
3678 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
3680 if ((PnPFlags
& PNP_DEVICE_REMOVED
) ||
3681 ((PnPFlags
& PNP_DEVICE_FAILED
) && !(PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
)))
3683 /* Surprise removal */
3685 IopSendSurpriseRemoval(PhysicalDeviceObject
);
3687 /* Tell the user-mode PnP manager that a device was removed */
3688 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
3689 &DeviceNode
->InstancePath
);
3691 IopSendRemoveDevice(PhysicalDeviceObject
);
3693 else if ((PnPFlags
& PNP_DEVICE_FAILED
) && (PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
))
3695 /* Stop for resource rebalance */
3697 Status
= IopStopDevice(DeviceNode
);
3698 if (!NT_SUCCESS(Status
))
3700 DPRINT1("Failed to stop device for rebalancing\n");
3702 /* Stop failed so don't rebalance */
3703 PnPFlags
&= ~PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
;
3707 /* Resource rebalance */
3708 if (PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
)
3710 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
3712 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
3714 IRP_MN_QUERY_RESOURCES
,
3716 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
3718 DeviceNode
->BootResources
=
3719 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
3720 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
3724 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
3725 DeviceNode
->BootResources
= NULL
;
3728 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
3730 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
3732 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
3734 if (NT_SUCCESS(Status
))
3736 DeviceNode
->ResourceRequirements
=
3737 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
3741 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
3742 DeviceNode
->ResourceRequirements
= NULL
;
3745 /* IRP_MN_FILTER_RESOURCE_REQUIREMENTS is called indirectly by IopStartDevice */
3746 if (IopStartDevice(DeviceNode
) != STATUS_SUCCESS
)
3748 DPRINT1("Restart after resource rebalance failed\n");
3750 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
3751 DeviceNode
->Flags
|= DNF_START_FAILED
;
3753 IopRemoveDevice(DeviceNode
);
3759 * @name IoOpenDeviceRegistryKey
3761 * Open a registry key unique for a specified driver or device instance.
3763 * @param DeviceObject Device to get the registry key for.
3764 * @param DevInstKeyType Type of the key to return.
3765 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
3766 * @param DevInstRegKey Handle to the opened registry key on
3767 * successful return.
3775 IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject
,
3776 IN ULONG DevInstKeyType
,
3777 IN ACCESS_MASK DesiredAccess
,
3778 OUT PHANDLE DevInstRegKey
)
3780 static WCHAR RootKeyName
[] =
3781 L
"\\Registry\\Machine\\System\\CurrentControlSet\\";
3782 static WCHAR ProfileKeyName
[] =
3783 L
"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
3784 static WCHAR ClassKeyName
[] = L
"Control\\Class\\";
3785 static WCHAR EnumKeyName
[] = L
"Enum\\";
3786 static WCHAR DeviceParametersKeyName
[] = L
"Device Parameters";
3787 ULONG KeyNameLength
;
3788 LPWSTR KeyNameBuffer
;
3789 UNICODE_STRING KeyName
;
3790 ULONG DriverKeyLength
;
3791 OBJECT_ATTRIBUTES ObjectAttributes
;
3792 PDEVICE_NODE DeviceNode
= NULL
;
3795 DPRINT("IoOpenDeviceRegistryKey() called\n");
3797 if ((DevInstKeyType
& (PLUGPLAY_REGKEY_DEVICE
| PLUGPLAY_REGKEY_DRIVER
)) == 0)
3799 DPRINT1("IoOpenDeviceRegistryKey(): got wrong params, exiting... \n");
3800 return STATUS_INVALID_PARAMETER
;
3803 if (!IopIsValidPhysicalDeviceObject(DeviceObject
))
3804 return STATUS_INVALID_DEVICE_REQUEST
;
3805 DeviceNode
= IopGetDeviceNode(DeviceObject
);
3808 * Calculate the length of the base key name. This is the full
3809 * name for driver key or the name excluding "Device Parameters"
3810 * subkey for device key.
3813 KeyNameLength
= sizeof(RootKeyName
);
3814 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
3815 KeyNameLength
+= sizeof(ProfileKeyName
) - sizeof(UNICODE_NULL
);
3816 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
3818 KeyNameLength
+= sizeof(ClassKeyName
) - sizeof(UNICODE_NULL
);
3819 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
3820 0, NULL
, &DriverKeyLength
);
3821 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
3823 KeyNameLength
+= DriverKeyLength
;
3827 KeyNameLength
+= sizeof(EnumKeyName
) - sizeof(UNICODE_NULL
) +
3828 DeviceNode
->InstancePath
.Length
;
3832 * Now allocate the buffer for the key name...
3835 KeyNameBuffer
= ExAllocatePool(PagedPool
, KeyNameLength
);
3836 if (KeyNameBuffer
== NULL
)
3837 return STATUS_INSUFFICIENT_RESOURCES
;
3840 KeyName
.MaximumLength
= (USHORT
)KeyNameLength
;
3841 KeyName
.Buffer
= KeyNameBuffer
;
3844 * ...and build the key name.
3847 KeyName
.Length
+= sizeof(RootKeyName
) - sizeof(UNICODE_NULL
);
3848 RtlCopyMemory(KeyNameBuffer
, RootKeyName
, KeyName
.Length
);
3850 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
3851 RtlAppendUnicodeToString(&KeyName
, ProfileKeyName
);
3853 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
3855 RtlAppendUnicodeToString(&KeyName
, ClassKeyName
);
3856 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
3857 DriverKeyLength
, KeyNameBuffer
+
3858 (KeyName
.Length
/ sizeof(WCHAR
)),
3860 if (!NT_SUCCESS(Status
))
3862 DPRINT1("Call to IoGetDeviceProperty() failed with Status 0x%08lx\n", Status
);
3863 ExFreePool(KeyNameBuffer
);
3866 KeyName
.Length
+= (USHORT
)DriverKeyLength
- sizeof(UNICODE_NULL
);
3870 RtlAppendUnicodeToString(&KeyName
, EnumKeyName
);
3871 Status
= RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->InstancePath
);
3872 if (DeviceNode
->InstancePath
.Length
== 0)
3874 ExFreePool(KeyNameBuffer
);
3880 * Open the base key.
3882 Status
= IopOpenRegistryKeyEx(DevInstRegKey
, NULL
, &KeyName
, DesiredAccess
);
3883 if (!NT_SUCCESS(Status
))
3885 DPRINT1("IoOpenDeviceRegistryKey(%wZ): Base key doesn't exist, exiting... (Status 0x%08lx)\n", &KeyName
, Status
);
3886 ExFreePool(KeyNameBuffer
);
3889 ExFreePool(KeyNameBuffer
);
3892 * For driver key we're done now.
3895 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
3899 * Let's go further. For device key we must open "Device Parameters"
3900 * subkey and create it if it doesn't exist yet.
3903 RtlInitUnicodeString(&KeyName
, DeviceParametersKeyName
);
3904 InitializeObjectAttributes(&ObjectAttributes
, &KeyName
,
3905 OBJ_CASE_INSENSITIVE
, *DevInstRegKey
, NULL
);
3906 Status
= ZwCreateKey(DevInstRegKey
, DesiredAccess
, &ObjectAttributes
,
3907 0, NULL
, ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0, NULL
);
3908 ZwClose(ObjectAttributes
.RootDirectory
);
3915 IopQueryRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
3917 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
, FailedRemoveDevice
;
3921 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3922 ChildDeviceNode
= ParentDeviceNode
->Child
;
3923 while (ChildDeviceNode
!= NULL
)
3925 NextDeviceNode
= ChildDeviceNode
->Sibling
;
3926 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
3928 Status
= IopPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
3929 if (!NT_SUCCESS(Status
))
3931 FailedRemoveDevice
= ChildDeviceNode
;
3935 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3936 ChildDeviceNode
= NextDeviceNode
;
3938 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
3940 return STATUS_SUCCESS
;
3943 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3944 ChildDeviceNode
= ParentDeviceNode
->Child
;
3945 while (ChildDeviceNode
!= NULL
)
3947 NextDeviceNode
= ChildDeviceNode
->Sibling
;
3948 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
3950 IopCancelPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
3952 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
3953 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
3954 if (ChildDeviceNode
== FailedRemoveDevice
)
3957 ChildDeviceNode
= NextDeviceNode
;
3959 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3961 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
3968 IopSendRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
3970 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
;
3973 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3974 ChildDeviceNode
= ParentDeviceNode
->Child
;
3975 while (ChildDeviceNode
!= NULL
)
3977 NextDeviceNode
= ChildDeviceNode
->Sibling
;
3978 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
3980 IopSendRemoveDevice(ChildDeviceNode
->PhysicalDeviceObject
);
3982 ChildDeviceNode
= NextDeviceNode
;
3984 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3986 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
3991 IopCancelRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
3993 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
;
3996 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
3997 ChildDeviceNode
= ParentDeviceNode
->Child
;
3998 while (ChildDeviceNode
!= NULL
)
4000 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4001 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4003 IopCancelPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
4005 ChildDeviceNode
= NextDeviceNode
;
4007 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4009 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4014 IopQueryRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4016 /* This function DOES NOT dereference the device objects on SUCCESS
4017 * but it DOES dereference device objects on FAILURE */
4022 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4024 Status
= IopPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4025 if (!NT_SUCCESS(Status
))
4032 return STATUS_SUCCESS
;
4035 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
4036 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
4037 for (i
= 0; i
<= j
; i
++)
4039 IopCancelPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4040 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4041 DeviceRelations
->Objects
[i
] = NULL
;
4043 for (; i
< DeviceRelations
->Count
; i
++)
4045 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4046 DeviceRelations
->Objects
[i
] = NULL
;
4048 ExFreePool(DeviceRelations
);
4055 IopSendRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4057 /* This function DOES dereference the device objects in all cases */
4061 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4063 IopSendRemoveDevice(DeviceRelations
->Objects
[i
]);
4064 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4065 DeviceRelations
->Objects
[i
] = NULL
;
4068 ExFreePool(DeviceRelations
);
4073 IopCancelRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4075 /* This function DOES dereference the device objects in all cases */
4079 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4081 IopCancelPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4082 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4083 DeviceRelations
->Objects
[i
] = NULL
;
4086 ExFreePool(DeviceRelations
);
4090 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
)
4092 IO_STACK_LOCATION Stack
;
4093 IO_STATUS_BLOCK IoStatusBlock
;
4094 PDEVICE_RELATIONS DeviceRelations
;
4097 IopCancelRemoveDevice(DeviceObject
);
4099 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
4101 Status
= IopInitiatePnpIrp(DeviceObject
,
4103 IRP_MN_QUERY_DEVICE_RELATIONS
,
4105 if (!NT_SUCCESS(Status
))
4107 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4108 DeviceRelations
= NULL
;
4112 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4115 if (DeviceRelations
)
4116 IopCancelRemoveDeviceRelations(DeviceRelations
);
4120 IopPrepareDeviceForRemoval(IN PDEVICE_OBJECT DeviceObject
)
4122 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
4123 IO_STACK_LOCATION Stack
;
4124 IO_STATUS_BLOCK IoStatusBlock
;
4125 PDEVICE_RELATIONS DeviceRelations
;
4128 if (DeviceNode
->UserFlags
& DNUF_NOT_DISABLEABLE
)
4130 DPRINT1("Removal not allowed for %wZ\n", &DeviceNode
->InstancePath
);
4131 return STATUS_UNSUCCESSFUL
;
4134 if (IopQueryRemoveDevice(DeviceObject
) != STATUS_SUCCESS
)
4136 DPRINT1("Removal vetoed by failing the query remove request\n");
4138 IopCancelRemoveDevice(DeviceObject
);
4140 return STATUS_UNSUCCESSFUL
;
4143 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
4145 Status
= IopInitiatePnpIrp(DeviceObject
,
4147 IRP_MN_QUERY_DEVICE_RELATIONS
,
4149 if (!NT_SUCCESS(Status
))
4151 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4152 DeviceRelations
= NULL
;
4156 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4159 if (DeviceRelations
)
4161 Status
= IopQueryRemoveDeviceRelations(DeviceRelations
);
4162 if (!NT_SUCCESS(Status
))
4166 Status
= IopQueryRemoveChildDevices(DeviceNode
);
4167 if (!NT_SUCCESS(Status
))
4169 if (DeviceRelations
)
4170 IopCancelRemoveDeviceRelations(DeviceRelations
);
4174 if (DeviceRelations
)
4175 IopSendRemoveDeviceRelations(DeviceRelations
);
4176 IopSendRemoveChildDevices(DeviceNode
);
4178 return STATUS_SUCCESS
;
4182 IopRemoveDevice(PDEVICE_NODE DeviceNode
)
4186 DPRINT("Removing device: %wZ\n", &DeviceNode
->InstancePath
);
4188 Status
= IopPrepareDeviceForRemoval(DeviceNode
->PhysicalDeviceObject
);
4189 if (NT_SUCCESS(Status
))
4191 IopSendRemoveDevice(DeviceNode
->PhysicalDeviceObject
);
4192 IopQueueTargetDeviceEvent(&GUID_DEVICE_SAFE_REMOVAL
,
4193 &DeviceNode
->InstancePath
);
4194 DeviceNode
->Flags
|= DNF_WILL_BE_REMOVED
;
4195 return STATUS_SUCCESS
;
4206 IoRequestDeviceEject(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4208 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(PhysicalDeviceObject
);
4209 PDEVICE_RELATIONS DeviceRelations
;
4210 IO_STATUS_BLOCK IoStatusBlock
;
4211 IO_STACK_LOCATION Stack
;
4212 DEVICE_CAPABILITIES Capabilities
;
4215 IopQueueTargetDeviceEvent(&GUID_DEVICE_KERNEL_INITIATED_EJECT
,
4216 &DeviceNode
->InstancePath
);
4218 if (IopQueryDeviceCapabilities(DeviceNode
, &Capabilities
) != STATUS_SUCCESS
)
4223 Stack
.Parameters
.QueryDeviceRelations
.Type
= EjectionRelations
;
4225 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
4227 IRP_MN_QUERY_DEVICE_RELATIONS
,
4229 if (!NT_SUCCESS(Status
))
4231 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4232 DeviceRelations
= NULL
;
4236 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4239 if (DeviceRelations
)
4241 Status
= IopQueryRemoveDeviceRelations(DeviceRelations
);
4242 if (!NT_SUCCESS(Status
))
4246 Status
= IopQueryRemoveChildDevices(DeviceNode
);
4247 if (!NT_SUCCESS(Status
))
4249 if (DeviceRelations
)
4250 IopCancelRemoveDeviceRelations(DeviceRelations
);
4254 if (IopPrepareDeviceForRemoval(PhysicalDeviceObject
) != STATUS_SUCCESS
)
4256 if (DeviceRelations
)
4257 IopCancelRemoveDeviceRelations(DeviceRelations
);
4258 IopCancelRemoveChildDevices(DeviceNode
);
4262 if (DeviceRelations
)
4263 IopSendRemoveDeviceRelations(DeviceRelations
);
4264 IopSendRemoveChildDevices(DeviceNode
);
4266 if (Capabilities
.EjectSupported
)
4268 if (IopSendEject(PhysicalDeviceObject
) != STATUS_SUCCESS
)
4275 DeviceNode
->Flags
|= DNF_DISABLED
;
4278 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT
,
4279 &DeviceNode
->InstancePath
);
4284 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT_VETOED
,
4285 &DeviceNode
->InstancePath
);
4293 IoInvalidateDeviceRelations(
4294 IN PDEVICE_OBJECT DeviceObject
,
4295 IN DEVICE_RELATION_TYPE Type
)
4297 PIO_WORKITEM WorkItem
;
4298 PINVALIDATE_DEVICE_RELATION_DATA Data
;
4300 Data
= ExAllocatePool(NonPagedPool
, sizeof(INVALIDATE_DEVICE_RELATION_DATA
));
4303 WorkItem
= IoAllocateWorkItem(DeviceObject
);
4310 ObReferenceObject(DeviceObject
);
4311 Data
->DeviceObject
= DeviceObject
;
4313 Data
->WorkItem
= WorkItem
;
4317 IopAsynchronousInvalidateDeviceRelations
,
4327 IoSynchronousInvalidateDeviceRelations(
4328 IN PDEVICE_OBJECT DeviceObject
,
4329 IN DEVICE_RELATION_TYPE Type
)
4336 /* Enumerate the device */
4337 return IopEnumerateDevice(DeviceObject
);
4338 case PowerRelations
:
4339 /* Not handled yet */
4340 return STATUS_NOT_IMPLEMENTED
;
4341 case TargetDeviceRelation
:
4343 return STATUS_SUCCESS
;
4345 /* Ejection relations are not supported */
4346 return STATUS_NOT_SUPPORTED
;
4355 IoTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
4357 IN PHYSICAL_ADDRESS BusAddress
,
4358 IN OUT PULONG AddressSpace
,
4359 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
4361 /* FIXME: Notify the resource arbiter */
4363 return HalTranslateBusAddress(InterfaceType
,