2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/io/pnpmgr/pnpinit.c
5 * PURPOSE: PnP Initialization Code
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 /* GLOBALS ********************************************************************/
17 typedef struct _IOPNP_DEVICE_EXTENSION
19 PWCHAR CompatibleIdList
;
20 ULONG CompatibleIdListSize
;
21 } IOPNP_DEVICE_EXTENSION
, *PIOPNP_DEVICE_EXTENSION
;
23 PUNICODE_STRING PiInitGroupOrderTable
;
24 USHORT PiInitGroupOrderTableCount
;
25 INTERFACE_TYPE PnpDefaultInterfaceType
;
27 /* FUNCTIONS ******************************************************************/
31 IopDetermineDefaultInterfaceType(VOID
)
33 /* FIXME: ReactOS doesn't support MicroChannel yet */
39 IopInitializeArbiters(VOID
)
42 return STATUS_SUCCESS
;
48 PiInitCacheGroupInformation(VOID
)
52 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
53 PUNICODE_STRING GroupTable
;
55 UNICODE_STRING GroupString
=
56 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet"
57 L
"\\Control\\ServiceGroupOrder");
59 /* ReactOS HACK for SETUPLDR */
60 if (KeLoaderBlock
->SetupLdrBlock
)
62 DPRINT1("WARNING!! In PiInitCacheGroupInformation, using ReactOS HACK for SETUPLDR!!\n");
65 PiInitGroupOrderTableCount
= 0;
66 PiInitGroupOrderTable
= (PVOID
)0xBABEB00B;
67 return STATUS_SUCCESS
;
70 /* Open the registry key */
71 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
75 if (NT_SUCCESS(Status
))
78 Status
= IopGetRegistryValue(KeyHandle
, L
"List", &KeyValueInformation
);
81 /* Make sure we got it */
82 if (NT_SUCCESS(Status
))
84 /* Make sure it's valid */
85 if ((KeyValueInformation
->Type
== REG_MULTI_SZ
) &&
86 (KeyValueInformation
->DataLength
))
88 /* Convert it to unicode strings */
89 Status
= PnpRegMultiSzToUnicodeStrings(KeyValueInformation
,
93 /* Cache it for later */
94 PiInitGroupOrderTable
= GroupTable
;
95 PiInitGroupOrderTableCount
= (USHORT
)Count
;
100 Status
= STATUS_UNSUCCESSFUL
;
103 /* Free the information */
104 ExFreePool(KeyValueInformation
);
114 PpInitGetGroupOrderIndex(IN HANDLE ServiceHandle
)
117 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
120 UNICODE_STRING Group
;
123 /* Make sure we have a cache */
124 if (!PiInitGroupOrderTable
) return -1;
126 /* If we don't have a handle, the rest is easy -- return the count */
127 if (!ServiceHandle
) return PiInitGroupOrderTableCount
+ 1;
129 /* Otherwise, get the group value */
130 Status
= IopGetRegistryValue(ServiceHandle
, L
"Group", &KeyValueInformation
);
131 if (!NT_SUCCESS(Status
)) return PiInitGroupOrderTableCount
;
133 /* Make sure we have a valid string */
134 ASSERT(KeyValueInformation
->Type
== REG_SZ
);
135 ASSERT(KeyValueInformation
->DataLength
);
137 /* Convert to unicode string */
138 Buffer
= (PVOID
)((ULONG_PTR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
139 PnpRegSzToString(Buffer
, KeyValueInformation
->DataLength
, &Group
.Length
);
140 Group
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
141 Group
.Buffer
= Buffer
;
143 /* Loop the groups */
144 for (i
= 0; i
< PiInitGroupOrderTableCount
; i
++)
146 /* Try to find a match */
147 if (RtlEqualUnicodeString(&Group
, &PiInitGroupOrderTable
[i
], TRUE
)) break;
151 ExFreePool(KeyValueInformation
);
157 PipGetDriverTagPriority(IN HANDLE ServiceHandle
)
160 HANDLE KeyHandle
= NULL
;
161 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
= NULL
;
162 PKEY_VALUE_FULL_INFORMATION KeyValueInformationTag
;
163 PKEY_VALUE_FULL_INFORMATION KeyValueInformationGroupOrderList
;
165 UNICODE_STRING Group
;
167 ULONG Count
, Tag
= 0;
169 UNICODE_STRING GroupString
=
170 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet"
171 L
"\\Control\\ServiceGroupOrder");
174 Status
= IopOpenRegistryKeyEx(&KeyHandle
, NULL
, &GroupString
, KEY_READ
);
175 if (!NT_SUCCESS(Status
)) goto Quickie
;
178 Status
= IopGetRegistryValue(ServiceHandle
, L
"Group", &KeyValueInformation
);
179 if (!NT_SUCCESS(Status
)) goto Quickie
;
181 /* Make sure we have a group */
182 if ((KeyValueInformation
->Type
== REG_SZ
) &&
183 (KeyValueInformation
->DataLength
))
185 /* Convert to unicode string */
186 Buffer
= (PVOID
)((ULONG_PTR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
187 PnpRegSzToString(Buffer
, KeyValueInformation
->DataLength
, &Group
.Length
);
188 Group
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
189 Group
.Buffer
= Buffer
;
192 /* Now read the tag */
193 Status
= IopGetRegistryValue(ServiceHandle
, L
"Tag", &KeyValueInformationTag
);
194 if (!NT_SUCCESS(Status
)) goto Quickie
;
196 /* Make sure we have a tag */
197 if ((KeyValueInformationTag
->Type
== REG_DWORD
) &&
198 (KeyValueInformationTag
->DataLength
))
201 Tag
= *(PULONG
)((ULONG_PTR
)KeyValueInformationTag
+
202 KeyValueInformationTag
->DataOffset
);
205 /* We can get rid of this now */
206 ExFreePool(KeyValueInformationTag
);
208 /* Now let's read the group's tag order */
209 Status
= IopGetRegistryValue(KeyHandle
,
211 &KeyValueInformationGroupOrderList
);
213 /* We can get rid of this now */
215 if (KeyValueInformation
) ExFreePool(KeyValueInformation
);
216 if (KeyHandle
) NtClose(KeyHandle
);
217 if (!NT_SUCCESS(Status
)) return -1;
219 /* We're on the success path -- validate the tag order*/
220 if ((KeyValueInformationGroupOrderList
->Type
== REG_BINARY
) &&
221 (KeyValueInformationGroupOrderList
->DataLength
))
223 /* Get the order array */
224 GroupOrder
= (PULONG
)((ULONG_PTR
)KeyValueInformationGroupOrderList
+
225 KeyValueInformationGroupOrderList
->DataOffset
);
229 ASSERT(((Count
+ 1) * sizeof(ULONG
)) <=
230 KeyValueInformationGroupOrderList
->DataLength
);
232 /* Now loop each tag */
234 for (i
= 1; i
<= Count
; i
++)
236 /* If we found it, we're out */
237 if (Tag
== *GroupOrder
) break;
239 /* Try the next one */
244 /* Last buffer to free */
245 ExFreePool(KeyValueInformationGroupOrderList
);
251 PipCallDriverAddDevice(IN PDEVICE_NODE DeviceNode
,
252 IN BOOLEAN LoadDriver
,
253 IN PDRIVER_OBJECT DriverObject
)
256 HANDLE EnumRootKey
, SubKey
, ControlKey
, ClassKey
, PropertiesKey
;
257 UNICODE_STRING ClassGuid
, Properties
;
258 UNICODE_STRING EnumRoot
= RTL_CONSTANT_STRING(ENUM_ROOT
);
259 UNICODE_STRING ControlClass
=
260 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Class");
261 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
= NULL
;
264 /* Open enumeration root key */
265 Status
= IopOpenRegistryKeyEx(&EnumRootKey
,
269 if (!NT_SUCCESS(Status
))
271 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
276 /* Open instance subkey */
277 Status
= IopOpenRegistryKeyEx(&SubKey
,
279 &DeviceNode
->InstancePath
,
281 ZwClose(EnumRootKey
);
282 if (!NT_SUCCESS(Status
))
284 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
285 &DeviceNode
->InstancePath
, Status
);
290 Status
= IopGetRegistryValue(SubKey
,
291 REGSTR_VAL_CLASSGUID
,
292 &KeyValueInformation
);
294 if (NT_SUCCESS(Status
))
296 /* Convert to unicode string */
297 Buffer
= (PVOID
)((ULONG_PTR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
298 PnpRegSzToString(Buffer
, KeyValueInformation
->DataLength
, &ClassGuid
.Length
);
299 ClassGuid
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
300 ClassGuid
.Buffer
= Buffer
;
303 Status
= IopOpenRegistryKeyEx(&ControlKey
,
307 if (!NT_SUCCESS(Status
))
310 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
311 &ControlClass
, Status
);
316 /* Open the class key */
317 Status
= IopOpenRegistryKeyEx(&ClassKey
,
322 if (!NT_SUCCESS(Status
))
325 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
331 /* Check if we made it till here */
334 /* Get the device properties */
335 RtlInitUnicodeString(&Properties
, REGSTR_KEY_DEVICE_PROPERTIES
);
336 Status
= IopOpenRegistryKeyEx(&PropertiesKey
,
341 if (!NT_SUCCESS(Status
))
344 DPRINT("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
345 &Properties
, Status
);
346 PropertiesKey
= NULL
;
350 ZwClose(PropertiesKey
);
354 /* Free the registry data */
355 ExFreePool(KeyValueInformation
);
358 /* Do ReactOS-style setup */
359 Status
= IopAttachFilterDrivers(DeviceNode
, TRUE
);
360 if (!NT_SUCCESS(Status
))
362 IopRemoveDevice(DeviceNode
);
365 Status
= IopInitializeDevice(DeviceNode
, DriverObject
);
366 if (NT_SUCCESS(Status
))
368 Status
= IopAttachFilterDrivers(DeviceNode
, FALSE
);
369 if (!NT_SUCCESS(Status
))
371 IopRemoveDevice(DeviceNode
);
375 Status
= IopStartDevice(DeviceNode
);
385 IopInitializePlugPlayServices(VOID
)
389 HANDLE KeyHandle
, EnumHandle
, ParentHandle
, TreeHandle
, ControlHandle
;
390 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET");
391 UNICODE_STRING PnpManagerDriverName
= RTL_CONSTANT_STRING(DRIVER_ROOT_NAME L
"PnpManager");
394 /* Initialize locks and such */
395 KeInitializeSpinLock(&IopDeviceTreeLock
);
396 KeInitializeSpinLock(&IopDeviceRelationsSpinLock
);
397 InitializeListHead(&IopDeviceRelationsRequestList
);
399 /* Get the default interface */
400 PnpDefaultInterfaceType
= IopDetermineDefaultInterfaceType();
402 /* Initialize arbiters */
403 Status
= IopInitializeArbiters();
404 if (!NT_SUCCESS(Status
)) return Status
;
406 /* Setup the group cache */
407 Status
= PiInitCacheGroupInformation();
408 if (!NT_SUCCESS(Status
)) return Status
;
410 /* Open the current control set */
411 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
415 if (!NT_SUCCESS(Status
)) return Status
;
417 /* Create the control key */
418 RtlInitUnicodeString(&KeyName
, L
"Control");
419 Status
= IopCreateRegistryKeyEx(&ControlHandle
,
423 REG_OPTION_NON_VOLATILE
,
425 if (!NT_SUCCESS(Status
)) return Status
;
427 /* Check if it's a new key */
428 if (Disposition
== REG_CREATED_NEW_KEY
)
430 HANDLE DeviceClassesHandle
;
432 /* Create the device classes key */
433 RtlInitUnicodeString(&KeyName
, L
"DeviceClasses");
434 Status
= IopCreateRegistryKeyEx(&DeviceClassesHandle
,
438 REG_OPTION_NON_VOLATILE
,
440 if (!NT_SUCCESS(Status
)) return Status
;
442 ZwClose(DeviceClassesHandle
);
445 ZwClose(ControlHandle
);
447 /* Create the enum key */
448 RtlInitUnicodeString(&KeyName
, REGSTR_KEY_ENUM
);
449 Status
= IopCreateRegistryKeyEx(&EnumHandle
,
453 REG_OPTION_NON_VOLATILE
,
455 if (!NT_SUCCESS(Status
)) return Status
;
457 /* Check if it's a new key */
458 if (Disposition
== REG_CREATED_NEW_KEY
)
463 /* Create the root key */
464 ParentHandle
= EnumHandle
;
465 RtlInitUnicodeString(&KeyName
, REGSTR_KEY_ROOTENUM
);
466 Status
= IopCreateRegistryKeyEx(&EnumHandle
,
470 REG_OPTION_NON_VOLATILE
,
472 NtClose(ParentHandle
);
473 if (!NT_SUCCESS(Status
)) return Status
;
476 /* Open the root key now */
477 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\ENUM");
478 Status
= IopOpenRegistryKeyEx(&EnumHandle
,
482 if (NT_SUCCESS(Status
))
484 /* Create the root dev node */
485 RtlInitUnicodeString(&KeyName
, REGSTR_VAL_ROOT_DEVNODE
);
486 Status
= IopCreateRegistryKeyEx(&TreeHandle
,
490 REG_OPTION_NON_VOLATILE
,
493 if (NT_SUCCESS(Status
)) NtClose(TreeHandle
);
496 /* Create the root driver */
497 Status
= IoCreateDriver(&PnpManagerDriverName
, PnpRootDriverEntry
);
498 if (!NT_SUCCESS(Status
))
500 DPRINT1("IoCreateDriverObject() failed\n");
501 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
504 /* Create the root PDO */
505 Status
= IoCreateDevice(IopRootDriverObject
,
506 sizeof(IOPNP_DEVICE_EXTENSION
),
508 FILE_DEVICE_CONTROLLER
,
512 if (!NT_SUCCESS(Status
))
514 DPRINT1("IoCreateDevice() failed\n");
515 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
518 /* This is a bus enumerated device */
519 Pdo
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
521 /* Create the root device node */
522 IopRootDeviceNode
= PipAllocateDeviceNode(Pdo
);
525 IopRootDeviceNode
->Flags
|= DNF_STARTED
+ DNF_PROCESSED
+ DNF_ENUMERATED
+
526 DNF_MADEUP
+ DNF_NO_RESOURCE_REQUIRED
+
529 /* Create instance path */
530 RtlCreateUnicodeString(&IopRootDeviceNode
->InstancePath
,
531 REGSTR_VAL_ROOT_DEVNODE
);
533 /* Call the add device routine */
534 IopRootDriverObject
->DriverExtension
->AddDevice(IopRootDriverObject
,
535 IopRootDeviceNode
->PhysicalDeviceObject
);
537 /* Initialize PnP-Event notification support */
538 Status
= IopInitPlugPlayEvents();
539 if (!NT_SUCCESS(Status
)) return Status
;
541 /* Report the device to the user-mode pnp manager */
542 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
543 &IopRootDeviceNode
->InstancePath
);
545 /* Initialize the Bus Type GUID List */
546 PnpBusTypeGuidList
= ExAllocatePool(PagedPool
, sizeof(IO_BUS_TYPE_GUID_LIST
));
547 RtlZeroMemory(PnpBusTypeGuidList
, sizeof(IO_BUS_TYPE_GUID_LIST
));
548 ExInitializeFastMutex(&PnpBusTypeGuidList
->Lock
);
550 /* Launch the firmware mapper */
551 Status
= IopUpdateRootKey();
552 if (!NT_SUCCESS(Status
)) return Status
;
554 /* Close the handle to the control set */
558 return STATUS_SUCCESS
;