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 ERESOURCE IopDriverLoadResource
;
25 extern ULONG ExpInitializationPhase
;
26 extern BOOLEAN PnpSystemInit
;
28 #define MAX_DEVICE_ID_LEN 200
29 #define MAX_SEPARATORS_INSTANCEID 0
30 #define MAX_SEPARATORS_DEVICEID 1
32 /* DATA **********************************************************************/
34 PDRIVER_OBJECT IopRootDriverObject
;
35 PIO_BUS_TYPE_GUID_LIST PnpBusTypeGuidList
= NULL
;
36 LIST_ENTRY IopDeviceActionRequestList
;
37 WORK_QUEUE_ITEM IopDeviceActionWorkItem
;
38 BOOLEAN IopDeviceActionInProgress
;
39 KSPIN_LOCK IopDeviceActionLock
;
41 typedef struct _DEVICE_ACTION_DATA
43 LIST_ENTRY RequestListEntry
;
44 PDEVICE_OBJECT DeviceObject
;
45 DEVICE_RELATION_TYPE Type
;
46 } DEVICE_ACTION_DATA
, *PDEVICE_ACTION_DATA
;
48 /* FUNCTIONS *****************************************************************/
51 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
52 IN ULONG CreateOptions
,
56 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
);
59 IopPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
, BOOLEAN Force
);
62 IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance
);
66 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject
)
68 return ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
72 IopFixupDeviceId(PWCHAR String
)
74 SIZE_T Length
= wcslen(String
), i
;
76 for (i
= 0; i
< Length
; i
++)
78 if (String
[i
] == L
'\\')
85 IopInstallCriticalDevice(PDEVICE_NODE DeviceNode
)
88 HANDLE CriticalDeviceKey
, InstanceKey
;
89 OBJECT_ATTRIBUTES ObjectAttributes
;
90 UNICODE_STRING CriticalDeviceKeyU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\CriticalDeviceDatabase");
91 UNICODE_STRING CompatibleIdU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
92 UNICODE_STRING HardwareIdU
= RTL_CONSTANT_STRING(L
"HardwareID");
93 UNICODE_STRING ServiceU
= RTL_CONSTANT_STRING(L
"Service");
94 UNICODE_STRING ClassGuidU
= RTL_CONSTANT_STRING(L
"ClassGUID");
95 PKEY_VALUE_PARTIAL_INFORMATION PartialInfo
;
96 ULONG HidLength
= 0, CidLength
= 0, BufferLength
;
97 PWCHAR IdBuffer
, OriginalIdBuffer
;
99 /* Open the device instance key */
100 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_NON_VOLATILE
, &InstanceKey
);
101 if (Status
!= STATUS_SUCCESS
)
104 Status
= ZwQueryValueKey(InstanceKey
,
106 KeyValuePartialInformation
,
110 if (Status
!= STATUS_BUFFER_OVERFLOW
&& Status
!= STATUS_BUFFER_TOO_SMALL
)
112 ZwClose(InstanceKey
);
116 Status
= ZwQueryValueKey(InstanceKey
,
118 KeyValuePartialInformation
,
122 if (Status
!= STATUS_BUFFER_OVERFLOW
&& Status
!= STATUS_BUFFER_TOO_SMALL
)
127 BufferLength
= HidLength
+ CidLength
;
128 BufferLength
-= (((CidLength
!= 0) ? 2 : 1) * FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
));
130 /* Allocate a buffer to hold data from both */
131 OriginalIdBuffer
= IdBuffer
= ExAllocatePool(PagedPool
, BufferLength
);
134 ZwClose(InstanceKey
);
138 /* Compute the buffer size */
139 if (HidLength
> CidLength
)
140 BufferLength
= HidLength
;
142 BufferLength
= CidLength
;
144 PartialInfo
= ExAllocatePool(PagedPool
, BufferLength
);
147 ZwClose(InstanceKey
);
148 ExFreePool(OriginalIdBuffer
);
152 Status
= ZwQueryValueKey(InstanceKey
,
154 KeyValuePartialInformation
,
158 if (Status
!= STATUS_SUCCESS
)
160 ExFreePool(PartialInfo
);
161 ExFreePool(OriginalIdBuffer
);
162 ZwClose(InstanceKey
);
166 /* Copy in HID info first (without 2nd terminating NULL if CID is present) */
167 HidLength
= PartialInfo
->DataLength
- ((CidLength
!= 0) ? sizeof(WCHAR
) : 0);
168 RtlCopyMemory(IdBuffer
, PartialInfo
->Data
, HidLength
);
172 Status
= ZwQueryValueKey(InstanceKey
,
174 KeyValuePartialInformation
,
178 if (Status
!= STATUS_SUCCESS
)
180 ExFreePool(PartialInfo
);
181 ExFreePool(OriginalIdBuffer
);
182 ZwClose(InstanceKey
);
187 CidLength
= PartialInfo
->DataLength
;
188 RtlCopyMemory(((PUCHAR
)IdBuffer
) + HidLength
, PartialInfo
->Data
, CidLength
);
191 /* Free our temp buffer */
192 ExFreePool(PartialInfo
);
194 InitializeObjectAttributes(&ObjectAttributes
,
196 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
199 Status
= ZwOpenKey(&CriticalDeviceKey
,
200 KEY_ENUMERATE_SUB_KEYS
,
202 if (!NT_SUCCESS(Status
))
204 /* The critical device database doesn't exist because
205 * we're probably in 1st stage setup, but it's ok */
206 ExFreePool(OriginalIdBuffer
);
207 ZwClose(InstanceKey
);
213 USHORT StringLength
= (USHORT
)wcslen(IdBuffer
) + 1, Index
;
215 IopFixupDeviceId(IdBuffer
);
217 /* Look through all subkeys for a match */
218 for (Index
= 0; TRUE
; Index
++)
221 PKEY_BASIC_INFORMATION BasicInfo
;
223 Status
= ZwEnumerateKey(CriticalDeviceKey
,
229 if (Status
== STATUS_NO_MORE_ENTRIES
)
231 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
233 UNICODE_STRING ChildIdNameU
, RegKeyNameU
;
235 BasicInfo
= ExAllocatePool(PagedPool
, NeededLength
);
239 ExFreePool(OriginalIdBuffer
);
240 ZwClose(CriticalDeviceKey
);
241 ZwClose(InstanceKey
);
245 Status
= ZwEnumerateKey(CriticalDeviceKey
,
251 if (Status
!= STATUS_SUCCESS
)
253 /* This shouldn't happen */
254 ExFreePool(BasicInfo
);
258 ChildIdNameU
.Buffer
= IdBuffer
;
259 ChildIdNameU
.MaximumLength
= ChildIdNameU
.Length
= (StringLength
- 1) * sizeof(WCHAR
);
260 RegKeyNameU
.Buffer
= BasicInfo
->Name
;
261 RegKeyNameU
.MaximumLength
= RegKeyNameU
.Length
= (USHORT
)BasicInfo
->NameLength
;
263 if (RtlEqualUnicodeString(&ChildIdNameU
, &RegKeyNameU
, TRUE
))
265 HANDLE ChildKeyHandle
;
267 InitializeObjectAttributes(&ObjectAttributes
,
269 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
273 Status
= ZwOpenKey(&ChildKeyHandle
,
276 if (Status
!= STATUS_SUCCESS
)
278 ExFreePool(BasicInfo
);
282 /* Check if there's already a driver installed */
283 Status
= ZwQueryValueKey(InstanceKey
,
285 KeyValuePartialInformation
,
289 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
291 ExFreePool(BasicInfo
);
295 Status
= ZwQueryValueKey(ChildKeyHandle
,
297 KeyValuePartialInformation
,
301 if (Status
!= STATUS_BUFFER_OVERFLOW
&& Status
!= STATUS_BUFFER_TOO_SMALL
)
303 ExFreePool(BasicInfo
);
307 PartialInfo
= ExAllocatePool(PagedPool
, NeededLength
);
310 ExFreePool(OriginalIdBuffer
);
311 ExFreePool(BasicInfo
);
312 ZwClose(InstanceKey
);
313 ZwClose(ChildKeyHandle
);
314 ZwClose(CriticalDeviceKey
);
318 /* Read ClassGUID entry in the CDDB */
319 Status
= ZwQueryValueKey(ChildKeyHandle
,
321 KeyValuePartialInformation
,
325 if (Status
!= STATUS_SUCCESS
)
327 ExFreePool(BasicInfo
);
331 /* Write it to the ENUM key */
332 Status
= ZwSetValueKey(InstanceKey
,
337 PartialInfo
->DataLength
);
338 if (Status
!= STATUS_SUCCESS
)
340 ExFreePool(BasicInfo
);
341 ExFreePool(PartialInfo
);
342 ZwClose(ChildKeyHandle
);
346 Status
= ZwQueryValueKey(ChildKeyHandle
,
348 KeyValuePartialInformation
,
352 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
354 ExFreePool(PartialInfo
);
355 PartialInfo
= ExAllocatePool(PagedPool
, NeededLength
);
358 ExFreePool(OriginalIdBuffer
);
359 ExFreePool(BasicInfo
);
360 ZwClose(InstanceKey
);
361 ZwClose(ChildKeyHandle
);
362 ZwClose(CriticalDeviceKey
);
366 /* Read the service entry from the CDDB */
367 Status
= ZwQueryValueKey(ChildKeyHandle
,
369 KeyValuePartialInformation
,
373 if (Status
!= STATUS_SUCCESS
)
375 ExFreePool(BasicInfo
);
376 ExFreePool(PartialInfo
);
377 ZwClose(ChildKeyHandle
);
381 /* Write it to the ENUM key */
382 Status
= ZwSetValueKey(InstanceKey
,
387 PartialInfo
->DataLength
);
388 if (Status
!= STATUS_SUCCESS
)
390 ExFreePool(BasicInfo
);
391 ExFreePool(PartialInfo
);
392 ZwClose(ChildKeyHandle
);
396 DPRINT("Installed service '%S' for critical device '%wZ'\n", PartialInfo
->Data
, &ChildIdNameU
);
400 DPRINT1("Installed NULL service for critical device '%wZ'\n", &ChildIdNameU
);
403 ExFreePool(OriginalIdBuffer
);
404 ExFreePool(PartialInfo
);
405 ExFreePool(BasicInfo
);
406 ZwClose(InstanceKey
);
407 ZwClose(ChildKeyHandle
);
408 ZwClose(CriticalDeviceKey
);
414 ExFreePool(BasicInfo
);
418 /* Umm, not sure what happened here */
423 /* Advance to the next ID */
424 IdBuffer
+= StringLength
;
427 ExFreePool(OriginalIdBuffer
);
428 ZwClose(InstanceKey
);
429 ZwClose(CriticalDeviceKey
);
434 IopInitializeDevice(PDEVICE_NODE DeviceNode
,
435 PDRIVER_OBJECT DriverObject
)
442 /* Special case for bus driven devices */
443 DeviceNode
->Flags
|= DNF_ADDED
;
444 return STATUS_SUCCESS
;
447 if (!DriverObject
->DriverExtension
->AddDevice
)
449 DeviceNode
->Flags
|= DNF_LEGACY_DRIVER
;
452 if (DeviceNode
->Flags
& DNF_LEGACY_DRIVER
)
454 DeviceNode
->Flags
|= (DNF_ADDED
| DNF_STARTED
);
455 return STATUS_SUCCESS
;
458 /* This is a Plug and Play driver */
459 DPRINT("Plug and Play driver found\n");
460 ASSERT(DeviceNode
->PhysicalDeviceObject
);
462 DPRINT("Calling %wZ->AddDevice(%wZ)\n",
463 &DriverObject
->DriverName
,
464 &DeviceNode
->InstancePath
);
465 Status
= DriverObject
->DriverExtension
->AddDevice(DriverObject
,
466 DeviceNode
->PhysicalDeviceObject
);
467 if (!NT_SUCCESS(Status
))
469 DPRINT1("%wZ->AddDevice(%wZ) failed with status 0x%x\n",
470 &DriverObject
->DriverName
,
471 &DeviceNode
->InstancePath
,
473 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
474 DeviceNode
->Problem
= CM_PROB_FAILED_ADD
;
478 Fdo
= IoGetAttachedDeviceReference(DeviceNode
->PhysicalDeviceObject
);
480 /* Check if we have a ACPI device (needed for power management) */
481 if (Fdo
->DeviceType
== FILE_DEVICE_ACPI
)
483 static BOOLEAN SystemPowerDeviceNodeCreated
= FALSE
;
485 /* There can be only one system power device */
486 if (!SystemPowerDeviceNodeCreated
)
488 PopSystemPowerDeviceNode
= DeviceNode
;
489 ObReferenceObject(PopSystemPowerDeviceNode
->PhysicalDeviceObject
);
490 SystemPowerDeviceNodeCreated
= TRUE
;
494 ObDereferenceObject(Fdo
);
496 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
498 return STATUS_SUCCESS
;
504 IopSendEject(IN PDEVICE_OBJECT DeviceObject
)
506 IO_STACK_LOCATION Stack
;
509 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
510 Stack
.MajorFunction
= IRP_MJ_PNP
;
511 Stack
.MinorFunction
= IRP_MN_EJECT
;
513 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
519 IopSendSurpriseRemoval(IN PDEVICE_OBJECT DeviceObject
)
521 IO_STACK_LOCATION Stack
;
524 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
525 Stack
.MajorFunction
= IRP_MJ_PNP
;
526 Stack
.MinorFunction
= IRP_MN_SURPRISE_REMOVAL
;
528 /* Drivers should never fail a IRP_MN_SURPRISE_REMOVAL request */
529 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
535 IopQueryRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
537 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
538 IO_STACK_LOCATION Stack
;
544 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVE_PENDING
,
545 &DeviceNode
->InstancePath
);
547 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
548 Stack
.MajorFunction
= IRP_MJ_PNP
;
549 Stack
.MinorFunction
= IRP_MN_QUERY_REMOVE_DEVICE
;
551 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
553 IopNotifyPlugPlayNotification(DeviceObject
,
554 EventCategoryTargetDeviceChange
,
555 &GUID_TARGET_DEVICE_QUERY_REMOVE
,
559 if (!NT_SUCCESS(Status
))
561 DPRINT1("Removal vetoed by %wZ\n", &DeviceNode
->InstancePath
);
562 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVAL_VETOED
,
563 &DeviceNode
->InstancePath
);
572 IopQueryStopDevice(IN PDEVICE_OBJECT DeviceObject
)
574 IO_STACK_LOCATION Stack
;
577 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
578 Stack
.MajorFunction
= IRP_MJ_PNP
;
579 Stack
.MinorFunction
= IRP_MN_QUERY_STOP_DEVICE
;
581 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
587 IopSendRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
589 IO_STACK_LOCATION Stack
;
591 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
593 /* Drop all our state for this device in case it isn't really going away */
594 DeviceNode
->Flags
&= DNF_ENUMERATED
| DNF_PROCESSED
;
596 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
597 Stack
.MajorFunction
= IRP_MJ_PNP
;
598 Stack
.MinorFunction
= IRP_MN_REMOVE_DEVICE
;
600 /* Drivers should never fail a IRP_MN_REMOVE_DEVICE request */
601 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
603 IopNotifyPlugPlayNotification(DeviceObject
,
604 EventCategoryTargetDeviceChange
,
605 &GUID_TARGET_DEVICE_REMOVE_COMPLETE
,
608 ObDereferenceObject(DeviceObject
);
614 IopCancelRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
616 IO_STACK_LOCATION Stack
;
619 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
620 Stack
.MajorFunction
= IRP_MJ_PNP
;
621 Stack
.MinorFunction
= IRP_MN_CANCEL_REMOVE_DEVICE
;
623 /* Drivers should never fail a IRP_MN_CANCEL_REMOVE_DEVICE request */
624 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
626 IopNotifyPlugPlayNotification(DeviceObject
,
627 EventCategoryTargetDeviceChange
,
628 &GUID_TARGET_DEVICE_REMOVE_CANCELLED
,
636 IopSendStopDevice(IN PDEVICE_OBJECT DeviceObject
)
638 IO_STACK_LOCATION Stack
;
641 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
642 Stack
.MajorFunction
= IRP_MJ_PNP
;
643 Stack
.MinorFunction
= IRP_MN_STOP_DEVICE
;
645 /* Drivers should never fail a IRP_MN_STOP_DEVICE request */
646 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
651 IopSetServiceEnumData(PDEVICE_NODE DeviceNode
)
653 UNICODE_STRING ServicesKeyPath
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
654 UNICODE_STRING ServiceKeyName
;
655 UNICODE_STRING EnumKeyName
;
656 UNICODE_STRING ValueName
;
657 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
658 HANDLE ServiceKey
= NULL
, ServiceEnumKey
;
660 ULONG Count
= 0, NextInstance
= 0;
661 WCHAR ValueBuffer
[6];
662 NTSTATUS Status
= STATUS_SUCCESS
;
664 DPRINT("IopSetServiceEnumData(%p)\n", DeviceNode
);
665 DPRINT("Instance: %wZ\n", &DeviceNode
->InstancePath
);
666 DPRINT("Service: %wZ\n", &DeviceNode
->ServiceName
);
668 if (DeviceNode
->ServiceName
.Buffer
== NULL
)
670 DPRINT1("No service!\n");
671 return STATUS_SUCCESS
;
674 ServiceKeyName
.MaximumLength
= ServicesKeyPath
.Length
+ DeviceNode
->ServiceName
.Length
+ sizeof(UNICODE_NULL
);
675 ServiceKeyName
.Length
= 0;
676 ServiceKeyName
.Buffer
= ExAllocatePool(PagedPool
, ServiceKeyName
.MaximumLength
);
677 if (ServiceKeyName
.Buffer
== NULL
)
679 DPRINT1("No ServiceKeyName.Buffer!\n");
680 return STATUS_INSUFFICIENT_RESOURCES
;
683 RtlAppendUnicodeStringToString(&ServiceKeyName
, &ServicesKeyPath
);
684 RtlAppendUnicodeStringToString(&ServiceKeyName
, &DeviceNode
->ServiceName
);
686 DPRINT("ServiceKeyName: %wZ\n", &ServiceKeyName
);
688 Status
= IopOpenRegistryKeyEx(&ServiceKey
, NULL
, &ServiceKeyName
, KEY_CREATE_SUB_KEY
);
689 if (!NT_SUCCESS(Status
))
694 RtlInitUnicodeString(&EnumKeyName
, L
"Enum");
695 Status
= IopCreateRegistryKeyEx(&ServiceEnumKey
,
701 if (NT_SUCCESS(Status
))
703 if (Disposition
== REG_OPENED_EXISTING_KEY
)
705 /* Read the NextInstance value */
706 Status
= IopGetRegistryValue(ServiceEnumKey
,
708 &KeyValueInformation
);
709 if (!NT_SUCCESS(Status
))
712 if ((KeyValueInformation
->Type
== REG_DWORD
) &&
713 (KeyValueInformation
->DataLength
))
716 Count
= *(PULONG
)((ULONG_PTR
)KeyValueInformation
+
717 KeyValueInformation
->DataOffset
);
720 ExFreePool(KeyValueInformation
);
721 KeyValueInformation
= NULL
;
723 /* Read the NextInstance value */
724 Status
= IopGetRegistryValue(ServiceEnumKey
,
726 &KeyValueInformation
);
727 if (!NT_SUCCESS(Status
))
730 if ((KeyValueInformation
->Type
== REG_DWORD
) &&
731 (KeyValueInformation
->DataLength
))
733 NextInstance
= *(PULONG
)((ULONG_PTR
)KeyValueInformation
+
734 KeyValueInformation
->DataOffset
);
737 ExFreePool(KeyValueInformation
);
738 KeyValueInformation
= NULL
;
741 /* Set the instance path */
742 swprintf(ValueBuffer
, L
"%lu", NextInstance
);
743 RtlInitUnicodeString(&ValueName
, ValueBuffer
);
744 Status
= ZwSetValueKey(ServiceEnumKey
,
748 DeviceNode
->InstancePath
.Buffer
,
749 DeviceNode
->InstancePath
.MaximumLength
);
750 if (!NT_SUCCESS(Status
))
753 /* Increment Count and NextInstance */
757 /* Set the new Count value */
758 RtlInitUnicodeString(&ValueName
, L
"Count");
759 Status
= ZwSetValueKey(ServiceEnumKey
,
765 if (!NT_SUCCESS(Status
))
768 /* Set the new NextInstance value */
769 RtlInitUnicodeString(&ValueName
, L
"NextInstance");
770 Status
= ZwSetValueKey(ServiceEnumKey
,
775 sizeof(NextInstance
));
779 if (ServiceEnumKey
!= NULL
)
780 ZwClose(ServiceEnumKey
);
782 if (ServiceKey
!= NULL
)
785 ExFreePool(ServiceKeyName
.Buffer
);
792 IopStartDevice2(IN PDEVICE_OBJECT DeviceObject
)
794 IO_STACK_LOCATION Stack
;
795 PDEVICE_NODE DeviceNode
;
798 DEVICE_CAPABILITIES DeviceCapabilities
;
800 /* Get the device node */
801 DeviceNode
= IopGetDeviceNode(DeviceObject
);
803 ASSERT(!(DeviceNode
->Flags
& DNF_DISABLED
));
805 /* Build the I/O stack location */
806 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
807 Stack
.MajorFunction
= IRP_MJ_PNP
;
808 Stack
.MinorFunction
= IRP_MN_START_DEVICE
;
810 Stack
.Parameters
.StartDevice
.AllocatedResources
=
811 DeviceNode
->ResourceList
;
812 Stack
.Parameters
.StartDevice
.AllocatedResourcesTranslated
=
813 DeviceNode
->ResourceListTranslated
;
816 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
817 if (!NT_SUCCESS(Status
))
819 /* Send an IRP_MN_REMOVE_DEVICE request */
820 IopRemoveDevice(DeviceNode
);
822 /* Set the appropriate flag */
823 DeviceNode
->Flags
|= DNF_START_FAILED
;
824 DeviceNode
->Problem
= CM_PROB_FAILED_START
;
826 DPRINT1("Warning: PnP Start failed (%wZ) [Status: 0x%x]\n", &DeviceNode
->InstancePath
, Status
);
830 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after start)\n");
832 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
833 if (!NT_SUCCESS(Status
))
835 DPRINT("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
838 /* Invalidate device state so IRP_MN_QUERY_PNP_DEVICE_STATE is sent */
839 IoInvalidateDeviceState(DeviceObject
);
841 /* Otherwise, mark us as started */
842 DeviceNode
->Flags
|= DNF_STARTED
;
843 DeviceNode
->Flags
&= ~DNF_STOPPED
;
845 /* We now need enumeration */
846 DeviceNode
->Flags
|= DNF_NEED_ENUMERATION_ONLY
;
851 IopStartAndEnumerateDevice(IN PDEVICE_NODE DeviceNode
)
853 PDEVICE_OBJECT DeviceObject
;
858 ASSERT((DeviceNode
->Flags
& DNF_ADDED
));
859 ASSERT((DeviceNode
->Flags
& (DNF_RESOURCE_ASSIGNED
|
860 DNF_RESOURCE_REPORTED
|
861 DNF_NO_RESOURCE_REQUIRED
)));
863 /* Get the device object */
864 DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
866 /* Check if we're not started yet */
867 if (!(DeviceNode
->Flags
& DNF_STARTED
))
870 IopStartDevice2(DeviceObject
);
873 /* Do we need to query IDs? This happens in the case of manual reporting */
875 if (DeviceNode
->Flags
& DNF_NEED_QUERY_IDS
)
877 DPRINT1("Warning: Device node has DNF_NEED_QUERY_IDS\n");
878 /* And that case shouldn't happen yet */
883 IopSetServiceEnumData(DeviceNode
);
885 /* Make sure we're started, and check if we need enumeration */
886 if ((DeviceNode
->Flags
& DNF_STARTED
) &&
887 (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
))
890 IoSynchronousInvalidateDeviceRelations(DeviceObject
, BusRelations
);
891 Status
= STATUS_SUCCESS
;
896 Status
= STATUS_SUCCESS
;
905 PDEVICE_NODE DeviceNode
)
909 DPRINT("Stopping device: %wZ\n", &DeviceNode
->InstancePath
);
911 Status
= IopQueryStopDevice(DeviceNode
->PhysicalDeviceObject
);
912 if (NT_SUCCESS(Status
))
914 IopSendStopDevice(DeviceNode
->PhysicalDeviceObject
);
916 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
917 DeviceNode
->Flags
|= DNF_STOPPED
;
919 return STATUS_SUCCESS
;
927 PDEVICE_NODE DeviceNode
)
930 HANDLE InstanceHandle
= NULL
, ControlHandle
= NULL
;
931 UNICODE_STRING KeyName
, ValueString
;
932 OBJECT_ATTRIBUTES ObjectAttributes
;
934 if (DeviceNode
->Flags
& DNF_DISABLED
)
935 return STATUS_SUCCESS
;
937 Status
= IopAssignDeviceResources(DeviceNode
);
938 if (!NT_SUCCESS(Status
))
942 IopStartAndEnumerateDevice(DeviceNode
);
944 /* FIX: Should be done in new device instance code */
945 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_NON_VOLATILE
, &InstanceHandle
);
946 if (!NT_SUCCESS(Status
))
949 /* FIX: Should be done in IoXxxPrepareDriverLoading */
951 RtlInitUnicodeString(&KeyName
, L
"Control");
952 InitializeObjectAttributes(&ObjectAttributes
,
954 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
957 Status
= ZwCreateKey(&ControlHandle
,
964 if (!NT_SUCCESS(Status
))
967 RtlInitUnicodeString(&KeyName
, L
"ActiveService");
968 ValueString
= DeviceNode
->ServiceName
;
969 if (!ValueString
.Buffer
)
970 RtlInitUnicodeString(&ValueString
, L
"");
971 Status
= ZwSetValueKey(ControlHandle
, &KeyName
, 0, REG_SZ
, ValueString
.Buffer
, ValueString
.Length
+ sizeof(UNICODE_NULL
));
975 if (ControlHandle
!= NULL
)
976 ZwClose(ControlHandle
);
978 if (InstanceHandle
!= NULL
)
979 ZwClose(InstanceHandle
);
986 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode
,
987 PDEVICE_CAPABILITIES DeviceCaps
)
989 IO_STATUS_BLOCK StatusBlock
;
990 IO_STACK_LOCATION Stack
;
993 UNICODE_STRING ValueName
;
995 /* Set up the Header */
996 RtlZeroMemory(DeviceCaps
, sizeof(DEVICE_CAPABILITIES
));
997 DeviceCaps
->Size
= sizeof(DEVICE_CAPABILITIES
);
998 DeviceCaps
->Version
= 1;
999 DeviceCaps
->Address
= -1;
1000 DeviceCaps
->UINumber
= -1;
1002 /* Set up the Stack */
1003 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
1004 Stack
.Parameters
.DeviceCapabilities
.Capabilities
= DeviceCaps
;
1007 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1009 IRP_MN_QUERY_CAPABILITIES
,
1011 if (!NT_SUCCESS(Status
))
1013 if (Status
!= STATUS_NOT_SUPPORTED
)
1015 DPRINT1("IRP_MN_QUERY_CAPABILITIES failed with status 0x%lx\n", Status
);
1020 DeviceNode
->CapabilityFlags
= *(PULONG
)((ULONG_PTR
)&DeviceCaps
->Version
+ sizeof(DeviceCaps
->Version
));
1022 if (DeviceCaps
->NoDisplayInUI
)
1023 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
1025 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
1027 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_NON_VOLATILE
, &InstanceKey
);
1028 if (NT_SUCCESS(Status
))
1030 /* Set 'Capabilities' value */
1031 RtlInitUnicodeString(&ValueName
, L
"Capabilities");
1032 Status
= ZwSetValueKey(InstanceKey
,
1036 &DeviceNode
->CapabilityFlags
,
1039 /* Set 'UINumber' value */
1040 if (DeviceCaps
->UINumber
!= MAXULONG
)
1042 RtlInitUnicodeString(&ValueName
, L
"UINumber");
1043 Status
= ZwSetValueKey(InstanceKey
,
1047 &DeviceCaps
->UINumber
,
1051 ZwClose(InstanceKey
);
1060 IopDeviceActionWorker(
1063 PLIST_ENTRY ListEntry
;
1064 PDEVICE_ACTION_DATA Data
;
1067 KeAcquireSpinLock(&IopDeviceActionLock
, &OldIrql
);
1068 while (!IsListEmpty(&IopDeviceActionRequestList
))
1070 ListEntry
= RemoveHeadList(&IopDeviceActionRequestList
);
1071 KeReleaseSpinLock(&IopDeviceActionLock
, OldIrql
);
1072 Data
= CONTAINING_RECORD(ListEntry
,
1076 IoSynchronousInvalidateDeviceRelations(Data
->DeviceObject
,
1079 ObDereferenceObject(Data
->DeviceObject
);
1080 ExFreePoolWithTag(Data
, TAG_IO
);
1081 KeAcquireSpinLock(&IopDeviceActionLock
, &OldIrql
);
1083 IopDeviceActionInProgress
= FALSE
;
1084 KeReleaseSpinLock(&IopDeviceActionLock
, OldIrql
);
1088 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT
*DeviceObject
)
1092 if (PopSystemPowerDeviceNode
)
1094 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
1095 *DeviceObject
= PopSystemPowerDeviceNode
->PhysicalDeviceObject
;
1096 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
1098 return STATUS_SUCCESS
;
1101 return STATUS_UNSUCCESSFUL
;
1106 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid
)
1108 USHORT i
= 0, FoundIndex
= 0xFFFF;
1112 /* Acquire the lock */
1113 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
1115 /* Loop all entries */
1116 while (i
< PnpBusTypeGuidList
->GuidCount
)
1118 /* Try to find a match */
1119 if (RtlCompareMemory(BusTypeGuid
,
1120 &PnpBusTypeGuidList
->Guids
[i
],
1121 sizeof(GUID
)) == sizeof(GUID
))
1130 /* Check if we have to grow the list */
1131 if (PnpBusTypeGuidList
->GuidCount
)
1133 /* Calculate the new size */
1134 NewSize
= sizeof(IO_BUS_TYPE_GUID_LIST
) +
1135 (sizeof(GUID
) * PnpBusTypeGuidList
->GuidCount
);
1137 /* Allocate the new copy */
1138 NewList
= ExAllocatePool(PagedPool
, NewSize
);
1143 ExFreePool(PnpBusTypeGuidList
);
1147 /* Now copy them, decrease the size too */
1148 NewSize
-= sizeof(GUID
);
1149 RtlCopyMemory(NewList
, PnpBusTypeGuidList
, NewSize
);
1151 /* Free the old list */
1152 ExFreePool(PnpBusTypeGuidList
);
1154 /* Use the new buffer */
1155 PnpBusTypeGuidList
= NewList
;
1158 /* Copy the new GUID */
1159 RtlCopyMemory(&PnpBusTypeGuidList
->Guids
[PnpBusTypeGuidList
->GuidCount
],
1163 /* The new entry is the index */
1164 FoundIndex
= (USHORT
)PnpBusTypeGuidList
->GuidCount
;
1165 PnpBusTypeGuidList
->GuidCount
++;
1168 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
1174 * Creates a device node
1177 * ParentNode = Pointer to parent device node
1178 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
1179 * to have the root device node create one
1180 * (eg. for legacy drivers)
1181 * DeviceNode = Pointer to storage for created device node
1187 IopCreateDeviceNode(PDEVICE_NODE ParentNode
,
1188 PDEVICE_OBJECT PhysicalDeviceObject
,
1189 PUNICODE_STRING ServiceName
,
1190 PDEVICE_NODE
*DeviceNode
)
1195 UNICODE_STRING FullServiceName
;
1196 UNICODE_STRING LegacyPrefix
= RTL_CONSTANT_STRING(L
"LEGACY_");
1197 UNICODE_STRING UnknownDeviceName
= RTL_CONSTANT_STRING(L
"UNKNOWN");
1198 UNICODE_STRING KeyName
, ClassName
;
1199 PUNICODE_STRING ServiceName1
;
1201 UNICODE_STRING ClassGUID
;
1202 HANDLE InstanceHandle
;
1204 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p ServiceName %wZ\n",
1205 ParentNode
, PhysicalDeviceObject
, ServiceName
);
1207 Node
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(DEVICE_NODE
), TAG_IO_DEVNODE
);
1210 return STATUS_INSUFFICIENT_RESOURCES
;
1213 RtlZeroMemory(Node
, sizeof(DEVICE_NODE
));
1216 ServiceName1
= &UnknownDeviceName
;
1218 ServiceName1
= ServiceName
;
1220 if (!PhysicalDeviceObject
)
1222 FullServiceName
.MaximumLength
= LegacyPrefix
.Length
+ ServiceName1
->Length
+ sizeof(UNICODE_NULL
);
1223 FullServiceName
.Length
= 0;
1224 FullServiceName
.Buffer
= ExAllocatePool(PagedPool
, FullServiceName
.MaximumLength
);
1225 if (!FullServiceName
.Buffer
)
1227 ExFreePoolWithTag(Node
, TAG_IO_DEVNODE
);
1228 return STATUS_INSUFFICIENT_RESOURCES
;
1231 RtlAppendUnicodeStringToString(&FullServiceName
, &LegacyPrefix
);
1232 RtlAppendUnicodeStringToString(&FullServiceName
, ServiceName1
);
1233 RtlUpcaseUnicodeString(&FullServiceName
, &FullServiceName
, FALSE
);
1235 Status
= PnpRootCreateDevice(&FullServiceName
, NULL
, &PhysicalDeviceObject
, &Node
->InstancePath
);
1236 if (!NT_SUCCESS(Status
))
1238 DPRINT1("PnpRootCreateDevice() failed with status 0x%08X\n", Status
);
1239 ExFreePool(FullServiceName
.Buffer
);
1240 ExFreePoolWithTag(Node
, TAG_IO_DEVNODE
);
1244 /* Create the device key for legacy drivers */
1245 Status
= IopCreateDeviceKeyPath(&Node
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceHandle
);
1246 if (!NT_SUCCESS(Status
))
1248 ExFreePool(FullServiceName
.Buffer
);
1249 ExFreePoolWithTag(Node
, TAG_IO_DEVNODE
);
1253 Node
->ServiceName
.MaximumLength
= ServiceName1
->Length
+ sizeof(UNICODE_NULL
);
1254 Node
->ServiceName
.Length
= 0;
1255 Node
->ServiceName
.Buffer
= ExAllocatePool(PagedPool
, Node
->ServiceName
.MaximumLength
);
1256 if (!Node
->ServiceName
.Buffer
)
1258 ZwClose(InstanceHandle
);
1259 ExFreePool(FullServiceName
.Buffer
);
1260 ExFreePoolWithTag(Node
, TAG_IO_DEVNODE
);
1264 RtlCopyUnicodeString(&Node
->ServiceName
, ServiceName1
);
1268 RtlInitUnicodeString(&KeyName
, L
"Service");
1269 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ServiceName
->Buffer
, ServiceName
->Length
+ sizeof(UNICODE_NULL
));
1272 if (NT_SUCCESS(Status
))
1274 RtlInitUnicodeString(&KeyName
, L
"Legacy");
1276 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_DWORD
, &LegacyValue
, sizeof(LegacyValue
));
1278 RtlInitUnicodeString(&KeyName
, L
"ConfigFlags");
1280 ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_DWORD
, &LegacyValue
, sizeof(LegacyValue
));
1282 if (NT_SUCCESS(Status
))
1284 RtlInitUnicodeString(&KeyName
, L
"Class");
1285 RtlInitUnicodeString(&ClassName
, L
"LegacyDriver");
1286 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassName
.Buffer
, ClassName
.Length
+ sizeof(UNICODE_NULL
));
1287 if (NT_SUCCESS(Status
))
1289 RtlInitUnicodeString(&KeyName
, L
"ClassGUID");
1290 RtlInitUnicodeString(&ClassGUID
, L
"{8ECC055D-047F-11D1-A537-0000F8753ED1}");
1291 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassGUID
.Buffer
, ClassGUID
.Length
+ sizeof(UNICODE_NULL
));
1292 if (NT_SUCCESS(Status
))
1294 // FIXME: Retrieve the real "description" by looking at the "DisplayName" string
1295 // of the corresponding CurrentControlSet\Services\xxx entry for this driver.
1296 RtlInitUnicodeString(&KeyName
, L
"DeviceDesc");
1297 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ServiceName1
->Buffer
, ServiceName1
->Length
+ sizeof(UNICODE_NULL
));
1303 ZwClose(InstanceHandle
);
1304 ExFreePool(FullServiceName
.Buffer
);
1306 if (!NT_SUCCESS(Status
))
1308 ExFreePool(Node
->ServiceName
.Buffer
);
1309 ExFreePoolWithTag(Node
, TAG_IO_DEVNODE
);
1313 IopDeviceNodeSetFlag(Node
, DNF_LEGACY_DRIVER
);
1314 IopDeviceNodeSetFlag(Node
, DNF_PROCESSED
);
1315 IopDeviceNodeSetFlag(Node
, DNF_ADDED
);
1316 IopDeviceNodeSetFlag(Node
, DNF_STARTED
);
1319 Node
->PhysicalDeviceObject
= PhysicalDeviceObject
;
1321 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= Node
;
1325 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
1326 Node
->Parent
= ParentNode
;
1327 Node
->Sibling
= NULL
;
1328 if (ParentNode
->LastChild
== NULL
)
1330 ParentNode
->Child
= Node
;
1331 ParentNode
->LastChild
= Node
;
1335 ParentNode
->LastChild
->Sibling
= Node
;
1336 ParentNode
->LastChild
= Node
;
1338 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
1339 Node
->Level
= ParentNode
->Level
+ 1;
1342 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
1346 return STATUS_SUCCESS
;
1350 IopFreeDeviceNode(PDEVICE_NODE DeviceNode
)
1353 PDEVICE_NODE PrevSibling
= NULL
;
1355 /* All children must be deleted before a parent is deleted */
1356 ASSERT(!DeviceNode
->Child
);
1357 ASSERT(DeviceNode
->PhysicalDeviceObject
);
1359 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
1361 /* Get previous sibling */
1362 if (DeviceNode
->Parent
&& DeviceNode
->Parent
->Child
!= DeviceNode
)
1364 PrevSibling
= DeviceNode
->Parent
->Child
;
1365 while (PrevSibling
->Sibling
!= DeviceNode
)
1366 PrevSibling
= PrevSibling
->Sibling
;
1369 /* Unlink from parent if it exists */
1370 if (DeviceNode
->Parent
)
1372 if (DeviceNode
->Parent
->LastChild
== DeviceNode
)
1374 DeviceNode
->Parent
->LastChild
= PrevSibling
;
1376 PrevSibling
->Sibling
= NULL
;
1378 if (DeviceNode
->Parent
->Child
== DeviceNode
)
1379 DeviceNode
->Parent
->Child
= DeviceNode
->Sibling
;
1382 /* Unlink from sibling list */
1384 PrevSibling
->Sibling
= DeviceNode
->Sibling
;
1386 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
1388 RtlFreeUnicodeString(&DeviceNode
->InstancePath
);
1390 RtlFreeUnicodeString(&DeviceNode
->ServiceName
);
1392 if (DeviceNode
->ResourceList
)
1394 ExFreePool(DeviceNode
->ResourceList
);
1397 if (DeviceNode
->ResourceListTranslated
)
1399 ExFreePool(DeviceNode
->ResourceListTranslated
);
1402 if (DeviceNode
->ResourceRequirements
)
1404 ExFreePool(DeviceNode
->ResourceRequirements
);
1407 if (DeviceNode
->BootResources
)
1409 ExFreePool(DeviceNode
->BootResources
);
1412 ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceNode
->PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= NULL
;
1413 ExFreePoolWithTag(DeviceNode
, TAG_IO_DEVNODE
);
1415 return STATUS_SUCCESS
;
1420 IopSynchronousCall(IN PDEVICE_OBJECT DeviceObject
,
1421 IN PIO_STACK_LOCATION IoStackLocation
,
1422 OUT PVOID
*Information
)
1425 PIO_STACK_LOCATION IrpStack
;
1426 IO_STATUS_BLOCK IoStatusBlock
;
1429 PDEVICE_OBJECT TopDeviceObject
;
1432 /* Call the top of the device stack */
1433 TopDeviceObject
= IoGetAttachedDeviceReference(DeviceObject
);
1435 /* Allocate an IRP */
1436 Irp
= IoAllocateIrp(TopDeviceObject
->StackSize
, FALSE
);
1437 if (!Irp
) return STATUS_INSUFFICIENT_RESOURCES
;
1439 /* Initialize to failure */
1440 Irp
->IoStatus
.Status
= IoStatusBlock
.Status
= STATUS_NOT_SUPPORTED
;
1441 Irp
->IoStatus
.Information
= IoStatusBlock
.Information
= 0;
1443 /* Special case for IRP_MN_FILTER_RESOURCE_REQUIREMENTS */
1444 if (IoStackLocation
->MinorFunction
== IRP_MN_FILTER_RESOURCE_REQUIREMENTS
)
1446 /* Copy the resource requirements list into the IOSB */
1447 Irp
->IoStatus
.Information
=
1448 IoStatusBlock
.Information
= (ULONG_PTR
)IoStackLocation
->Parameters
.FilterResourceRequirements
.IoResourceRequirementList
;
1451 /* Initialize the event */
1452 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
1455 Irp
->UserIosb
= &IoStatusBlock
;
1456 Irp
->UserEvent
= &Event
;
1459 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
1460 IoQueueThreadIrp(Irp
);
1462 /* Copy-in the stack */
1463 IrpStack
= IoGetNextIrpStackLocation(Irp
);
1464 *IrpStack
= *IoStackLocation
;
1466 /* Call the driver */
1467 Status
= IoCallDriver(TopDeviceObject
, Irp
);
1468 if (Status
== STATUS_PENDING
)
1471 KeWaitForSingleObject(&Event
,
1476 Status
= IoStatusBlock
.Status
;
1479 /* Remove the reference */
1480 ObDereferenceObject(TopDeviceObject
);
1482 /* Return the information */
1483 *Information
= (PVOID
)IoStatusBlock
.Information
;
1489 IopInitiatePnpIrp(IN PDEVICE_OBJECT DeviceObject
,
1490 IN OUT PIO_STATUS_BLOCK IoStatusBlock
,
1491 IN UCHAR MinorFunction
,
1492 IN PIO_STACK_LOCATION Stack OPTIONAL
)
1494 IO_STACK_LOCATION IoStackLocation
;
1496 /* Fill out the stack information */
1497 RtlZeroMemory(&IoStackLocation
, sizeof(IO_STACK_LOCATION
));
1498 IoStackLocation
.MajorFunction
= IRP_MJ_PNP
;
1499 IoStackLocation
.MinorFunction
= MinorFunction
;
1503 RtlCopyMemory(&IoStackLocation
.Parameters
,
1505 sizeof(Stack
->Parameters
));
1508 /* Do the PnP call */
1509 IoStatusBlock
->Status
= IopSynchronousCall(DeviceObject
,
1511 (PVOID
)&IoStatusBlock
->Information
);
1512 return IoStatusBlock
->Status
;
1516 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context
)
1518 PDEVICE_NODE ParentDeviceNode
;
1519 PDEVICE_NODE ChildDeviceNode
;
1522 /* Copy context data so we don't overwrite it in subsequent calls to this function */
1523 ParentDeviceNode
= Context
->DeviceNode
;
1525 /* Call the action routine */
1526 Status
= (Context
->Action
)(ParentDeviceNode
, Context
->Context
);
1527 if (!NT_SUCCESS(Status
))
1532 /* Traversal of all children nodes */
1533 for (ChildDeviceNode
= ParentDeviceNode
->Child
;
1534 ChildDeviceNode
!= NULL
;
1535 ChildDeviceNode
= ChildDeviceNode
->Sibling
)
1537 /* Pass the current device node to the action routine */
1538 Context
->DeviceNode
= ChildDeviceNode
;
1540 Status
= IopTraverseDeviceTreeNode(Context
);
1541 if (!NT_SUCCESS(Status
))
1552 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context
)
1556 DPRINT("Context 0x%p\n", Context
);
1558 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %p Context 0x%p)\n",
1559 Context
->DeviceNode
, Context
->FirstDeviceNode
, Context
->Action
, Context
->Context
);
1561 /* Start from the specified device node */
1562 Context
->DeviceNode
= Context
->FirstDeviceNode
;
1564 /* Recursively traverse the device tree */
1565 Status
= IopTraverseDeviceTreeNode(Context
);
1566 if (Status
== STATUS_UNSUCCESSFUL
)
1568 /* The action routine just wanted to terminate the traversal with status
1569 code STATUS_SUCCESS */
1570 Status
= STATUS_SUCCESS
;
1578 * IopCreateDeviceKeyPath
1580 * Creates a registry key
1584 * Name of the key to be created.
1586 * Handle to the newly created key
1589 * This method can create nested trees, so parent of RegistryPath can
1590 * be not existant, and will be created if needed.
1594 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
1595 IN ULONG CreateOptions
,
1598 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(ENUM_ROOT
);
1599 HANDLE hParent
= NULL
, hKey
;
1600 OBJECT_ATTRIBUTES ObjectAttributes
;
1601 UNICODE_STRING KeyName
;
1602 PCWSTR Current
, Last
;
1606 /* Assume failure */
1609 /* Open root key for device instances */
1610 Status
= IopOpenRegistryKeyEx(&hParent
, NULL
, &EnumU
, KEY_CREATE_SUB_KEY
);
1611 if (!NT_SUCCESS(Status
))
1613 DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
1617 Current
= KeyName
.Buffer
= RegistryPath
->Buffer
;
1618 Last
= &RegistryPath
->Buffer
[RegistryPath
->Length
/ sizeof(WCHAR
)];
1620 /* Go up to the end of the string */
1621 while (Current
<= Last
)
1623 if (Current
!= Last
&& *Current
!= L
'\\')
1625 /* Not the end of the string and not a separator */
1630 /* Prepare relative key name */
1631 Length
= (USHORT
)((ULONG_PTR
)Current
- (ULONG_PTR
)KeyName
.Buffer
);
1632 KeyName
.MaximumLength
= KeyName
.Length
= Length
;
1633 DPRINT("Create '%wZ'\n", &KeyName
);
1636 InitializeObjectAttributes(&ObjectAttributes
,
1638 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1641 Status
= ZwCreateKey(&hKey
,
1642 Current
== Last
? KEY_ALL_ACCESS
: KEY_CREATE_SUB_KEY
,
1649 /* Close parent key handle, we don't need it anymore */
1653 /* Key opening/creating failed? */
1654 if (!NT_SUCCESS(Status
))
1656 DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName
, Status
);
1660 /* Check if it is the end of the string */
1661 if (Current
== Last
)
1663 /* Yes, return success */
1665 return STATUS_SUCCESS
;
1668 /* Start with this new parent key */
1671 KeyName
.Buffer
= (PWSTR
)Current
;
1674 return STATUS_UNSUCCESSFUL
;
1678 IopSetDeviceInstanceData(HANDLE InstanceKey
,
1679 PDEVICE_NODE DeviceNode
)
1681 OBJECT_ATTRIBUTES ObjectAttributes
;
1682 UNICODE_STRING KeyName
;
1683 HANDLE LogConfKey
, ControlKey
, DeviceParamsKey
;
1688 DPRINT("IopSetDeviceInstanceData() called\n");
1690 /* Create the 'LogConf' key */
1691 RtlInitUnicodeString(&KeyName
, L
"LogConf");
1692 InitializeObjectAttributes(&ObjectAttributes
,
1694 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1697 Status
= ZwCreateKey(&LogConfKey
,
1702 // FIXME? In r53694 it was silently turned from non-volatile into this,
1703 // without any extra warning. Is this still needed??
1704 REG_OPTION_VOLATILE
,
1706 if (NT_SUCCESS(Status
))
1708 /* Set 'BootConfig' value */
1709 if (DeviceNode
->BootResources
!= NULL
)
1711 ResCount
= DeviceNode
->BootResources
->Count
;
1714 RtlInitUnicodeString(&KeyName
, L
"BootConfig");
1715 Status
= ZwSetValueKey(LogConfKey
,
1719 DeviceNode
->BootResources
,
1720 PnpDetermineResourceListSize(DeviceNode
->BootResources
));
1724 /* Set 'BasicConfigVector' value */
1725 if (DeviceNode
->ResourceRequirements
!= NULL
&&
1726 DeviceNode
->ResourceRequirements
->ListSize
!= 0)
1728 RtlInitUnicodeString(&KeyName
, L
"BasicConfigVector");
1729 Status
= ZwSetValueKey(LogConfKey
,
1732 REG_RESOURCE_REQUIREMENTS_LIST
,
1733 DeviceNode
->ResourceRequirements
,
1734 DeviceNode
->ResourceRequirements
->ListSize
);
1737 ZwClose(LogConfKey
);
1740 /* Set the 'ConfigFlags' value */
1741 RtlInitUnicodeString(&KeyName
, L
"ConfigFlags");
1742 Status
= ZwQueryValueKey(InstanceKey
,
1744 KeyValueBasicInformation
,
1748 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1750 /* Write the default value */
1751 ULONG DefaultConfigFlags
= 0;
1752 Status
= ZwSetValueKey(InstanceKey
,
1756 &DefaultConfigFlags
,
1757 sizeof(DefaultConfigFlags
));
1760 /* Create the 'Control' key */
1761 RtlInitUnicodeString(&KeyName
, L
"Control");
1762 InitializeObjectAttributes(&ObjectAttributes
,
1764 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1767 Status
= ZwCreateKey(&ControlKey
,
1772 REG_OPTION_VOLATILE
,
1774 if (NT_SUCCESS(Status
))
1775 ZwClose(ControlKey
);
1777 /* Create the 'Device Parameters' key and set the 'FirmwareIdentified' value for all ACPI-enumerated devices */
1778 if (_wcsnicmp(DeviceNode
->InstancePath
.Buffer
, L
"ACPI\\", 5) == 0)
1780 RtlInitUnicodeString(&KeyName
, L
"Device Parameters");
1781 InitializeObjectAttributes(&ObjectAttributes
,
1783 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1786 Status
= ZwCreateKey(&DeviceParamsKey
,
1791 REG_OPTION_NON_VOLATILE
,
1793 if (NT_SUCCESS(Status
))
1795 ULONG FirmwareIdentified
= 1;
1796 RtlInitUnicodeString(&KeyName
, L
"FirmwareIdentified");
1797 Status
= ZwSetValueKey(DeviceParamsKey
,
1801 &FirmwareIdentified
,
1802 sizeof(FirmwareIdentified
));
1804 ZwClose(DeviceParamsKey
);
1808 DPRINT("IopSetDeviceInstanceData() done\n");
1814 * IopGetParentIdPrefix
1816 * Retrieve (or create) a string which identifies a device.
1820 * Pointer to device node.
1822 * Pointer to the string where is returned the parent node identifier
1825 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1826 * valid and its Buffer field is NULL-terminated. The caller needs to
1827 * to free the string with RtlFreeUnicodeString when it is no longer
1832 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode
,
1833 PUNICODE_STRING ParentIdPrefix
)
1835 const UNICODE_STRING EnumKeyPath
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1836 ULONG KeyNameBufferLength
;
1837 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation
= NULL
;
1838 UNICODE_STRING KeyName
= {0, 0, NULL
};
1839 UNICODE_STRING KeyValue
;
1840 UNICODE_STRING ValueName
;
1845 /* HACK: As long as some devices have a NULL device
1846 * instance path, the following test is required :(
1848 if (DeviceNode
->Parent
->InstancePath
.Length
== 0)
1850 DPRINT1("Parent of %wZ has NULL Instance path, please report!\n",
1851 &DeviceNode
->InstancePath
);
1852 return STATUS_UNSUCCESSFUL
;
1855 /* 1. Try to retrieve ParentIdPrefix from registry */
1856 KeyNameBufferLength
= FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
[0]) + MAX_PATH
* sizeof(WCHAR
);
1857 ParentIdPrefixInformation
= ExAllocatePoolWithTag(PagedPool
,
1858 KeyNameBufferLength
+ sizeof(UNICODE_NULL
),
1860 if (!ParentIdPrefixInformation
)
1862 return STATUS_INSUFFICIENT_RESOURCES
;
1866 KeyName
.MaximumLength
= EnumKeyPath
.Length
+
1867 DeviceNode
->Parent
->InstancePath
.Length
+
1868 sizeof(UNICODE_NULL
);
1869 KeyName
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1870 KeyName
.MaximumLength
,
1872 if (!KeyName
.Buffer
)
1874 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1878 RtlCopyUnicodeString(&KeyName
, &EnumKeyPath
);
1879 RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->Parent
->InstancePath
);
1881 Status
= IopOpenRegistryKeyEx(&hKey
, NULL
, &KeyName
, KEY_QUERY_VALUE
| KEY_SET_VALUE
);
1882 if (!NT_SUCCESS(Status
))
1886 RtlInitUnicodeString(&ValueName
, L
"ParentIdPrefix");
1887 Status
= ZwQueryValueKey(hKey
,
1889 KeyValuePartialInformation
,
1890 ParentIdPrefixInformation
,
1891 KeyNameBufferLength
,
1892 &KeyNameBufferLength
);
1893 if (NT_SUCCESS(Status
))
1895 if (ParentIdPrefixInformation
->Type
!= REG_SZ
)
1897 Status
= STATUS_UNSUCCESSFUL
;
1901 KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1902 KeyValue
.Length
= KeyValue
.MaximumLength
- sizeof(UNICODE_NULL
);
1903 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1904 ASSERT(KeyValue
.Buffer
[KeyValue
.Length
/ sizeof(WCHAR
)] == UNICODE_NULL
);
1908 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1910 /* FIXME how do we get here and why is ParentIdPrefixInformation valid? */
1911 KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1912 KeyValue
.Length
= KeyValue
.MaximumLength
- sizeof(UNICODE_NULL
);
1913 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1914 ASSERT(KeyValue
.Buffer
[KeyValue
.Length
/ sizeof(WCHAR
)] == UNICODE_NULL
);
1918 /* 2. Create the ParentIdPrefix value */
1919 crc32
= RtlComputeCrc32(0,
1920 (PUCHAR
)DeviceNode
->Parent
->InstancePath
.Buffer
,
1921 DeviceNode
->Parent
->InstancePath
.Length
);
1923 RtlStringCbPrintfW((PWSTR
)ParentIdPrefixInformation
,
1924 KeyNameBufferLength
,
1926 DeviceNode
->Parent
->Level
,
1928 RtlInitUnicodeString(&KeyValue
, (PWSTR
)ParentIdPrefixInformation
);
1930 /* 3. Try to write the ParentIdPrefix to registry */
1931 Status
= ZwSetValueKey(hKey
,
1936 ((ULONG
)wcslen(KeyValue
.Buffer
) + 1) * sizeof(WCHAR
));
1939 if (NT_SUCCESS(Status
))
1941 /* Duplicate the string to return it */
1942 Status
= RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
1946 ExFreePoolWithTag(ParentIdPrefixInformation
, TAG_IO
);
1947 RtlFreeUnicodeString(&KeyName
);
1959 _In_ BUS_QUERY_ID_TYPE QueryType
)
1964 ULONG SeparatorsCount
= 0;
1965 PWCHAR PtrPrevChar
= NULL
;
1966 ULONG MaxSeparators
;
1973 case BusQueryDeviceID
:
1974 MaxSeparators
= MAX_SEPARATORS_DEVICEID
;
1977 case BusQueryInstanceID
:
1978 MaxSeparators
= MAX_SEPARATORS_INSTANCEID
;
1982 case BusQueryHardwareIDs
:
1983 case BusQueryCompatibleIDs
:
1984 MaxSeparators
= MAX_SEPARATORS_DEVICEID
;
1989 DPRINT1("IopValidateID: Not handled QueryType - %x\n", QueryType
);
1993 StringEnd
= Id
+ MAX_DEVICE_ID_LEN
;
1995 for (PtrChar
= Id
; PtrChar
< StringEnd
; PtrChar
++)
1999 if (Char
== UNICODE_NULL
)
2001 if (!IsMultiSz
|| (PtrPrevChar
&& PtrChar
== PtrPrevChar
+ 1))
2003 if (MaxSeparators
== SeparatorsCount
|| IsMultiSz
)
2008 DPRINT1("IopValidateID: SeparatorsCount - %lu, MaxSeparators - %lu\n",
2009 SeparatorsCount
, MaxSeparators
);
2013 StringEnd
= PtrChar
+ MAX_DEVICE_ID_LEN
+ 1;
2014 PtrPrevChar
= PtrChar
;
2015 SeparatorsCount
= 0;
2017 else if (Char
< ' ' || Char
> 0x7F || Char
== ',')
2019 DPRINT1("IopValidateID: Invalid character - %04X\n", Char
);
2022 else if (Char
== ' ')
2026 else if (Char
== '\\')
2030 if (SeparatorsCount
> MaxSeparators
)
2032 DPRINT1("IopValidateID: SeparatorsCount - %lu, MaxSeparators - %lu\n",
2033 SeparatorsCount
, MaxSeparators
);
2039 DPRINT1("IopValidateID: Not terminated ID\n");
2047 IopQueryHardwareIds(PDEVICE_NODE DeviceNode
,
2050 IO_STACK_LOCATION Stack
;
2051 IO_STATUS_BLOCK IoStatusBlock
;
2053 UNICODE_STRING ValueName
;
2055 ULONG Length
, TotalLength
;
2058 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
2060 RtlZeroMemory(&Stack
, sizeof(Stack
));
2061 Stack
.Parameters
.QueryId
.IdType
= BusQueryHardwareIDs
;
2062 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2066 if (NT_SUCCESS(Status
))
2068 IsValidID
= IopValidateID((PWCHAR
)IoStatusBlock
.Information
, BusQueryHardwareIDs
);
2072 DPRINT1("Invalid HardwareIDs. DeviceNode - %p\n", DeviceNode
);
2077 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
2078 DPRINT("Hardware IDs:\n");
2081 DPRINT(" %S\n", Ptr
);
2082 Length
= (ULONG
)wcslen(Ptr
) + 1;
2085 TotalLength
+= Length
;
2087 DPRINT("TotalLength: %hu\n", TotalLength
);
2090 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
2091 Status
= ZwSetValueKey(InstanceKey
,
2095 (PVOID
)IoStatusBlock
.Information
,
2096 (TotalLength
+ 1) * sizeof(WCHAR
));
2097 if (!NT_SUCCESS(Status
))
2099 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
2104 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
2111 IopQueryCompatibleIds(PDEVICE_NODE DeviceNode
,
2114 IO_STACK_LOCATION Stack
;
2115 IO_STATUS_BLOCK IoStatusBlock
;
2117 UNICODE_STRING ValueName
;
2119 ULONG Length
, TotalLength
;
2122 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
2124 RtlZeroMemory(&Stack
, sizeof(Stack
));
2125 Stack
.Parameters
.QueryId
.IdType
= BusQueryCompatibleIDs
;
2126 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2130 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2132 IsValidID
= IopValidateID((PWCHAR
)IoStatusBlock
.Information
, BusQueryCompatibleIDs
);
2136 DPRINT1("Invalid CompatibleIDs. DeviceNode - %p\n", DeviceNode
);
2141 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
2142 DPRINT("Compatible IDs:\n");
2145 DPRINT(" %S\n", Ptr
);
2146 Length
= (ULONG
)wcslen(Ptr
) + 1;
2149 TotalLength
+= Length
;
2151 DPRINT("TotalLength: %hu\n", TotalLength
);
2154 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
2155 Status
= ZwSetValueKey(InstanceKey
,
2159 (PVOID
)IoStatusBlock
.Information
,
2160 (TotalLength
+ 1) * sizeof(WCHAR
));
2161 if (!NT_SUCCESS(Status
))
2163 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status
);
2168 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
2175 IopCreateDeviceInstancePath(
2176 _In_ PDEVICE_NODE DeviceNode
,
2177 _Out_ PUNICODE_STRING InstancePath
)
2179 IO_STATUS_BLOCK IoStatusBlock
;
2180 UNICODE_STRING DeviceId
;
2181 UNICODE_STRING InstanceId
;
2182 IO_STACK_LOCATION Stack
;
2184 UNICODE_STRING ParentIdPrefix
= { 0, 0, NULL
};
2185 DEVICE_CAPABILITIES DeviceCapabilities
;
2188 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
2190 Stack
.Parameters
.QueryId
.IdType
= BusQueryDeviceID
;
2191 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2195 if (!NT_SUCCESS(Status
))
2197 DPRINT1("IopInitiatePnpIrp(BusQueryDeviceID) failed (Status %x)\n", Status
);
2201 IsValidID
= IopValidateID((PWCHAR
)IoStatusBlock
.Information
, BusQueryDeviceID
);
2205 DPRINT1("Invalid DeviceID. DeviceNode - %p\n", DeviceNode
);
2208 /* Save the device id string */
2209 RtlInitUnicodeString(&DeviceId
, (PWSTR
)IoStatusBlock
.Information
);
2211 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after enumeration)\n");
2213 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
2214 if (!NT_SUCCESS(Status
))
2216 DPRINT1("IopQueryDeviceCapabilities() failed (Status 0x%08lx)\n", Status
);
2217 RtlFreeUnicodeString(&DeviceId
);
2221 /* This bit is only check after enumeration */
2222 if (DeviceCapabilities
.HardwareDisabled
)
2224 /* FIXME: Cleanup device */
2225 DeviceNode
->Flags
|= DNF_DISABLED
;
2226 RtlFreeUnicodeString(&DeviceId
);
2227 return STATUS_PLUGPLAY_NO_DEVICE
;
2231 DeviceNode
->Flags
&= ~DNF_DISABLED
;
2234 if (!DeviceCapabilities
.UniqueID
)
2236 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
2237 DPRINT("Instance ID is not unique\n");
2238 Status
= IopGetParentIdPrefix(DeviceNode
, &ParentIdPrefix
);
2239 if (!NT_SUCCESS(Status
))
2241 DPRINT1("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status
);
2242 RtlFreeUnicodeString(&DeviceId
);
2247 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
2249 Stack
.Parameters
.QueryId
.IdType
= BusQueryInstanceID
;
2250 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2254 if (!NT_SUCCESS(Status
))
2256 DPRINT("IopInitiatePnpIrp(BusQueryInstanceID) failed (Status %lx)\n", Status
);
2257 ASSERT(IoStatusBlock
.Information
== 0);
2260 if (IoStatusBlock
.Information
)
2262 IsValidID
= IopValidateID((PWCHAR
)IoStatusBlock
.Information
, BusQueryInstanceID
);
2266 DPRINT1("Invalid InstanceID. DeviceNode - %p\n", DeviceNode
);
2270 RtlInitUnicodeString(&InstanceId
,
2271 (PWSTR
)IoStatusBlock
.Information
);
2273 InstancePath
->Length
= 0;
2274 InstancePath
->MaximumLength
= DeviceId
.Length
+ sizeof(WCHAR
) +
2275 ParentIdPrefix
.Length
+
2277 sizeof(UNICODE_NULL
);
2278 if (ParentIdPrefix
.Length
&& InstanceId
.Length
)
2280 InstancePath
->MaximumLength
+= sizeof(WCHAR
);
2283 InstancePath
->Buffer
= ExAllocatePoolWithTag(PagedPool
,
2284 InstancePath
->MaximumLength
,
2286 if (!InstancePath
->Buffer
)
2288 RtlFreeUnicodeString(&InstanceId
);
2289 RtlFreeUnicodeString(&ParentIdPrefix
);
2290 RtlFreeUnicodeString(&DeviceId
);
2291 return STATUS_INSUFFICIENT_RESOURCES
;
2294 /* Start with the device id */
2295 RtlCopyUnicodeString(InstancePath
, &DeviceId
);
2296 RtlAppendUnicodeToString(InstancePath
, L
"\\");
2298 /* Add information from parent bus device to InstancePath */
2299 RtlAppendUnicodeStringToString(InstancePath
, &ParentIdPrefix
);
2300 if (ParentIdPrefix
.Length
&& InstanceId
.Length
)
2302 RtlAppendUnicodeToString(InstancePath
, L
"&");
2305 /* Finally, add the id returned by the driver stack */
2306 RtlAppendUnicodeStringToString(InstancePath
, &InstanceId
);
2309 * FIXME: Check for valid characters, if there is invalid characters
2313 RtlFreeUnicodeString(&InstanceId
);
2314 RtlFreeUnicodeString(&DeviceId
);
2315 RtlFreeUnicodeString(&ParentIdPrefix
);
2317 return STATUS_SUCCESS
;
2321 * IopActionInterrogateDeviceStack
2323 * Retrieve information for all (direct) child nodes of a parent node.
2327 * Pointer to device node.
2329 * Pointer to parent node to retrieve child node information for.
2332 * Any errors that occur are logged instead so that all child services have a chance
2333 * of being interrogated.
2337 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode
,
2340 IO_STATUS_BLOCK IoStatusBlock
;
2341 PWSTR DeviceDescription
;
2342 PWSTR LocationInformation
;
2343 PDEVICE_NODE ParentDeviceNode
;
2344 IO_STACK_LOCATION Stack
;
2346 ULONG RequiredLength
;
2348 HANDLE InstanceKey
= NULL
;
2349 UNICODE_STRING ValueName
;
2350 UNICODE_STRING InstancePathU
;
2351 PDEVICE_OBJECT OldDeviceObject
;
2353 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode
, Context
);
2354 DPRINT("PDO 0x%p\n", DeviceNode
->PhysicalDeviceObject
);
2356 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2359 * We are called for the parent too, but we don't need to do special
2360 * handling for this node
2362 if (DeviceNode
== ParentDeviceNode
)
2364 DPRINT("Success\n");
2365 return STATUS_SUCCESS
;
2369 * Make sure this device node is a direct child of the parent device node
2370 * that is given as an argument
2372 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2374 DPRINT("Skipping 2+ level child\n");
2375 return STATUS_SUCCESS
;
2378 /* Skip processing if it was already completed before */
2379 if (DeviceNode
->Flags
& DNF_PROCESSED
)
2382 return STATUS_SUCCESS
;
2386 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
2387 if (!NT_SUCCESS(Status
))
2389 DPRINT1("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status
);
2394 * FIXME: For critical errors, cleanup and disable device, but always
2395 * return STATUS_SUCCESS.
2398 Status
= IopCreateDeviceInstancePath(DeviceNode
, &InstancePathU
);
2399 if (!NT_SUCCESS(Status
))
2401 if (Status
!= STATUS_PLUGPLAY_NO_DEVICE
)
2403 DPRINT1("IopCreateDeviceInstancePath() failed with status 0x%lx\n", Status
);
2406 /* We have to return success otherwise we abort the traverse operation */
2407 return STATUS_SUCCESS
;
2410 /* Verify that this is not a duplicate */
2411 OldDeviceObject
= IopGetDeviceObjectFromDeviceInstance(&InstancePathU
);
2412 if (OldDeviceObject
!= NULL
)
2414 PDEVICE_NODE OldDeviceNode
= IopGetDeviceNode(OldDeviceObject
);
2416 DPRINT1("Duplicate device instance '%wZ'\n", &InstancePathU
);
2417 DPRINT1("Current instance parent: '%wZ'\n", &DeviceNode
->Parent
->InstancePath
);
2418 DPRINT1("Old instance parent: '%wZ'\n", &OldDeviceNode
->Parent
->InstancePath
);
2420 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
,
2422 (ULONG_PTR
)DeviceNode
->PhysicalDeviceObject
,
2423 (ULONG_PTR
)OldDeviceObject
,
2427 DeviceNode
->InstancePath
= InstancePathU
;
2429 DPRINT("InstancePath is %S\n", DeviceNode
->InstancePath
.Buffer
);
2432 * Create registry key for the instance id, if it doesn't exist yet
2434 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_NON_VOLATILE
, &InstanceKey
);
2435 if (!NT_SUCCESS(Status
))
2437 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status
);
2439 /* We have to return success otherwise we abort the traverse operation */
2440 return STATUS_SUCCESS
;
2443 IopQueryHardwareIds(DeviceNode
, InstanceKey
);
2445 IopQueryCompatibleIds(DeviceNode
, InstanceKey
);
2447 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
2449 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextDescription
;
2450 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
2451 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2453 IRP_MN_QUERY_DEVICE_TEXT
,
2455 DeviceDescription
= NT_SUCCESS(Status
) ? (PWSTR
)IoStatusBlock
.Information
2457 /* This key is mandatory, so even if the Irp fails, we still write it */
2458 RtlInitUnicodeString(&ValueName
, L
"DeviceDesc");
2459 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
2461 if (DeviceDescription
&&
2462 *DeviceDescription
!= UNICODE_NULL
)
2464 /* This key is overriden when a driver is installed. Don't write the
2465 * new description if another one already exists */
2466 Status
= ZwSetValueKey(InstanceKey
,
2471 ((ULONG
)wcslen(DeviceDescription
) + 1) * sizeof(WCHAR
));
2475 UNICODE_STRING DeviceDesc
= RTL_CONSTANT_STRING(L
"Unknown device");
2476 DPRINT("Driver didn't return DeviceDesc (Status 0x%08lx), so place unknown device there\n", Status
);
2478 Status
= ZwSetValueKey(InstanceKey
,
2483 DeviceDesc
.MaximumLength
);
2484 if (!NT_SUCCESS(Status
))
2486 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status
);
2492 if (DeviceDescription
)
2494 ExFreePoolWithTag(DeviceDescription
, 0);
2497 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
2499 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextLocationInformation
;
2500 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
2501 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2503 IRP_MN_QUERY_DEVICE_TEXT
,
2505 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2507 LocationInformation
= (PWSTR
)IoStatusBlock
.Information
;
2508 DPRINT("LocationInformation: %S\n", LocationInformation
);
2509 RtlInitUnicodeString(&ValueName
, L
"LocationInformation");
2510 Status
= ZwSetValueKey(InstanceKey
,
2514 LocationInformation
,
2515 ((ULONG
)wcslen(LocationInformation
) + 1) * sizeof(WCHAR
));
2516 if (!NT_SUCCESS(Status
))
2518 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
2521 ExFreePoolWithTag(LocationInformation
, 0);
2525 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2528 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
2530 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2532 IRP_MN_QUERY_BUS_INFORMATION
,
2534 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2536 PPNP_BUS_INFORMATION BusInformation
= (PPNP_BUS_INFORMATION
)IoStatusBlock
.Information
;
2538 DeviceNode
->ChildBusNumber
= BusInformation
->BusNumber
;
2539 DeviceNode
->ChildInterfaceType
= BusInformation
->LegacyBusType
;
2540 DeviceNode
->ChildBusTypeIndex
= IopGetBusTypeGuidIndex(&BusInformation
->BusTypeGuid
);
2541 ExFreePoolWithTag(BusInformation
, 0);
2545 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2547 DeviceNode
->ChildBusNumber
= 0xFFFFFFF0;
2548 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
2549 DeviceNode
->ChildBusTypeIndex
= -1;
2552 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
2554 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2556 IRP_MN_QUERY_RESOURCES
,
2558 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2560 DeviceNode
->BootResources
= (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
2561 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
2565 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2566 DeviceNode
->BootResources
= NULL
;
2569 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
2571 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2573 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
2575 if (NT_SUCCESS(Status
))
2577 DeviceNode
->ResourceRequirements
= (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
2581 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
2582 DeviceNode
->ResourceRequirements
= NULL
;
2585 if (InstanceKey
!= NULL
)
2587 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
2590 ZwClose(InstanceKey
);
2592 IopDeviceNodeSetFlag(DeviceNode
, DNF_PROCESSED
);
2594 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_LEGACY_DRIVER
))
2596 /* Report the device to the user-mode pnp manager */
2597 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
2598 &DeviceNode
->InstancePath
);
2601 return STATUS_SUCCESS
;
2606 IopHandleDeviceRemoval(
2607 IN PDEVICE_NODE DeviceNode
,
2608 IN PDEVICE_RELATIONS DeviceRelations
)
2610 PDEVICE_NODE Child
= DeviceNode
->Child
, NextChild
;
2614 if (DeviceNode
== IopRootDeviceNode
)
2617 while (Child
!= NULL
)
2619 NextChild
= Child
->Sibling
;
2622 for (i
= 0; DeviceRelations
&& i
< DeviceRelations
->Count
; i
++)
2624 if (IopGetDeviceNode(DeviceRelations
->Objects
[i
]) == Child
)
2631 if (!Found
&& !(Child
->Flags
& DNF_WILL_BE_REMOVED
))
2633 /* Send removal IRPs to all of its children */
2634 IopPrepareDeviceForRemoval(Child
->PhysicalDeviceObject
, TRUE
);
2636 /* Send the surprise removal IRP */
2637 IopSendSurpriseRemoval(Child
->PhysicalDeviceObject
);
2639 /* Tell the user-mode PnP manager that a device was removed */
2640 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
2641 &Child
->InstancePath
);
2643 /* Send the remove device IRP */
2644 IopSendRemoveDevice(Child
->PhysicalDeviceObject
);
2653 IN PDEVICE_OBJECT DeviceObject
)
2655 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
2656 DEVICETREE_TRAVERSE_CONTEXT Context
;
2657 PDEVICE_RELATIONS DeviceRelations
;
2658 PDEVICE_OBJECT ChildDeviceObject
;
2659 IO_STATUS_BLOCK IoStatusBlock
;
2660 PDEVICE_NODE ChildDeviceNode
;
2661 IO_STACK_LOCATION Stack
;
2665 DPRINT("DeviceObject 0x%p\n", DeviceObject
);
2667 if (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
)
2669 DeviceNode
->Flags
&= ~DNF_NEED_ENUMERATION_ONLY
;
2671 DPRINT("Sending GUID_DEVICE_ARRIVAL\n");
2672 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
2673 &DeviceNode
->InstancePath
);
2676 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
2678 Stack
.Parameters
.QueryDeviceRelations
.Type
= BusRelations
;
2680 Status
= IopInitiatePnpIrp(
2683 IRP_MN_QUERY_DEVICE_RELATIONS
,
2685 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
2687 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
2691 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
2694 * Send removal IRPs for devices that have disappeared
2695 * NOTE: This code handles the case where no relations are specified
2697 IopHandleDeviceRemoval(DeviceNode
, DeviceRelations
);
2699 /* Now we bail if nothing was returned */
2700 if (!DeviceRelations
)
2702 /* We're all done */
2703 DPRINT("No PDOs\n");
2704 return STATUS_SUCCESS
;
2707 DPRINT("Got %u PDOs\n", DeviceRelations
->Count
);
2710 * Create device nodes for all discovered devices
2712 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
2714 ChildDeviceObject
= DeviceRelations
->Objects
[i
];
2715 ASSERT((ChildDeviceObject
->Flags
& DO_DEVICE_INITIALIZING
) == 0);
2717 ChildDeviceNode
= IopGetDeviceNode(ChildDeviceObject
);
2718 if (!ChildDeviceNode
)
2720 /* One doesn't exist, create it */
2721 Status
= IopCreateDeviceNode(
2726 if (NT_SUCCESS(Status
))
2728 /* Mark the node as enumerated */
2729 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
2731 /* Mark the DO as bus enumerated */
2732 ChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
2736 /* Ignore this DO */
2737 DPRINT1("IopCreateDeviceNode() failed with status 0x%08x. Skipping PDO %u\n", Status
, i
);
2738 ObDereferenceObject(ChildDeviceObject
);
2743 /* Mark it as enumerated */
2744 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
2745 ObDereferenceObject(ChildDeviceObject
);
2748 ExFreePool(DeviceRelations
);
2751 * Retrieve information about all discovered children from the bus driver
2753 IopInitDeviceTreeTraverseContext(
2756 IopActionInterrogateDeviceStack
,
2759 Status
= IopTraverseDeviceTree(&Context
);
2760 if (!NT_SUCCESS(Status
))
2762 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
2767 * Retrieve configuration from the registry for discovered children
2769 IopInitDeviceTreeTraverseContext(
2772 IopActionConfigureChildServices
,
2775 Status
= IopTraverseDeviceTree(&Context
);
2776 if (!NT_SUCCESS(Status
))
2778 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
2783 * Initialize services for discovered children.
2785 Status
= IopInitializePnpServices(DeviceNode
);
2786 if (!NT_SUCCESS(Status
))
2788 DPRINT("IopInitializePnpServices() failed with status 0x%08lx\n", Status
);
2792 DPRINT("IopEnumerateDevice() finished\n");
2793 return STATUS_SUCCESS
;
2798 * IopActionConfigureChildServices
2800 * Retrieve configuration for all (direct) child nodes of a parent node.
2804 * Pointer to device node.
2806 * Pointer to parent node to retrieve child node configuration for.
2809 * Any errors that occur are logged instead so that all child services have a chance of beeing
2814 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode
,
2817 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
2818 PDEVICE_NODE ParentDeviceNode
;
2819 PUNICODE_STRING Service
;
2820 UNICODE_STRING ClassGUID
;
2822 DEVICE_CAPABILITIES DeviceCaps
;
2824 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode
, Context
);
2826 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2829 * We are called for the parent too, but we don't need to do special
2830 * handling for this node
2832 if (DeviceNode
== ParentDeviceNode
)
2834 DPRINT("Success\n");
2835 return STATUS_SUCCESS
;
2839 * Make sure this device node is a direct child of the parent device node
2840 * that is given as an argument
2843 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2845 DPRINT("Skipping 2+ level child\n");
2846 return STATUS_SUCCESS
;
2849 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2851 DPRINT1("Child not ready to be configured\n");
2852 return STATUS_SUCCESS
;
2855 if (!(DeviceNode
->Flags
& (DNF_DISABLED
| DNF_STARTED
| DNF_ADDED
)))
2857 WCHAR RegKeyBuffer
[MAX_PATH
];
2858 UNICODE_STRING RegKey
;
2860 /* Install the service for this if it's in the CDDB */
2861 IopInstallCriticalDevice(DeviceNode
);
2864 RegKey
.MaximumLength
= sizeof(RegKeyBuffer
);
2865 RegKey
.Buffer
= RegKeyBuffer
;
2868 * Retrieve configuration from Enum key
2871 Service
= &DeviceNode
->ServiceName
;
2873 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
2874 RtlInitUnicodeString(Service
, NULL
);
2875 RtlInitUnicodeString(&ClassGUID
, NULL
);
2877 QueryTable
[0].Name
= L
"Service";
2878 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2879 QueryTable
[0].EntryContext
= Service
;
2881 QueryTable
[1].Name
= L
"ClassGUID";
2882 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2883 QueryTable
[1].EntryContext
= &ClassGUID
;
2884 QueryTable
[1].DefaultType
= REG_SZ
;
2885 QueryTable
[1].DefaultData
= L
"";
2886 QueryTable
[1].DefaultLength
= 0;
2888 RtlAppendUnicodeToString(&RegKey
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2889 RtlAppendUnicodeStringToString(&RegKey
, &DeviceNode
->InstancePath
);
2891 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
2892 RegKey
.Buffer
, QueryTable
, NULL
, NULL
);
2894 if (!NT_SUCCESS(Status
))
2896 /* FIXME: Log the error */
2897 DPRINT("Could not retrieve configuration for device %wZ (Status 0x%08x)\n",
2898 &DeviceNode
->InstancePath
, Status
);
2899 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2900 return STATUS_SUCCESS
;
2903 if (Service
->Buffer
== NULL
)
2905 if (NT_SUCCESS(IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
)) &&
2906 DeviceCaps
.RawDeviceOK
)
2908 DPRINT("%wZ is using parent bus driver (%wZ)\n", &DeviceNode
->InstancePath
, &ParentDeviceNode
->ServiceName
);
2909 RtlInitEmptyUnicodeString(&DeviceNode
->ServiceName
, NULL
, 0);
2911 else if (ClassGUID
.Length
!= 0)
2913 /* Device has a ClassGUID value, but no Service value.
2914 * Suppose it is using the NULL driver, so state the
2915 * device is started */
2916 DPRINT("%wZ is using NULL driver\n", &DeviceNode
->InstancePath
);
2917 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
2921 DeviceNode
->Problem
= CM_PROB_FAILED_INSTALL
;
2922 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2924 return STATUS_SUCCESS
;
2927 DPRINT("Got Service %S\n", Service
->Buffer
);
2930 return STATUS_SUCCESS
;
2934 * IopActionInitChildServices
2936 * Initialize the service for all (direct) child nodes of a parent node
2940 * Pointer to device node.
2942 * Pointer to parent node to initialize child node services for.
2945 * If the driver image for a service is not loaded and initialized
2946 * it is done here too. Any errors that occur are logged instead so
2947 * that all child services have a chance of being initialized.
2951 IopActionInitChildServices(PDEVICE_NODE DeviceNode
,
2954 PDEVICE_NODE ParentDeviceNode
;
2956 BOOLEAN BootDrivers
= !PnpSystemInit
;
2958 DPRINT("IopActionInitChildServices(%p, %p)\n", DeviceNode
, Context
);
2960 ParentDeviceNode
= Context
;
2963 * We are called for the parent too, but we don't need to do special
2964 * handling for this node
2966 if (DeviceNode
== ParentDeviceNode
)
2968 DPRINT("Success\n");
2969 return STATUS_SUCCESS
;
2973 * We don't want to check for a direct child because
2974 * this function is called during boot to reinitialize
2975 * devices with drivers that couldn't load yet due to
2976 * stage 0 limitations (ie can't load from disk yet).
2979 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2981 DPRINT1("Child not ready to be added\n");
2982 return STATUS_SUCCESS
;
2985 if (IopDeviceNodeHasFlag(DeviceNode
, DNF_STARTED
) ||
2986 IopDeviceNodeHasFlag(DeviceNode
, DNF_ADDED
) ||
2987 IopDeviceNodeHasFlag(DeviceNode
, DNF_DISABLED
))
2988 return STATUS_SUCCESS
;
2990 if (DeviceNode
->ServiceName
.Buffer
== NULL
)
2992 /* We don't need to worry about loading the driver because we're
2993 * being driven in raw mode so our parent must be loaded to get here */
2994 Status
= IopInitializeDevice(DeviceNode
, NULL
);
2995 if (NT_SUCCESS(Status
))
2997 Status
= IopStartDevice(DeviceNode
);
2998 if (!NT_SUCCESS(Status
))
3000 DPRINT1("IopStartDevice(%wZ) failed with status 0x%08x\n",
3001 &DeviceNode
->InstancePath
, Status
);
3007 PLDR_DATA_TABLE_ENTRY ModuleObject
;
3008 PDRIVER_OBJECT DriverObject
;
3010 KeEnterCriticalRegion();
3011 ExAcquireResourceExclusiveLite(&IopDriverLoadResource
, TRUE
);
3012 /* Get existing DriverObject pointer (in case the driver has
3013 already been loaded and initialized) */
3014 Status
= IopGetDriverObject(
3016 &DeviceNode
->ServiceName
,
3019 if (!NT_SUCCESS(Status
))
3021 /* Driver is not initialized, try to load it */
3022 Status
= IopLoadServiceModule(&DeviceNode
->ServiceName
, &ModuleObject
);
3024 if (NT_SUCCESS(Status
) || Status
== STATUS_IMAGE_ALREADY_LOADED
)
3026 /* Initialize the driver */
3027 Status
= IopInitializeDriverModule(DeviceNode
, ModuleObject
,
3028 &DeviceNode
->ServiceName
, FALSE
, &DriverObject
);
3029 if (!NT_SUCCESS(Status
)) DeviceNode
->Problem
= CM_PROB_FAILED_DRIVER_ENTRY
;
3031 else if (Status
== STATUS_DRIVER_UNABLE_TO_LOAD
)
3033 DPRINT1("Service '%wZ' is disabled\n", &DeviceNode
->ServiceName
);
3034 DeviceNode
->Problem
= CM_PROB_DISABLED_SERVICE
;
3038 DPRINT("IopLoadServiceModule(%wZ) failed with status 0x%08x\n",
3039 &DeviceNode
->ServiceName
, Status
);
3040 if (!BootDrivers
) DeviceNode
->Problem
= CM_PROB_DRIVER_FAILED_LOAD
;
3043 ExReleaseResourceLite(&IopDriverLoadResource
);
3044 KeLeaveCriticalRegion();
3046 /* Driver is loaded and initialized at this point */
3047 if (NT_SUCCESS(Status
))
3049 /* Initialize the device, including all filters */
3050 Status
= PipCallDriverAddDevice(DeviceNode
, FALSE
, DriverObject
);
3052 /* Remove the extra reference */
3053 ObDereferenceObject(DriverObject
);
3058 * Don't disable when trying to load only boot drivers
3062 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
3067 return STATUS_SUCCESS
;
3071 * IopInitializePnpServices
3073 * Initialize services for discovered children
3077 * Top device node to start initializing services.
3083 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode
)
3085 DEVICETREE_TRAVERSE_CONTEXT Context
;
3087 DPRINT("IopInitializePnpServices(%p)\n", DeviceNode
);
3089 IopInitDeviceTreeTraverseContext(
3092 IopActionInitChildServices
,
3095 return IopTraverseDeviceTree(&Context
);
3098 static NTSTATUS INIT_FUNCTION
3099 IopEnumerateDetectedDevices(
3101 IN PUNICODE_STRING RelativePath OPTIONAL
,
3103 IN BOOLEAN EnumerateSubKeys
,
3104 IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources
,
3105 IN ULONG ParentBootResourcesLength
)
3107 UNICODE_STRING IdentifierU
= RTL_CONSTANT_STRING(L
"Identifier");
3108 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
3109 UNICODE_STRING ConfigurationDataU
= RTL_CONSTANT_STRING(L
"Configuration Data");
3110 UNICODE_STRING BootConfigU
= RTL_CONSTANT_STRING(L
"BootConfig");
3111 UNICODE_STRING LogConfU
= RTL_CONSTANT_STRING(L
"LogConf");
3112 OBJECT_ATTRIBUTES ObjectAttributes
;
3113 HANDLE hDevicesKey
= NULL
;
3114 HANDLE hDeviceKey
= NULL
;
3115 HANDLE hLevel1Key
, hLevel2Key
= NULL
, hLogConf
;
3116 UNICODE_STRING Level2NameU
;
3117 WCHAR Level2Name
[5];
3118 ULONG IndexDevice
= 0;
3120 PKEY_BASIC_INFORMATION pDeviceInformation
= NULL
;
3121 ULONG DeviceInfoLength
= sizeof(KEY_BASIC_INFORMATION
) + 50 * sizeof(WCHAR
);
3122 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation
= NULL
;
3123 ULONG ValueInfoLength
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 50 * sizeof(WCHAR
);
3124 UNICODE_STRING DeviceName
, ValueName
;
3126 PCM_FULL_RESOURCE_DESCRIPTOR BootResources
= NULL
;
3127 ULONG BootResourcesLength
;
3130 const UNICODE_STRING IdentifierSerial
= RTL_CONSTANT_STRING(L
"SerialController");
3131 UNICODE_STRING HardwareIdSerial
= RTL_CONSTANT_STRING(L
"*PNP0501\0");
3132 static ULONG DeviceIndexSerial
= 0;
3133 const UNICODE_STRING IdentifierKeyboard
= RTL_CONSTANT_STRING(L
"KeyboardController");
3134 UNICODE_STRING HardwareIdKeyboard
= RTL_CONSTANT_STRING(L
"*PNP0303\0");
3135 static ULONG DeviceIndexKeyboard
= 0;
3136 const UNICODE_STRING IdentifierMouse
= RTL_CONSTANT_STRING(L
"PointerController");
3137 UNICODE_STRING HardwareIdMouse
= RTL_CONSTANT_STRING(L
"*PNP0F13\0");
3138 static ULONG DeviceIndexMouse
= 0;
3139 const UNICODE_STRING IdentifierParallel
= RTL_CONSTANT_STRING(L
"ParallelController");
3140 UNICODE_STRING HardwareIdParallel
= RTL_CONSTANT_STRING(L
"*PNP0400\0");
3141 static ULONG DeviceIndexParallel
= 0;
3142 const UNICODE_STRING IdentifierFloppy
= RTL_CONSTANT_STRING(L
"FloppyDiskPeripheral");
3143 UNICODE_STRING HardwareIdFloppy
= RTL_CONSTANT_STRING(L
"*PNP0700\0");
3144 static ULONG DeviceIndexFloppy
= 0;
3145 UNICODE_STRING HardwareIdKey
;
3146 PUNICODE_STRING pHardwareId
;
3147 ULONG DeviceIndex
= 0;
3148 PUCHAR CmResourceList
;
3153 Status
= IopOpenRegistryKeyEx(&hDevicesKey
, hBaseKey
, RelativePath
, KEY_ENUMERATE_SUB_KEYS
);
3154 if (!NT_SUCCESS(Status
))
3156 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3161 hDevicesKey
= hBaseKey
;
3163 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3164 if (!pDeviceInformation
)
3166 DPRINT("ExAllocatePool() failed\n");
3167 Status
= STATUS_NO_MEMORY
;
3171 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3172 if (!pValueInformation
)
3174 DPRINT("ExAllocatePool() failed\n");
3175 Status
= STATUS_NO_MEMORY
;
3181 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3182 if (Status
== STATUS_NO_MORE_ENTRIES
)
3184 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3186 ExFreePool(pDeviceInformation
);
3187 DeviceInfoLength
= RequiredSize
;
3188 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3189 if (!pDeviceInformation
)
3191 DPRINT("ExAllocatePool() failed\n");
3192 Status
= STATUS_NO_MEMORY
;
3195 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3197 if (!NT_SUCCESS(Status
))
3199 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
3204 /* Open device key */
3205 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
3206 DeviceName
.Buffer
= pDeviceInformation
->Name
;
3208 Status
= IopOpenRegistryKeyEx(&hDeviceKey
, hDevicesKey
, &DeviceName
,
3209 KEY_QUERY_VALUE
+ (EnumerateSubKeys
? KEY_ENUMERATE_SUB_KEYS
: 0));
3210 if (!NT_SUCCESS(Status
))
3212 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3216 /* Read boot resources, and add then to parent ones */
3217 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3218 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3220 ExFreePool(pValueInformation
);
3221 ValueInfoLength
= RequiredSize
;
3222 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3223 if (!pValueInformation
)
3225 DPRINT("ExAllocatePool() failed\n");
3226 ZwDeleteKey(hLevel2Key
);
3227 Status
= STATUS_NO_MEMORY
;
3230 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3232 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
3234 BootResources
= ParentBootResources
;
3235 BootResourcesLength
= ParentBootResourcesLength
;
3237 else if (!NT_SUCCESS(Status
))
3239 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
3242 else if (pValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
)
3244 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_FULL_RESOURCE_DESCRIPTOR
);
3249 static const ULONG Header
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
);
3251 /* Concatenate current resources and parent ones */
3252 if (ParentBootResourcesLength
== 0)
3253 BootResourcesLength
= pValueInformation
->DataLength
;
3255 BootResourcesLength
= ParentBootResourcesLength
3256 + pValueInformation
->DataLength
3258 BootResources
= ExAllocatePool(PagedPool
, BootResourcesLength
);
3261 DPRINT("ExAllocatePool() failed\n");
3264 if (ParentBootResourcesLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
3266 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
3268 else if (ParentBootResources
->PartialResourceList
.PartialDescriptors
[ParentBootResources
->PartialResourceList
.Count
- 1].Type
== CmResourceTypeDeviceSpecific
)
3270 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
3272 (PVOID
)((ULONG_PTR
)BootResources
+ pValueInformation
->DataLength
),
3273 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
3274 ParentBootResourcesLength
- Header
);
3275 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
3279 RtlCopyMemory(BootResources
, pValueInformation
->Data
, Header
);
3281 (PVOID
)((ULONG_PTR
)BootResources
+ Header
),
3282 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
3283 ParentBootResourcesLength
- Header
);
3285 (PVOID
)((ULONG_PTR
)BootResources
+ ParentBootResourcesLength
),
3286 pValueInformation
->Data
+ Header
,
3287 pValueInformation
->DataLength
- Header
);
3288 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
3292 if (EnumerateSubKeys
)
3297 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3298 if (Status
== STATUS_NO_MORE_ENTRIES
)
3300 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3302 ExFreePool(pDeviceInformation
);
3303 DeviceInfoLength
= RequiredSize
;
3304 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3305 if (!pDeviceInformation
)
3307 DPRINT("ExAllocatePool() failed\n");
3308 Status
= STATUS_NO_MEMORY
;
3311 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3313 if (!NT_SUCCESS(Status
))
3315 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
3319 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
3320 DeviceName
.Buffer
= pDeviceInformation
->Name
;
3322 Status
= IopEnumerateDetectedDevices(
3328 BootResourcesLength
);
3329 if (!NT_SUCCESS(Status
))
3334 /* Read identifier */
3335 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3336 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3338 ExFreePool(pValueInformation
);
3339 ValueInfoLength
= RequiredSize
;
3340 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3341 if (!pValueInformation
)
3343 DPRINT("ExAllocatePool() failed\n");
3344 Status
= STATUS_NO_MEMORY
;
3347 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3349 if (!NT_SUCCESS(Status
))
3351 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
3353 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
3356 ValueName
.Length
= ValueName
.MaximumLength
= 0;
3358 else if (pValueInformation
->Type
!= REG_SZ
)
3360 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_SZ
);
3365 /* Assign hardware id to this device */
3366 ValueName
.Length
= ValueName
.MaximumLength
= (USHORT
)pValueInformation
->DataLength
;
3367 ValueName
.Buffer
= (PWCHAR
)pValueInformation
->Data
;
3368 if (ValueName
.Length
>= sizeof(WCHAR
) && ValueName
.Buffer
[ValueName
.Length
/ sizeof(WCHAR
) - 1] == UNICODE_NULL
)
3369 ValueName
.Length
-= sizeof(WCHAR
);
3372 if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierSerial
, FALSE
) == 0)
3374 pHardwareId
= &HardwareIdSerial
;
3375 DeviceIndex
= DeviceIndexSerial
++;
3377 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierKeyboard
, FALSE
) == 0)
3379 pHardwareId
= &HardwareIdKeyboard
;
3380 DeviceIndex
= DeviceIndexKeyboard
++;
3382 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierMouse
, FALSE
) == 0)
3384 pHardwareId
= &HardwareIdMouse
;
3385 DeviceIndex
= DeviceIndexMouse
++;
3387 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierParallel
, FALSE
) == 0)
3389 pHardwareId
= &HardwareIdParallel
;
3390 DeviceIndex
= DeviceIndexParallel
++;
3392 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierFloppy
, FALSE
) == 0)
3394 pHardwareId
= &HardwareIdFloppy
;
3395 DeviceIndex
= DeviceIndexFloppy
++;
3399 /* Unknown key path */
3400 DPRINT("Unknown key path '%wZ'\n", RelativePath
);
3404 /* Prepare hardware id key (hardware id value without final \0) */
3405 HardwareIdKey
= *pHardwareId
;
3406 HardwareIdKey
.Length
-= sizeof(UNICODE_NULL
);
3408 /* Add the detected device to Root key */
3409 InitializeObjectAttributes(&ObjectAttributes
, &HardwareIdKey
, OBJ_KERNEL_HANDLE
, hRootKey
, NULL
);
3410 Status
= ZwCreateKey(
3416 REG_OPTION_NON_VOLATILE
,
3418 if (!NT_SUCCESS(Status
))
3420 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3423 swprintf(Level2Name
, L
"%04lu", DeviceIndex
);
3424 RtlInitUnicodeString(&Level2NameU
, Level2Name
);
3425 InitializeObjectAttributes(&ObjectAttributes
, &Level2NameU
, OBJ_KERNEL_HANDLE
, hLevel1Key
, NULL
);
3426 Status
= ZwCreateKey(
3428 KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
,
3432 REG_OPTION_NON_VOLATILE
,
3434 ZwClose(hLevel1Key
);
3435 if (!NT_SUCCESS(Status
))
3437 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3440 DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName
, DeviceIndex
, &HardwareIdKey
);
3441 Status
= ZwSetValueKey(hLevel2Key
, &HardwareIDU
, 0, REG_MULTI_SZ
, pHardwareId
->Buffer
, pHardwareId
->MaximumLength
);
3442 if (!NT_SUCCESS(Status
))
3444 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3445 ZwDeleteKey(hLevel2Key
);
3448 /* Create 'LogConf' subkey */
3449 InitializeObjectAttributes(&ObjectAttributes
, &LogConfU
, OBJ_KERNEL_HANDLE
, hLevel2Key
, NULL
);
3450 Status
= ZwCreateKey(
3456 REG_OPTION_VOLATILE
,
3458 if (!NT_SUCCESS(Status
))
3460 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3461 ZwDeleteKey(hLevel2Key
);
3464 if (BootResourcesLength
>= sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
3466 CmResourceList
= ExAllocatePool(PagedPool
, BootResourcesLength
+ sizeof(ULONG
));
3467 if (!CmResourceList
)
3470 ZwDeleteKey(hLevel2Key
);
3474 /* Add the list count (1st member of CM_RESOURCE_LIST) */
3476 RtlCopyMemory(CmResourceList
,
3480 /* Now add the actual list (2nd member of CM_RESOURCE_LIST) */
3481 RtlCopyMemory(CmResourceList
+ sizeof(ULONG
),
3483 BootResourcesLength
);
3485 /* Save boot resources to 'LogConf\BootConfig' */
3486 Status
= ZwSetValueKey(hLogConf
, &BootConfigU
, 0, REG_RESOURCE_LIST
, CmResourceList
, BootResourcesLength
+ sizeof(ULONG
));
3487 if (!NT_SUCCESS(Status
))
3489 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3491 ZwDeleteKey(hLevel2Key
);
3498 if (BootResources
&& BootResources
!= ParentBootResources
)
3500 ExFreePool(BootResources
);
3501 BootResources
= NULL
;
3505 ZwClose(hLevel2Key
);
3510 ZwClose(hDeviceKey
);
3515 Status
= STATUS_SUCCESS
;
3518 if (hDevicesKey
&& hDevicesKey
!= hBaseKey
)
3519 ZwClose(hDevicesKey
);
3521 ZwClose(hDeviceKey
);
3522 if (pDeviceInformation
)
3523 ExFreePool(pDeviceInformation
);
3524 if (pValueInformation
)
3525 ExFreePool(pValueInformation
);
3529 static BOOLEAN INIT_FUNCTION
3530 IopIsFirmwareMapperDisabled(VOID
)
3532 UNICODE_STRING KeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CURRENTCONTROLSET\\Control\\Pnp");
3533 UNICODE_STRING KeyNameU
= RTL_CONSTANT_STRING(L
"DisableFirmwareMapper");
3534 OBJECT_ATTRIBUTES ObjectAttributes
;
3536 PKEY_VALUE_PARTIAL_INFORMATION KeyInformation
;
3537 ULONG DesiredLength
, Length
;
3541 InitializeObjectAttributes(&ObjectAttributes
, &KeyPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
3542 Status
= ZwOpenKey(&hPnpKey
, KEY_QUERY_VALUE
, &ObjectAttributes
);
3543 if (NT_SUCCESS(Status
))
3545 Status
= ZwQueryValueKey(hPnpKey
,
3547 KeyValuePartialInformation
,
3551 if ((Status
== STATUS_BUFFER_TOO_SMALL
) ||
3552 (Status
== STATUS_BUFFER_OVERFLOW
))
3554 Length
= DesiredLength
;
3555 KeyInformation
= ExAllocatePool(PagedPool
, Length
);
3558 Status
= ZwQueryValueKey(hPnpKey
,
3560 KeyValuePartialInformation
,
3564 if (NT_SUCCESS(Status
) && KeyInformation
->DataLength
== sizeof(ULONG
))
3566 KeyValue
= (ULONG
)(*KeyInformation
->Data
);
3570 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed\n", &KeyPathU
, &KeyNameU
);
3573 ExFreePool(KeyInformation
);
3577 DPRINT1("Failed to allocate memory for registry query\n");
3582 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed with status 0x%08lx\n", &KeyPathU
, &KeyNameU
, Status
);
3589 DPRINT1("ZwOpenKey(%wZ) failed with status 0x%08lx\n", &KeyPathU
, Status
);
3592 DPRINT("Firmware mapper is %s\n", KeyValue
!= 0 ? "disabled" : "enabled");
3594 return (KeyValue
!= 0) ? TRUE
: FALSE
;
3600 IopUpdateRootKey(VOID
)
3602 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
3603 UNICODE_STRING RootPathU
= RTL_CONSTANT_STRING(L
"Root");
3604 UNICODE_STRING MultiKeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
3605 OBJECT_ATTRIBUTES ObjectAttributes
;
3606 HANDLE hEnum
, hRoot
;
3609 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
3610 Status
= ZwCreateKey(&hEnum
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, REG_OPTION_NON_VOLATILE
, NULL
);
3611 if (!NT_SUCCESS(Status
))
3613 DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3617 InitializeObjectAttributes(&ObjectAttributes
, &RootPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hEnum
, NULL
);
3618 Status
= ZwCreateKey(&hRoot
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, REG_OPTION_NON_VOLATILE
, NULL
);
3620 if (!NT_SUCCESS(Status
))
3622 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3626 if (!IopIsFirmwareMapperDisabled())
3628 Status
= IopOpenRegistryKeyEx(&hEnum
, NULL
, &MultiKeyPathU
, KEY_ENUMERATE_SUB_KEYS
);
3629 if (!NT_SUCCESS(Status
))
3631 /* Nothing to do, don't return with an error status */
3632 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3634 return STATUS_SUCCESS
;
3636 Status
= IopEnumerateDetectedDevices(
3647 /* Enumeration is disabled */
3648 Status
= STATUS_SUCCESS
;
3658 IopOpenRegistryKeyEx(PHANDLE KeyHandle
,
3660 PUNICODE_STRING Name
,
3661 ACCESS_MASK DesiredAccess
)
3663 OBJECT_ATTRIBUTES ObjectAttributes
;
3670 InitializeObjectAttributes(&ObjectAttributes
,
3672 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3676 Status
= ZwOpenKey(KeyHandle
, DesiredAccess
, &ObjectAttributes
);
3683 IopCreateRegistryKeyEx(OUT PHANDLE Handle
,
3684 IN HANDLE RootHandle OPTIONAL
,
3685 IN PUNICODE_STRING KeyName
,
3686 IN ACCESS_MASK DesiredAccess
,
3687 IN ULONG CreateOptions
,
3688 OUT PULONG Disposition OPTIONAL
)
3690 OBJECT_ATTRIBUTES ObjectAttributes
;
3691 ULONG KeyDisposition
, RootHandleIndex
= 0, i
= 1, NestedCloseLevel
= 0;
3693 HANDLE HandleArray
[2];
3694 BOOLEAN Recursing
= TRUE
;
3696 UNICODE_STRING KeyString
;
3697 NTSTATUS Status
= STATUS_SUCCESS
;
3700 /* P1 is start, pp is end */
3701 p1
= KeyName
->Buffer
;
3702 pp
= (PVOID
)((ULONG_PTR
)p1
+ KeyName
->Length
);
3704 /* Create the target key */
3705 InitializeObjectAttributes(&ObjectAttributes
,
3707 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3710 Status
= ZwCreateKey(&HandleArray
[i
],
3718 /* Now we check if this failed */
3719 if ((Status
== STATUS_OBJECT_NAME_NOT_FOUND
) && (RootHandle
))
3721 /* Target key failed, so we'll need to create its parent. Setup array */
3722 HandleArray
[0] = NULL
;
3723 HandleArray
[1] = RootHandle
;
3725 /* Keep recursing for each missing parent */
3728 /* And if we're deep enough, close the last handle */
3729 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
3731 /* We're setup to ping-pong between the two handle array entries */
3732 RootHandleIndex
= i
;
3735 /* Clear the one we're attempting to open now */
3736 HandleArray
[i
] = NULL
;
3738 /* Process the parent key name */
3739 for (p
= p1
; ((p
< pp
) && (*p
!= OBJ_NAME_PATH_SEPARATOR
)); p
++);
3740 Length
= (USHORT
)(p
- p1
) * sizeof(WCHAR
);
3742 /* Is there a parent name? */
3745 /* Build the unicode string for it */
3746 KeyString
.Buffer
= p1
;
3747 KeyString
.Length
= KeyString
.MaximumLength
= Length
;
3749 /* Now try opening the parent */
3750 InitializeObjectAttributes(&ObjectAttributes
,
3752 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3753 HandleArray
[RootHandleIndex
],
3755 Status
= ZwCreateKey(&HandleArray
[i
],
3762 if (NT_SUCCESS(Status
))
3764 /* It worked, we have one more handle */
3769 /* Parent key creation failed, abandon loop */
3776 /* We don't have a parent name, probably corrupted key name */
3777 Status
= STATUS_INVALID_PARAMETER
;
3782 /* Now see if there's more parents to create */
3784 if ((p
== pp
) || (p1
== pp
))
3786 /* We're done, hopefully successfully, so stop */
3791 /* Outer loop check for handle nesting that requires closing the top handle */
3792 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
3795 /* Check if we broke out of the loop due to success */
3796 if (NT_SUCCESS(Status
))
3798 /* Return the target handle (we closed all the parent ones) and disposition */
3799 *Handle
= HandleArray
[i
];
3800 if (Disposition
) *Disposition
= KeyDisposition
;
3803 /* Return the success state */
3809 IopGetRegistryValue(IN HANDLE Handle
,
3811 OUT PKEY_VALUE_FULL_INFORMATION
*Information
)
3813 UNICODE_STRING ValueString
;
3815 PKEY_VALUE_FULL_INFORMATION FullInformation
;
3819 RtlInitUnicodeString(&ValueString
, ValueName
);
3821 Status
= ZwQueryValueKey(Handle
,
3823 KeyValueFullInformation
,
3827 if ((Status
!= STATUS_BUFFER_OVERFLOW
) &&
3828 (Status
!= STATUS_BUFFER_TOO_SMALL
))
3833 FullInformation
= ExAllocatePool(NonPagedPool
, Size
);
3834 if (!FullInformation
) return STATUS_INSUFFICIENT_RESOURCES
;
3836 Status
= ZwQueryValueKey(Handle
,
3838 KeyValueFullInformation
,
3842 if (!NT_SUCCESS(Status
))
3844 ExFreePool(FullInformation
);
3848 *Information
= FullInformation
;
3849 return STATUS_SUCCESS
;
3852 RTL_GENERIC_COMPARE_RESULTS
3854 PiCompareInstancePath(IN PRTL_AVL_TABLE Table
,
3855 IN PVOID FirstStruct
,
3856 IN PVOID SecondStruct
)
3864 // The allocation function is called by the generic table package whenever
3865 // it needs to allocate memory for the table.
3870 PiAllocateGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3880 PiFreeGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3889 PpInitializeDeviceReferenceTable(VOID
)
3891 /* Setup the guarded mutex and AVL table */
3892 KeInitializeGuardedMutex(&PpDeviceReferenceTableLock
);
3893 RtlInitializeGenericTableAvl(
3894 &PpDeviceReferenceTable
,
3895 (PRTL_AVL_COMPARE_ROUTINE
)PiCompareInstancePath
,
3896 (PRTL_AVL_ALLOCATE_ROUTINE
)PiAllocateGenericTableEntry
,
3897 (PRTL_AVL_FREE_ROUTINE
)PiFreeGenericTableEntry
,
3905 /* Initialize the resource when accessing device registry data */
3906 ExInitializeResourceLite(&PpRegistryDeviceResource
);
3908 /* Setup the device reference AVL table */
3909 PpInitializeDeviceReferenceTable();
3917 /* Check the initialization phase */
3918 switch (ExpInitializationPhase
)
3923 return PiInitPhase0();
3929 //return PiInitPhase1();
3933 /* Don't know any other phase! Bugcheck! */
3934 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
3939 LONG IopNumberDeviceNodes
;
3943 PipAllocateDeviceNode(IN PDEVICE_OBJECT PhysicalDeviceObject
)
3945 PDEVICE_NODE DeviceNode
;
3949 DeviceNode
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(DEVICE_NODE
), TAG_IO_DEVNODE
);
3950 if (!DeviceNode
) return DeviceNode
;
3953 InterlockedIncrement(&IopNumberDeviceNodes
);
3956 RtlZeroMemory(DeviceNode
, sizeof(DEVICE_NODE
));
3957 DeviceNode
->InterfaceType
= InterfaceTypeUndefined
;
3958 DeviceNode
->BusNumber
= -1;
3959 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
3960 DeviceNode
->ChildBusNumber
= -1;
3961 DeviceNode
->ChildBusTypeIndex
= -1;
3962 // KeInitializeEvent(&DeviceNode->EnumerationMutex, SynchronizationEvent, TRUE);
3963 InitializeListHead(&DeviceNode
->DeviceArbiterList
);
3964 InitializeListHead(&DeviceNode
->DeviceTranslatorList
);
3965 InitializeListHead(&DeviceNode
->TargetDeviceNotify
);
3966 InitializeListHead(&DeviceNode
->DockInfo
.ListEntry
);
3967 InitializeListHead(&DeviceNode
->PendedSetInterfaceState
);
3969 /* Check if there is a PDO */
3970 if (PhysicalDeviceObject
)
3972 /* Link it and remove the init flag */
3973 DeviceNode
->PhysicalDeviceObject
= PhysicalDeviceObject
;
3974 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= DeviceNode
;
3975 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
3978 /* Return the node */
3982 /* PUBLIC FUNCTIONS **********************************************************/
3986 PnpBusTypeGuidGet(IN USHORT Index
,
3987 IN LPGUID BusTypeGuid
)
3989 NTSTATUS Status
= STATUS_SUCCESS
;
3991 /* Acquire the lock */
3992 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
3995 if (Index
< PnpBusTypeGuidList
->GuidCount
)
3998 RtlCopyMemory(BusTypeGuid
, &PnpBusTypeGuidList
->Guids
[Index
], sizeof(GUID
));
4003 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
4006 /* Release lock and return status */
4007 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
4013 PnpDeviceObjectToDeviceInstance(IN PDEVICE_OBJECT DeviceObject
,
4014 IN PHANDLE DeviceInstanceHandle
,
4015 IN ACCESS_MASK DesiredAccess
)
4019 PDEVICE_NODE DeviceNode
;
4020 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\ENUM");
4023 /* Open the enum key */
4024 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
4028 if (!NT_SUCCESS(Status
)) return Status
;
4030 /* Make sure we have an instance path */
4031 DeviceNode
= IopGetDeviceNode(DeviceObject
);
4032 if ((DeviceNode
) && (DeviceNode
->InstancePath
.Length
))
4034 /* Get the instance key */
4035 Status
= IopOpenRegistryKeyEx(DeviceInstanceHandle
,
4037 &DeviceNode
->InstancePath
,
4043 Status
= STATUS_INVALID_DEVICE_REQUEST
;
4046 /* Close the handle and return status */
4053 PnpDetermineResourceListSize(IN PCM_RESOURCE_LIST ResourceList
)
4055 ULONG FinalSize
, PartialSize
, EntrySize
, i
, j
;
4056 PCM_FULL_RESOURCE_DESCRIPTOR FullDescriptor
;
4057 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
4059 /* If we don't have one, that's easy */
4060 if (!ResourceList
) return 0;
4062 /* Start with the minimum size possible */
4063 FinalSize
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
4065 /* Loop each full descriptor */
4066 FullDescriptor
= ResourceList
->List
;
4067 for (i
= 0; i
< ResourceList
->Count
; i
++)
4069 /* Start with the minimum size possible */
4070 PartialSize
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
) +
4071 FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST
, PartialDescriptors
);
4073 /* Loop each partial descriptor */
4074 PartialDescriptor
= FullDescriptor
->PartialResourceList
.PartialDescriptors
;
4075 for (j
= 0; j
< FullDescriptor
->PartialResourceList
.Count
; j
++)
4077 /* Start with the minimum size possible */
4078 EntrySize
= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
4080 /* Check if there is extra data */
4081 if (PartialDescriptor
->Type
== CmResourceTypeDeviceSpecific
)
4084 EntrySize
+= PartialDescriptor
->u
.DeviceSpecificData
.DataSize
;
4087 /* The size of partial descriptors is bigger */
4088 PartialSize
+= EntrySize
;
4090 /* Go to the next partial descriptor */
4091 PartialDescriptor
= (PVOID
)((ULONG_PTR
)PartialDescriptor
+ EntrySize
);
4094 /* The size of full descriptors is bigger */
4095 FinalSize
+= PartialSize
;
4097 /* Go to the next full descriptor */
4098 FullDescriptor
= (PVOID
)((ULONG_PTR
)FullDescriptor
+ PartialSize
);
4101 /* Return the final size */
4107 PiGetDeviceRegistryProperty(IN PDEVICE_OBJECT DeviceObject
,
4112 IN PULONG BufferLength
)
4115 HANDLE KeyHandle
, SubHandle
;
4116 UNICODE_STRING KeyString
;
4117 PKEY_VALUE_FULL_INFORMATION KeyValueInfo
= NULL
;
4121 /* Find the instance key */
4122 Status
= PnpDeviceObjectToDeviceInstance(DeviceObject
, &KeyHandle
, KEY_READ
);
4123 if (NT_SUCCESS(Status
))
4125 /* Check for name given by caller */
4129 RtlInitUnicodeString(&KeyString
, KeyName
);
4130 Status
= IopOpenRegistryKeyEx(&SubHandle
,
4134 if (NT_SUCCESS(Status
))
4136 /* And use this handle instead */
4138 KeyHandle
= SubHandle
;
4142 /* Check if sub-key handle succeeded (or no-op if no key name given) */
4143 if (NT_SUCCESS(Status
))
4145 /* Now get the size of the property */
4146 Status
= IopGetRegistryValue(KeyHandle
,
4155 /* Fail if any of the registry operations failed */
4156 if (!NT_SUCCESS(Status
)) return Status
;
4158 /* Check how much data we have to copy */
4159 Length
= KeyValueInfo
->DataLength
;
4160 if (*BufferLength
>= Length
)
4162 /* Check for a match in the value type */
4163 if (KeyValueInfo
->Type
== ValueType
)
4166 RtlCopyMemory(Buffer
,
4167 (PVOID
)((ULONG_PTR
)KeyValueInfo
+
4168 KeyValueInfo
->DataOffset
),
4173 /* Invalid registry property type, fail */
4174 Status
= STATUS_INVALID_PARAMETER_2
;
4179 /* Buffer is too small to hold data */
4180 Status
= STATUS_BUFFER_TOO_SMALL
;
4183 /* Return the required buffer length, free the buffer, and return status */
4184 *BufferLength
= Length
;
4185 ExFreePool(KeyValueInfo
);
4189 #define PIP_RETURN_DATA(x, y) {ReturnLength = x; Data = y; Status = STATUS_SUCCESS; break;}
4190 #define PIP_REGISTRY_DATA(x, y) {ValueName = x; ValueType = y; break;}
4191 #define PIP_UNIMPLEMENTED() {UNIMPLEMENTED_DBGBREAK(); break;}
4198 IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject
,
4199 IN DEVICE_REGISTRY_PROPERTY DeviceProperty
,
4200 IN ULONG BufferLength
,
4201 OUT PVOID PropertyBuffer
,
4202 OUT PULONG ResultLength
)
4204 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
4205 DEVICE_CAPABILITIES DeviceCaps
;
4206 ULONG ReturnLength
= 0, Length
= 0, ValueType
;
4207 PWCHAR ValueName
= NULL
, EnumeratorNameEnd
, DeviceInstanceName
;
4209 NTSTATUS Status
= STATUS_BUFFER_TOO_SMALL
;
4211 POBJECT_NAME_INFORMATION ObjectNameInfo
= NULL
;
4212 BOOLEAN NullTerminate
= FALSE
;
4214 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject
, DeviceProperty
);
4216 /* Assume failure */
4219 /* Only PDOs can call this */
4220 if (!DeviceNode
) return STATUS_INVALID_DEVICE_REQUEST
;
4222 /* Handle all properties */
4223 switch (DeviceProperty
)
4225 case DevicePropertyBusTypeGuid
:
4227 /* Get the GUID from the internal cache */
4228 Status
= PnpBusTypeGuidGet(DeviceNode
->ChildBusTypeIndex
, &BusTypeGuid
);
4229 if (!NT_SUCCESS(Status
)) return Status
;
4231 /* This is the format of the returned data */
4232 PIP_RETURN_DATA(sizeof(GUID
), &BusTypeGuid
);
4234 case DevicePropertyLegacyBusType
:
4236 /* Validate correct interface type */
4237 if (DeviceNode
->ChildInterfaceType
== InterfaceTypeUndefined
)
4238 return STATUS_OBJECT_NAME_NOT_FOUND
;
4240 /* This is the format of the returned data */
4241 PIP_RETURN_DATA(sizeof(INTERFACE_TYPE
), &DeviceNode
->ChildInterfaceType
);
4243 case DevicePropertyBusNumber
:
4245 /* Validate correct bus number */
4246 if ((DeviceNode
->ChildBusNumber
& 0x80000000) == 0x80000000)
4247 return STATUS_OBJECT_NAME_NOT_FOUND
;
4249 /* This is the format of the returned data */
4250 PIP_RETURN_DATA(sizeof(ULONG
), &DeviceNode
->ChildBusNumber
);
4252 case DevicePropertyEnumeratorName
:
4254 /* Get the instance path */
4255 DeviceInstanceName
= DeviceNode
->InstancePath
.Buffer
;
4258 ASSERT((BufferLength
& 1) == 0);
4259 ASSERT(DeviceInstanceName
!= NULL
);
4261 /* Get the name from the path */
4262 EnumeratorNameEnd
= wcschr(DeviceInstanceName
, OBJ_NAME_PATH_SEPARATOR
);
4263 ASSERT(EnumeratorNameEnd
);
4265 /* This string needs to be NULL-terminated */
4266 NullTerminate
= TRUE
;
4268 /* This is the format of the returned data */
4269 PIP_RETURN_DATA((ULONG
)(EnumeratorNameEnd
- DeviceInstanceName
) * sizeof(WCHAR
),
4270 DeviceInstanceName
);
4272 case DevicePropertyAddress
:
4274 /* Query the device caps */
4275 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
);
4276 if (!NT_SUCCESS(Status
) || (DeviceCaps
.Address
== MAXULONG
))
4277 return STATUS_OBJECT_NAME_NOT_FOUND
;
4279 /* This is the format of the returned data */
4280 PIP_RETURN_DATA(sizeof(ULONG
), &DeviceCaps
.Address
);
4282 case DevicePropertyBootConfigurationTranslated
:
4284 /* Validate we have resources */
4285 if (!DeviceNode
->BootResources
)
4286 // if (!DeviceNode->BootResourcesTranslated) // FIXFIX: Need this field
4288 /* No resources will still fake success, but with 0 bytes */
4290 return STATUS_SUCCESS
;
4293 /* This is the format of the returned data */
4294 PIP_RETURN_DATA(PnpDetermineResourceListSize(DeviceNode
->BootResources
), // FIXFIX: Should use BootResourcesTranslated
4295 DeviceNode
->BootResources
); // FIXFIX: Should use BootResourcesTranslated
4297 case DevicePropertyPhysicalDeviceObjectName
:
4299 /* Sanity check for Unicode-sized string */
4300 ASSERT((BufferLength
& 1) == 0);
4302 /* Allocate name buffer */
4303 Length
= BufferLength
+ sizeof(OBJECT_NAME_INFORMATION
);
4304 ObjectNameInfo
= ExAllocatePool(PagedPool
, Length
);
4305 if (!ObjectNameInfo
) return STATUS_INSUFFICIENT_RESOURCES
;
4307 /* Query the PDO name */
4308 Status
= ObQueryNameString(DeviceObject
,
4312 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
4314 /* It's up to the caller to try again */
4315 Status
= STATUS_BUFFER_TOO_SMALL
;
4318 /* This string needs to be NULL-terminated */
4319 NullTerminate
= TRUE
;
4321 /* Return if successful */
4322 if (NT_SUCCESS(Status
)) PIP_RETURN_DATA(ObjectNameInfo
->Name
.Length
,
4323 ObjectNameInfo
->Name
.Buffer
);
4325 /* Let the caller know how big the name is */
4326 *ResultLength
-= sizeof(OBJECT_NAME_INFORMATION
);
4329 case DevicePropertyRemovalPolicy
:
4330 PIP_RETURN_DATA(sizeof(UCHAR
), &DeviceNode
->RemovalPolicy
);
4332 /* Handle the registry-based properties */
4333 case DevicePropertyUINumber
:
4334 PIP_REGISTRY_DATA(REGSTR_VAL_UI_NUMBER
, REG_DWORD
);
4335 case DevicePropertyLocationInformation
:
4336 PIP_REGISTRY_DATA(REGSTR_VAL_LOCATION_INFORMATION
, REG_SZ
);
4337 case DevicePropertyDeviceDescription
:
4338 PIP_REGISTRY_DATA(REGSTR_VAL_DEVDESC
, REG_SZ
);
4339 case DevicePropertyHardwareID
:
4340 PIP_REGISTRY_DATA(REGSTR_VAL_HARDWAREID
, REG_MULTI_SZ
);
4341 case DevicePropertyCompatibleIDs
:
4342 PIP_REGISTRY_DATA(REGSTR_VAL_COMPATIBLEIDS
, REG_MULTI_SZ
);
4343 case DevicePropertyBootConfiguration
:
4344 PIP_REGISTRY_DATA(REGSTR_VAL_BOOTCONFIG
, REG_RESOURCE_LIST
);
4345 case DevicePropertyClassName
:
4346 PIP_REGISTRY_DATA(REGSTR_VAL_CLASS
, REG_SZ
);
4347 case DevicePropertyClassGuid
:
4348 PIP_REGISTRY_DATA(REGSTR_VAL_CLASSGUID
, REG_SZ
);
4349 case DevicePropertyDriverKeyName
:
4350 PIP_REGISTRY_DATA(REGSTR_VAL_DRIVER
, REG_SZ
);
4351 case DevicePropertyManufacturer
:
4352 PIP_REGISTRY_DATA(REGSTR_VAL_MFG
, REG_SZ
);
4353 case DevicePropertyFriendlyName
:
4354 PIP_REGISTRY_DATA(REGSTR_VAL_FRIENDLYNAME
, REG_SZ
);
4355 case DevicePropertyContainerID
:
4356 //PIP_REGISTRY_DATA(REGSTR_VAL_CONTAINERID, REG_SZ); // Win7
4357 PIP_UNIMPLEMENTED();
4359 case DevicePropertyInstallState
:
4360 PIP_REGISTRY_DATA(REGSTR_VAL_CONFIGFLAGS
, REG_DWORD
);
4362 case DevicePropertyResourceRequirements
:
4363 PIP_UNIMPLEMENTED();
4364 case DevicePropertyAllocatedResources
:
4365 PIP_UNIMPLEMENTED();
4367 return STATUS_INVALID_PARAMETER_2
;
4370 /* Having a registry value name implies registry data */
4373 /* We know up-front how much data to expect */
4374 *ResultLength
= BufferLength
;
4376 /* Go get the data, use the LogConf subkey if necessary */
4377 Status
= PiGetDeviceRegistryProperty(DeviceObject
,
4381 DevicePropertyBootConfiguration
) ?
4386 else if (NT_SUCCESS(Status
))
4388 /* We know up-front how much data to expect, check the caller's buffer */
4389 *ResultLength
= ReturnLength
+ (NullTerminate
? sizeof(UNICODE_NULL
) : 0);
4390 if (*ResultLength
<= BufferLength
)
4392 /* Buffer is all good, copy the data */
4393 RtlCopyMemory(PropertyBuffer
, Data
, ReturnLength
);
4395 /* Check if we need to NULL-terminate the string */
4398 /* Terminate the string */
4399 ((PWCHAR
)PropertyBuffer
)[ReturnLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
4402 /* This is the success path */
4403 Status
= STATUS_SUCCESS
;
4408 Status
= STATUS_BUFFER_TOO_SMALL
;
4412 /* Free any allocation we may have made, and return the status code */
4413 if (ObjectNameInfo
) ExFreePool(ObjectNameInfo
);
4422 IoInvalidateDeviceState(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4424 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(PhysicalDeviceObject
);
4425 IO_STACK_LOCATION Stack
;
4428 IO_STATUS_BLOCK IoStatusBlock
;
4430 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
4431 Stack
.MajorFunction
= IRP_MJ_PNP
;
4432 Stack
.MinorFunction
= IRP_MN_QUERY_PNP_DEVICE_STATE
;
4434 Status
= IopSynchronousCall(PhysicalDeviceObject
, &Stack
, (PVOID
*)&PnPFlags
);
4435 if (!NT_SUCCESS(Status
))
4437 if (Status
!= STATUS_NOT_SUPPORTED
)
4439 DPRINT1("IRP_MN_QUERY_PNP_DEVICE_STATE failed with status 0x%lx\n", Status
);
4444 if (PnPFlags
& PNP_DEVICE_NOT_DISABLEABLE
)
4445 DeviceNode
->UserFlags
|= DNUF_NOT_DISABLEABLE
;
4447 DeviceNode
->UserFlags
&= ~DNUF_NOT_DISABLEABLE
;
4449 if (PnPFlags
& PNP_DEVICE_DONT_DISPLAY_IN_UI
)
4450 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
4452 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
4454 if ((PnPFlags
& PNP_DEVICE_REMOVED
) ||
4455 ((PnPFlags
& PNP_DEVICE_FAILED
) && !(PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
)))
4457 /* Flag it if it's failed */
4458 if (PnPFlags
& PNP_DEVICE_FAILED
) DeviceNode
->Problem
= CM_PROB_FAILED_POST_START
;
4460 /* Send removal IRPs to all of its children */
4461 IopPrepareDeviceForRemoval(PhysicalDeviceObject
, TRUE
);
4463 /* Send surprise removal */
4464 IopSendSurpriseRemoval(PhysicalDeviceObject
);
4466 /* Tell the user-mode PnP manager that a device was removed */
4467 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
4468 &DeviceNode
->InstancePath
);
4470 IopSendRemoveDevice(PhysicalDeviceObject
);
4472 else if ((PnPFlags
& PNP_DEVICE_FAILED
) && (PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
))
4474 /* Stop for resource rebalance */
4475 Status
= IopStopDevice(DeviceNode
);
4476 if (!NT_SUCCESS(Status
))
4478 DPRINT1("Failed to stop device for rebalancing\n");
4480 /* Stop failed so don't rebalance */
4481 PnPFlags
&= ~PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
;
4485 /* Resource rebalance */
4486 if (PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
)
4488 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
4490 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
4492 IRP_MN_QUERY_RESOURCES
,
4494 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
4496 DeviceNode
->BootResources
=
4497 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
4498 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
4502 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
4503 DeviceNode
->BootResources
= NULL
;
4506 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
4508 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
4510 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
4512 if (NT_SUCCESS(Status
))
4514 DeviceNode
->ResourceRequirements
=
4515 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
4519 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
4520 DeviceNode
->ResourceRequirements
= NULL
;
4523 /* IRP_MN_FILTER_RESOURCE_REQUIREMENTS is called indirectly by IopStartDevice */
4524 if (IopStartDevice(DeviceNode
) != STATUS_SUCCESS
)
4526 DPRINT1("Restart after resource rebalance failed\n");
4528 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
4529 DeviceNode
->Flags
|= DNF_START_FAILED
;
4531 IopRemoveDevice(DeviceNode
);
4537 * @name IoOpenDeviceRegistryKey
4539 * Open a registry key unique for a specified driver or device instance.
4541 * @param DeviceObject Device to get the registry key for.
4542 * @param DevInstKeyType Type of the key to return.
4543 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
4544 * @param DevInstRegKey Handle to the opened registry key on
4545 * successful return.
4553 IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject
,
4554 IN ULONG DevInstKeyType
,
4555 IN ACCESS_MASK DesiredAccess
,
4556 OUT PHANDLE DevInstRegKey
)
4558 static WCHAR RootKeyName
[] =
4559 L
"\\Registry\\Machine\\System\\CurrentControlSet\\";
4560 static WCHAR ProfileKeyName
[] =
4561 L
"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
4562 static WCHAR ClassKeyName
[] = L
"Control\\Class\\";
4563 static WCHAR EnumKeyName
[] = L
"Enum\\";
4564 static WCHAR DeviceParametersKeyName
[] = L
"Device Parameters";
4565 ULONG KeyNameLength
;
4566 PWSTR KeyNameBuffer
;
4567 UNICODE_STRING KeyName
;
4568 ULONG DriverKeyLength
;
4569 OBJECT_ATTRIBUTES ObjectAttributes
;
4570 PDEVICE_NODE DeviceNode
= NULL
;
4573 DPRINT("IoOpenDeviceRegistryKey() called\n");
4575 if ((DevInstKeyType
& (PLUGPLAY_REGKEY_DEVICE
| PLUGPLAY_REGKEY_DRIVER
)) == 0)
4577 DPRINT1("IoOpenDeviceRegistryKey(): got wrong params, exiting... \n");
4578 return STATUS_INVALID_PARAMETER
;
4581 if (!IopIsValidPhysicalDeviceObject(DeviceObject
))
4582 return STATUS_INVALID_DEVICE_REQUEST
;
4583 DeviceNode
= IopGetDeviceNode(DeviceObject
);
4586 * Calculate the length of the base key name. This is the full
4587 * name for driver key or the name excluding "Device Parameters"
4588 * subkey for device key.
4591 KeyNameLength
= sizeof(RootKeyName
);
4592 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
4593 KeyNameLength
+= sizeof(ProfileKeyName
) - sizeof(UNICODE_NULL
);
4594 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4596 KeyNameLength
+= sizeof(ClassKeyName
) - sizeof(UNICODE_NULL
);
4597 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
4598 0, NULL
, &DriverKeyLength
);
4599 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
4601 KeyNameLength
+= DriverKeyLength
;
4605 KeyNameLength
+= sizeof(EnumKeyName
) - sizeof(UNICODE_NULL
) +
4606 DeviceNode
->InstancePath
.Length
;
4610 * Now allocate the buffer for the key name...
4613 KeyNameBuffer
= ExAllocatePool(PagedPool
, KeyNameLength
);
4614 if (KeyNameBuffer
== NULL
)
4615 return STATUS_INSUFFICIENT_RESOURCES
;
4618 KeyName
.MaximumLength
= (USHORT
)KeyNameLength
;
4619 KeyName
.Buffer
= KeyNameBuffer
;
4622 * ...and build the key name.
4625 KeyName
.Length
+= sizeof(RootKeyName
) - sizeof(UNICODE_NULL
);
4626 RtlCopyMemory(KeyNameBuffer
, RootKeyName
, KeyName
.Length
);
4628 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
4629 RtlAppendUnicodeToString(&KeyName
, ProfileKeyName
);
4631 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4633 RtlAppendUnicodeToString(&KeyName
, ClassKeyName
);
4634 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
4635 DriverKeyLength
, KeyNameBuffer
+
4636 (KeyName
.Length
/ sizeof(WCHAR
)),
4638 if (!NT_SUCCESS(Status
))
4640 DPRINT1("Call to IoGetDeviceProperty() failed with Status 0x%08lx\n", Status
);
4641 ExFreePool(KeyNameBuffer
);
4644 KeyName
.Length
+= (USHORT
)DriverKeyLength
- sizeof(UNICODE_NULL
);
4648 RtlAppendUnicodeToString(&KeyName
, EnumKeyName
);
4649 Status
= RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->InstancePath
);
4650 if (DeviceNode
->InstancePath
.Length
== 0)
4652 ExFreePool(KeyNameBuffer
);
4658 * Open the base key.
4660 Status
= IopOpenRegistryKeyEx(DevInstRegKey
, NULL
, &KeyName
, DesiredAccess
);
4661 if (!NT_SUCCESS(Status
))
4663 DPRINT1("IoOpenDeviceRegistryKey(%wZ): Base key doesn't exist, exiting... (Status 0x%08lx)\n", &KeyName
, Status
);
4664 ExFreePool(KeyNameBuffer
);
4667 ExFreePool(KeyNameBuffer
);
4670 * For driver key we're done now.
4673 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4677 * Let's go further. For device key we must open "Device Parameters"
4678 * subkey and create it if it doesn't exist yet.
4681 RtlInitUnicodeString(&KeyName
, DeviceParametersKeyName
);
4682 InitializeObjectAttributes(&ObjectAttributes
,
4684 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
4687 Status
= ZwCreateKey(DevInstRegKey
,
4692 REG_OPTION_NON_VOLATILE
,
4694 ZwClose(ObjectAttributes
.RootDirectory
);
4701 IopQueryRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
, BOOLEAN Force
)
4703 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
, FailedRemoveDevice
;
4707 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4708 ChildDeviceNode
= ParentDeviceNode
->Child
;
4709 while (ChildDeviceNode
!= NULL
)
4711 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4712 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4714 Status
= IopPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
, Force
);
4715 if (!NT_SUCCESS(Status
))
4717 FailedRemoveDevice
= ChildDeviceNode
;
4721 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4722 ChildDeviceNode
= NextDeviceNode
;
4724 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4726 return STATUS_SUCCESS
;
4729 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4730 ChildDeviceNode
= ParentDeviceNode
->Child
;
4731 while (ChildDeviceNode
!= NULL
)
4733 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4734 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4736 IopCancelPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
4738 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
4739 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
4740 if (ChildDeviceNode
== FailedRemoveDevice
)
4743 ChildDeviceNode
= NextDeviceNode
;
4745 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4747 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4754 IopSendRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
4756 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
;
4759 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4760 ChildDeviceNode
= ParentDeviceNode
->Child
;
4761 while (ChildDeviceNode
!= NULL
)
4763 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4764 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4766 IopSendRemoveDevice(ChildDeviceNode
->PhysicalDeviceObject
);
4768 ChildDeviceNode
= NextDeviceNode
;
4770 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4772 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4777 IopCancelRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
4779 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
;
4782 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4783 ChildDeviceNode
= ParentDeviceNode
->Child
;
4784 while (ChildDeviceNode
!= NULL
)
4786 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4787 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4789 IopCancelPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
4791 ChildDeviceNode
= NextDeviceNode
;
4793 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4795 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4800 IopQueryRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
, BOOLEAN Force
)
4802 /* This function DOES NOT dereference the device objects on SUCCESS
4803 * but it DOES dereference device objects on FAILURE */
4808 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4810 Status
= IopPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
], Force
);
4811 if (!NT_SUCCESS(Status
))
4818 return STATUS_SUCCESS
;
4821 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
4822 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
4823 for (i
= 0; i
<= j
; i
++)
4825 IopCancelPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4826 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4827 DeviceRelations
->Objects
[i
] = NULL
;
4829 for (; i
< DeviceRelations
->Count
; i
++)
4831 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4832 DeviceRelations
->Objects
[i
] = NULL
;
4834 ExFreePool(DeviceRelations
);
4841 IopSendRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4843 /* This function DOES dereference the device objects in all cases */
4847 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4849 IopSendRemoveDevice(DeviceRelations
->Objects
[i
]);
4850 DeviceRelations
->Objects
[i
] = NULL
;
4853 ExFreePool(DeviceRelations
);
4858 IopCancelRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4860 /* This function DOES dereference the device objects in all cases */
4864 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4866 IopCancelPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4867 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4868 DeviceRelations
->Objects
[i
] = NULL
;
4871 ExFreePool(DeviceRelations
);
4875 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
)
4877 IO_STACK_LOCATION Stack
;
4878 IO_STATUS_BLOCK IoStatusBlock
;
4879 PDEVICE_RELATIONS DeviceRelations
;
4882 IopCancelRemoveDevice(DeviceObject
);
4884 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
4886 Status
= IopInitiatePnpIrp(DeviceObject
,
4888 IRP_MN_QUERY_DEVICE_RELATIONS
,
4890 if (!NT_SUCCESS(Status
))
4892 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4893 DeviceRelations
= NULL
;
4897 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4900 if (DeviceRelations
)
4901 IopCancelRemoveDeviceRelations(DeviceRelations
);
4905 IopPrepareDeviceForRemoval(IN PDEVICE_OBJECT DeviceObject
, BOOLEAN Force
)
4907 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
4908 IO_STACK_LOCATION Stack
;
4909 IO_STATUS_BLOCK IoStatusBlock
;
4910 PDEVICE_RELATIONS DeviceRelations
;
4913 if ((DeviceNode
->UserFlags
& DNUF_NOT_DISABLEABLE
) && !Force
)
4915 DPRINT1("Removal not allowed for %wZ\n", &DeviceNode
->InstancePath
);
4916 return STATUS_UNSUCCESSFUL
;
4919 if (!Force
&& IopQueryRemoveDevice(DeviceObject
) != STATUS_SUCCESS
)
4921 DPRINT1("Removal vetoed by failing the query remove request\n");
4923 IopCancelRemoveDevice(DeviceObject
);
4925 return STATUS_UNSUCCESSFUL
;
4928 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
4930 Status
= IopInitiatePnpIrp(DeviceObject
,
4932 IRP_MN_QUERY_DEVICE_RELATIONS
,
4934 if (!NT_SUCCESS(Status
))
4936 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4937 DeviceRelations
= NULL
;
4941 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4944 if (DeviceRelations
)
4946 Status
= IopQueryRemoveDeviceRelations(DeviceRelations
, Force
);
4947 if (!NT_SUCCESS(Status
))
4951 Status
= IopQueryRemoveChildDevices(DeviceNode
, Force
);
4952 if (!NT_SUCCESS(Status
))
4954 if (DeviceRelations
)
4955 IopCancelRemoveDeviceRelations(DeviceRelations
);
4959 if (DeviceRelations
)
4960 IopSendRemoveDeviceRelations(DeviceRelations
);
4961 IopSendRemoveChildDevices(DeviceNode
);
4963 return STATUS_SUCCESS
;
4967 IopRemoveDevice(PDEVICE_NODE DeviceNode
)
4971 DPRINT("Removing device: %wZ\n", &DeviceNode
->InstancePath
);
4973 Status
= IopPrepareDeviceForRemoval(DeviceNode
->PhysicalDeviceObject
, FALSE
);
4974 if (NT_SUCCESS(Status
))
4976 IopSendRemoveDevice(DeviceNode
->PhysicalDeviceObject
);
4977 IopQueueTargetDeviceEvent(&GUID_DEVICE_SAFE_REMOVAL
,
4978 &DeviceNode
->InstancePath
);
4979 return STATUS_SUCCESS
;
4990 IoRequestDeviceEject(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4992 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(PhysicalDeviceObject
);
4993 PDEVICE_RELATIONS DeviceRelations
;
4994 IO_STATUS_BLOCK IoStatusBlock
;
4995 IO_STACK_LOCATION Stack
;
4996 DEVICE_CAPABILITIES Capabilities
;
4999 IopQueueTargetDeviceEvent(&GUID_DEVICE_KERNEL_INITIATED_EJECT
,
5000 &DeviceNode
->InstancePath
);
5002 if (IopQueryDeviceCapabilities(DeviceNode
, &Capabilities
) != STATUS_SUCCESS
)
5007 Stack
.Parameters
.QueryDeviceRelations
.Type
= EjectionRelations
;
5009 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
5011 IRP_MN_QUERY_DEVICE_RELATIONS
,
5013 if (!NT_SUCCESS(Status
))
5015 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
5016 DeviceRelations
= NULL
;
5020 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
5023 if (DeviceRelations
)
5025 Status
= IopQueryRemoveDeviceRelations(DeviceRelations
, FALSE
);
5026 if (!NT_SUCCESS(Status
))
5030 Status
= IopQueryRemoveChildDevices(DeviceNode
, FALSE
);
5031 if (!NT_SUCCESS(Status
))
5033 if (DeviceRelations
)
5034 IopCancelRemoveDeviceRelations(DeviceRelations
);
5038 if (IopPrepareDeviceForRemoval(PhysicalDeviceObject
, FALSE
) != STATUS_SUCCESS
)
5040 if (DeviceRelations
)
5041 IopCancelRemoveDeviceRelations(DeviceRelations
);
5042 IopCancelRemoveChildDevices(DeviceNode
);
5046 if (DeviceRelations
)
5047 IopSendRemoveDeviceRelations(DeviceRelations
);
5048 IopSendRemoveChildDevices(DeviceNode
);
5050 DeviceNode
->Problem
= CM_PROB_HELD_FOR_EJECT
;
5051 if (Capabilities
.EjectSupported
)
5053 if (IopSendEject(PhysicalDeviceObject
) != STATUS_SUCCESS
)
5060 DeviceNode
->Flags
|= DNF_DISABLED
;
5063 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT
,
5064 &DeviceNode
->InstancePath
);
5069 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT_VETOED
,
5070 &DeviceNode
->InstancePath
);
5078 IoInvalidateDeviceRelations(
5079 IN PDEVICE_OBJECT DeviceObject
,
5080 IN DEVICE_RELATION_TYPE Type
)
5082 PDEVICE_ACTION_DATA Data
;
5085 Data
= ExAllocatePoolWithTag(NonPagedPool
,
5086 sizeof(DEVICE_ACTION_DATA
),
5091 ObReferenceObject(DeviceObject
);
5092 Data
->DeviceObject
= DeviceObject
;
5095 KeAcquireSpinLock(&IopDeviceActionLock
, &OldIrql
);
5096 InsertTailList(&IopDeviceActionRequestList
, &Data
->RequestListEntry
);
5097 if (IopDeviceActionInProgress
)
5099 KeReleaseSpinLock(&IopDeviceActionLock
, OldIrql
);
5102 IopDeviceActionInProgress
= TRUE
;
5103 KeReleaseSpinLock(&IopDeviceActionLock
, OldIrql
);
5105 ExInitializeWorkItem(&IopDeviceActionWorkItem
,
5106 IopDeviceActionWorker
,
5108 ExQueueWorkItem(&IopDeviceActionWorkItem
,
5117 IoSynchronousInvalidateDeviceRelations(
5118 IN PDEVICE_OBJECT DeviceObject
,
5119 IN DEVICE_RELATION_TYPE Type
)
5126 /* Enumerate the device */
5127 return IopEnumerateDevice(DeviceObject
);
5128 case PowerRelations
:
5129 /* Not handled yet */
5130 return STATUS_NOT_IMPLEMENTED
;
5131 case TargetDeviceRelation
:
5133 return STATUS_SUCCESS
;
5135 /* Ejection relations are not supported */
5136 return STATUS_NOT_SUPPORTED
;
5145 IoTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
5147 IN PHYSICAL_ADDRESS BusAddress
,
5148 IN OUT PULONG AddressSpace
,
5149 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
5151 /* FIXME: Notify the resource arbiter */
5153 return HalTranslateBusAddress(InterfaceType
,