IopAssignDeviceResources():
[reactos.git] / reactos / ntoskrnl / io / pnpmgr.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/pnpmgr.c
6 * PURPOSE: Initializes the PnP manager
7 *
8 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <ddk/wdmguid.h>
15
16 #define NDEBUG
17 #include <internal/debug.h>
18
19 /* GLOBALS *******************************************************************/
20
21 PDEVICE_NODE IopRootDeviceNode;
22 KSPIN_LOCK IopDeviceTreeLock;
23
24 /* DATA **********************************************************************/
25
26 PDRIVER_OBJECT IopRootDriverObject;
27
28 /* FUNCTIONS *****************************************************************/
29
30 PDEVICE_NODE FASTCALL
31 IopGetDeviceNode(
32 PDEVICE_OBJECT DeviceObject)
33 {
34 return DeviceObject->DeviceObjectExtension->DeviceNode;
35 }
36
37 /*
38 * @implemented
39 */
40 VOID
41 STDCALL
42 IoInvalidateDeviceRelations(
43 IN PDEVICE_OBJECT DeviceObject,
44 IN DEVICE_RELATION_TYPE Type)
45 {
46 IopInvalidateDeviceRelations(IopGetDeviceNode(DeviceObject), Type);
47 }
48
49 /*
50 * @unimplemented
51 */
52 NTSTATUS
53 STDCALL
54 IoGetDeviceProperty(
55 IN PDEVICE_OBJECT DeviceObject,
56 IN DEVICE_REGISTRY_PROPERTY DeviceProperty,
57 IN ULONG BufferLength,
58 OUT PVOID PropertyBuffer,
59 OUT PULONG ResultLength)
60 {
61 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
62 ULONG Length;
63 PVOID Data = NULL;
64 PWSTR Ptr;
65
66 DPRINT("IoGetDeviceProperty(%x %d)\n", DeviceObject, DeviceProperty);
67
68 if (DeviceNode == NULL)
69 return STATUS_INVALID_DEVICE_REQUEST;
70
71 switch (DeviceProperty)
72 {
73 case DevicePropertyBusNumber:
74 Length = sizeof(ULONG);
75 Data = &DeviceNode->ChildBusNumber;
76 break;
77
78 /* Complete, untested */
79 case DevicePropertyBusTypeGuid:
80 *ResultLength = 39 * sizeof(WCHAR);
81 if (BufferLength < (39 * sizeof(WCHAR)))
82 return STATUS_BUFFER_TOO_SMALL;
83 swprintf((PWSTR)PropertyBuffer,
84 L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
85 DeviceNode->BusTypeGuid.Data1,
86 DeviceNode->BusTypeGuid.Data2,
87 DeviceNode->BusTypeGuid.Data3,
88 DeviceNode->BusTypeGuid.Data4[0],
89 DeviceNode->BusTypeGuid.Data4[1],
90 DeviceNode->BusTypeGuid.Data4[2],
91 DeviceNode->BusTypeGuid.Data4[3],
92 DeviceNode->BusTypeGuid.Data4[4],
93 DeviceNode->BusTypeGuid.Data4[5],
94 DeviceNode->BusTypeGuid.Data4[6],
95 DeviceNode->BusTypeGuid.Data4[7]);
96 return STATUS_SUCCESS;
97
98 case DevicePropertyLegacyBusType:
99 Length = sizeof(INTERFACE_TYPE);
100 Data = &DeviceNode->ChildInterfaceType;
101 break;
102
103 case DevicePropertyAddress:
104 Length = sizeof(ULONG);
105 Data = &DeviceNode->Address;
106 break;
107
108 // case DevicePropertyUINumber:
109 // if (DeviceNode->CapabilityFlags == NULL)
110 // return STATUS_INVALID_DEVICE_REQUEST;
111 // Length = sizeof(ULONG);
112 // Data = &DeviceNode->CapabilityFlags->UINumber;
113 // break;
114
115 case DevicePropertyClassName:
116 case DevicePropertyClassGuid:
117 case DevicePropertyDriverKeyName:
118 case DevicePropertyManufacturer:
119 case DevicePropertyFriendlyName:
120 case DevicePropertyHardwareID:
121 case DevicePropertyCompatibleIDs:
122 case DevicePropertyDeviceDescription:
123 case DevicePropertyLocationInformation:
124 case DevicePropertyUINumber:
125 {
126 LPWSTR RegistryPropertyName, KeyNameBuffer;
127 UNICODE_STRING KeyName, ValueName;
128 OBJECT_ATTRIBUTES ObjectAttributes;
129 KEY_VALUE_PARTIAL_INFORMATION *ValueInformation;
130 ULONG ValueInformationLength;
131 HANDLE KeyHandle;
132 NTSTATUS Status;
133
134 switch (DeviceProperty)
135 {
136 case DevicePropertyClassName:
137 RegistryPropertyName = L"Class"; break;
138 case DevicePropertyClassGuid:
139 RegistryPropertyName = L"ClassGuid"; break;
140 case DevicePropertyDriverKeyName:
141 RegistryPropertyName = L"Driver"; break;
142 case DevicePropertyManufacturer:
143 RegistryPropertyName = L"Mfg"; break;
144 case DevicePropertyFriendlyName:
145 RegistryPropertyName = L"FriendlyName"; break;
146 case DevicePropertyHardwareID:
147 RegistryPropertyName = L"HardwareID"; break;
148 case DevicePropertyCompatibleIDs:
149 RegistryPropertyName = L"CompatibleIDs"; break;
150 case DevicePropertyDeviceDescription:
151 RegistryPropertyName = L"DeviceDesc"; break;
152 case DevicePropertyLocationInformation:
153 RegistryPropertyName = L"LocationInformation"; break;
154 case DevicePropertyUINumber:
155 RegistryPropertyName = L"UINumber"; break;
156 default:
157 RegistryPropertyName = NULL; break;
158 }
159
160 KeyNameBuffer = ExAllocatePool(PagedPool,
161 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
162
163 DPRINT("KeyNameBuffer: %x, value %S\n",
164 KeyNameBuffer, RegistryPropertyName);
165
166 if (KeyNameBuffer == NULL)
167 return STATUS_INSUFFICIENT_RESOURCES;
168
169 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
170 wcscat(KeyNameBuffer, DeviceNode->InstancePath.Buffer);
171 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
172 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
173 OBJ_CASE_INSENSITIVE, NULL, NULL);
174
175 Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
176 ExFreePool(KeyNameBuffer);
177 if (!NT_SUCCESS(Status))
178 return Status;
179
180 RtlInitUnicodeString(&ValueName, RegistryPropertyName);
181 ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
182 Data[0]) + BufferLength;
183 ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
184 if (ValueInformation == NULL)
185 {
186 ZwClose(KeyHandle);
187 return STATUS_INSUFFICIENT_RESOURCES;
188 }
189
190 Status = ZwQueryValueKey(KeyHandle, &ValueName,
191 KeyValuePartialInformation, ValueInformation,
192 ValueInformationLength,
193 &ValueInformationLength);
194 *ResultLength = ValueInformation->DataLength;
195 ZwClose(KeyHandle);
196
197 if (ValueInformation->DataLength > BufferLength)
198 Status = STATUS_BUFFER_TOO_SMALL;
199
200 if (!NT_SUCCESS(Status))
201 {
202 ExFreePool(ValueInformation);
203 return Status;
204 }
205
206 /* FIXME: Verify the value (NULL-terminated, correct format). */
207
208 RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
209 ValueInformation->DataLength);
210 ExFreePool(ValueInformation);
211
212 return STATUS_SUCCESS;
213 }
214
215 case DevicePropertyBootConfiguration:
216 Length = 0;
217 if (DeviceNode->BootResources->Count != 0)
218 {
219 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
220 }
221 Data = &DeviceNode->BootResources;
222 break;
223
224 /* FIXME: use a translated boot configuration instead */
225 case DevicePropertyBootConfigurationTranslated:
226 Length = 0;
227 if (DeviceNode->BootResources->Count != 0)
228 {
229 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
230 }
231 Data = &DeviceNode->BootResources;
232 break;
233
234 case DevicePropertyEnumeratorName:
235 Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
236 if (Ptr != NULL)
237 {
238 Length = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)DeviceNode->InstancePath.Buffer) + sizeof(WCHAR);
239 Data = DeviceNode->InstancePath.Buffer;
240 }
241 else
242 {
243 Length = 0;
244 Data = NULL;
245 }
246 break;
247
248 case DevicePropertyPhysicalDeviceObjectName:
249 Length = DeviceNode->InstancePath.Length + sizeof(WCHAR);
250 Data = DeviceNode->InstancePath.Buffer;
251 break;
252
253 default:
254 return STATUS_INVALID_PARAMETER_2;
255 }
256
257 *ResultLength = Length;
258 if (BufferLength < Length)
259 return STATUS_BUFFER_TOO_SMALL;
260 RtlCopyMemory(PropertyBuffer, Data, Length);
261
262 /* Terminate the string */
263 if (DeviceProperty == DevicePropertyEnumeratorName
264 || DeviceProperty == DevicePropertyPhysicalDeviceObjectName)
265 {
266 Ptr = (PWSTR)PropertyBuffer;
267 Ptr[(Length / sizeof(WCHAR)) - 1] = 0;
268 }
269
270 return STATUS_SUCCESS;
271 }
272
273 /*
274 * @unimplemented
275 */
276 VOID
277 STDCALL
278 IoInvalidateDeviceState(
279 IN PDEVICE_OBJECT PhysicalDeviceObject)
280 {
281 }
282
283 /**
284 * @name IoOpenDeviceRegistryKey
285 *
286 * Open a registry key unique for a specified driver or device instance.
287 *
288 * @param DeviceObject Device to get the registry key for.
289 * @param DevInstKeyType Type of the key to return.
290 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
291 * @param DevInstRegKey Handle to the opened registry key on
292 * successful return.
293 *
294 * @return Status.
295 *
296 * @implemented
297 */
298 NTSTATUS
299 STDCALL
300 IoOpenDeviceRegistryKey(
301 IN PDEVICE_OBJECT DeviceObject,
302 IN ULONG DevInstKeyType,
303 IN ACCESS_MASK DesiredAccess,
304 OUT PHANDLE DevInstRegKey)
305 {
306 static WCHAR RootKeyName[] =
307 L"\\Registry\\Machine\\System\\CurrentControlSet\\";
308 static WCHAR ProfileKeyName[] =
309 L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
310 static WCHAR ClassKeyName[] = L"Control\\Class\\";
311 static WCHAR EnumKeyName[] = L"Enum\\";
312 static WCHAR DeviceParametersKeyName[] = L"Device Parameters\\";
313 ULONG KeyNameLength;
314 LPWSTR KeyNameBuffer;
315 UNICODE_STRING KeyName;
316 ULONG DriverKeyLength;
317 OBJECT_ATTRIBUTES ObjectAttributes;
318 PDEVICE_NODE DeviceNode = NULL;
319 NTSTATUS Status;
320
321 if ((DevInstKeyType & (PLUGPLAY_REGKEY_DEVICE | PLUGPLAY_REGKEY_DRIVER)) == 0)
322 return STATUS_INVALID_PARAMETER;
323
324 /*
325 * Calculate the length of the base key name. This is the full
326 * name for driver key or the name excluding "Device Parameters"
327 * subkey for device key.
328 */
329
330 KeyNameLength = sizeof(RootKeyName);
331 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
332 KeyNameLength += sizeof(ProfileKeyName) - sizeof(UNICODE_NULL);
333 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
334 {
335 KeyNameLength += sizeof(ClassKeyName) - sizeof(UNICODE_NULL);
336 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
337 0, NULL, &DriverKeyLength);
338 if (Status != STATUS_BUFFER_TOO_SMALL)
339 return Status;
340 KeyNameLength += DriverKeyLength;
341 }
342 else
343 {
344 DeviceNode = IopGetDeviceNode(DeviceObject);
345 KeyNameLength += sizeof(EnumKeyName) - sizeof(UNICODE_NULL) +
346 DeviceNode->InstancePath.Length;
347 }
348
349 /*
350 * Now allocate the buffer for the key name...
351 */
352
353 KeyNameBuffer = ExAllocatePool(PagedPool, KeyNameLength);
354 if (KeyNameBuffer == NULL)
355 return STATUS_INSUFFICIENT_RESOURCES;
356
357 KeyName.Length = 0;
358 KeyName.MaximumLength = KeyNameLength;
359 KeyName.Buffer = KeyNameBuffer;
360
361 /*
362 * ...and build the key name.
363 */
364
365 KeyName.Length += sizeof(RootKeyName) - sizeof(UNICODE_NULL);
366 RtlCopyMemory(KeyNameBuffer, RootKeyName, KeyName.Length);
367
368 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
369 RtlAppendUnicodeToString(&KeyName, ProfileKeyName);
370
371 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
372 {
373 RtlAppendUnicodeToString(&KeyName, ClassKeyName);
374 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
375 DriverKeyLength, KeyNameBuffer +
376 (KeyName.Length / sizeof(WCHAR)),
377 &DriverKeyLength);
378 if (!NT_SUCCESS(Status))
379 {
380 ExFreePool(KeyNameBuffer);
381 return Status;
382 }
383 KeyName.Length += DriverKeyLength - sizeof(UNICODE_NULL);
384 }
385 else
386 {
387 RtlAppendUnicodeToString(&KeyName, EnumKeyName);
388 Status = RtlAppendUnicodeStringToString(&KeyName, &DeviceNode->InstancePath);
389 if (DeviceNode->InstancePath.Length == 0)
390 {
391 ExFreePool(KeyNameBuffer);
392 return Status;
393 }
394 }
395
396 /*
397 * Open the base key.
398 */
399
400 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
401 OBJ_CASE_INSENSITIVE, NULL, NULL);
402 Status = ZwOpenKey(DevInstRegKey, DesiredAccess, &ObjectAttributes);
403 ExFreePool(KeyNameBuffer);
404
405 /*
406 * For driver key we're done now. Also if the base key doesn't
407 * exist we can bail out with error...
408 */
409
410 if ((DevInstKeyType & PLUGPLAY_REGKEY_DRIVER) || !NT_SUCCESS(Status))
411 return Status;
412
413 /*
414 * Let's go further. For device key we must open "Device Parameters"
415 * subkey and create it if it doesn't exist yet.
416 */
417
418 RtlInitUnicodeString(&KeyName, DeviceParametersKeyName);
419 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
420 OBJ_CASE_INSENSITIVE, *DevInstRegKey, NULL);
421 Status = ZwCreateKey(DevInstRegKey, DesiredAccess, &ObjectAttributes,
422 0, NULL, REG_OPTION_NON_VOLATILE, NULL);
423 ZwClose(ObjectAttributes.RootDirectory);
424
425 return Status;
426 }
427
428 /*
429 * @unimplemented
430 */
431 VOID
432 STDCALL
433 IoRequestDeviceEject(
434 IN PDEVICE_OBJECT PhysicalDeviceObject
435 )
436 {
437 UNIMPLEMENTED;
438 }
439
440
441 BOOLEAN
442 IopCreateUnicodeString(
443 PUNICODE_STRING Destination,
444 PWSTR Source,
445 POOL_TYPE PoolType)
446 {
447 ULONG Length;
448
449 if (!Source)
450 {
451 RtlInitUnicodeString(Destination, NULL);
452 return TRUE;
453 }
454
455 Length = (wcslen(Source) + 1) * sizeof(WCHAR);
456
457 Destination->Buffer = ExAllocatePool(PoolType, Length);
458
459 if (Destination->Buffer == NULL)
460 {
461 return FALSE;
462 }
463
464 RtlCopyMemory(Destination->Buffer, Source, Length);
465
466 Destination->MaximumLength = Length;
467
468 Destination->Length = Length - sizeof(WCHAR);
469
470 return TRUE;
471 }
472
473 NTSTATUS
474 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT *DeviceObject)
475 {
476 KIRQL OldIrql;
477
478 if (PopSystemPowerDeviceNode)
479 {
480 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
481 *DeviceObject = PopSystemPowerDeviceNode->PhysicalDeviceObject;
482 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
483
484 return STATUS_SUCCESS;
485 }
486
487 return STATUS_UNSUCCESSFUL;
488 }
489
490 /*
491 * DESCRIPTION
492 * Creates a device node
493 *
494 * ARGUMENTS
495 * ParentNode = Pointer to parent device node
496 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
497 * to have the root device node create one
498 * (eg. for legacy drivers)
499 * DeviceNode = Pointer to storage for created device node
500 *
501 * RETURN VALUE
502 * Status
503 */
504 NTSTATUS
505 IopCreateDeviceNode(PDEVICE_NODE ParentNode,
506 PDEVICE_OBJECT PhysicalDeviceObject,
507 PDEVICE_NODE *DeviceNode)
508 {
509 PDEVICE_NODE Node;
510 NTSTATUS Status;
511 KIRQL OldIrql;
512
513 DPRINT("ParentNode %x PhysicalDeviceObject %x\n",
514 ParentNode, PhysicalDeviceObject);
515
516 Node = (PDEVICE_NODE)ExAllocatePool(NonPagedPool, sizeof(DEVICE_NODE));
517 if (!Node)
518 {
519 return STATUS_INSUFFICIENT_RESOURCES;
520 }
521
522 RtlZeroMemory(Node, sizeof(DEVICE_NODE));
523
524 if (!PhysicalDeviceObject)
525 {
526 Status = PnpRootCreateDevice(&PhysicalDeviceObject);
527 if (!NT_SUCCESS(Status))
528 {
529 ExFreePool(Node);
530 return Status;
531 }
532
533 /* This is for drivers passed on the command line to ntoskrnl.exe */
534 IopDeviceNodeSetFlag(Node, DNF_STARTED);
535 IopDeviceNodeSetFlag(Node, DNF_LEGACY_DRIVER);
536 }
537
538 Node->PhysicalDeviceObject = PhysicalDeviceObject;
539
540 PhysicalDeviceObject->DeviceObjectExtension->DeviceNode = Node;
541
542 if (ParentNode)
543 {
544 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
545 Node->Parent = ParentNode;
546 Node->NextSibling = ParentNode->Child;
547 if (ParentNode->Child != NULL)
548 {
549 ParentNode->Child->PrevSibling = Node;
550 }
551 ParentNode->Child = Node;
552 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
553 }
554
555 *DeviceNode = Node;
556
557 return STATUS_SUCCESS;
558 }
559
560 NTSTATUS
561 IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
562 {
563 KIRQL OldIrql;
564
565 /* All children must be deleted before a parent is deleted */
566 ASSERT(!DeviceNode->Child);
567
568 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
569
570 ASSERT(DeviceNode->PhysicalDeviceObject);
571
572 ObDereferenceObject(DeviceNode->PhysicalDeviceObject);
573
574 /* Unlink from parent if it exists */
575
576 if ((DeviceNode->Parent) && (DeviceNode->Parent->Child == DeviceNode))
577 {
578 DeviceNode->Parent->Child = DeviceNode->NextSibling;
579 }
580
581 /* Unlink from sibling list */
582
583 if (DeviceNode->PrevSibling)
584 {
585 DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
586 }
587
588 if (DeviceNode->NextSibling)
589 {
590 DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
591 }
592
593 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
594
595 RtlFreeUnicodeString(&DeviceNode->InstancePath);
596
597 RtlFreeUnicodeString(&DeviceNode->ServiceName);
598
599 if (DeviceNode->ResourceList)
600 {
601 ExFreePool(DeviceNode->ResourceList);
602 }
603
604 if (DeviceNode->ResourceListTranslated)
605 {
606 ExFreePool(DeviceNode->ResourceListTranslated);
607 }
608
609 if (DeviceNode->ResourceRequirements)
610 {
611 ExFreePool(DeviceNode->ResourceRequirements);
612 }
613
614 if (DeviceNode->BootResources)
615 {
616 ExFreePool(DeviceNode->BootResources);
617 }
618
619 ExFreePool(DeviceNode);
620
621 return STATUS_SUCCESS;
622 }
623
624 NTSTATUS
625 IopInitiatePnpIrp(
626 PDEVICE_OBJECT DeviceObject,
627 PIO_STATUS_BLOCK IoStatusBlock,
628 ULONG MinorFunction,
629 PIO_STACK_LOCATION Stack OPTIONAL)
630 {
631 PDEVICE_OBJECT TopDeviceObject;
632 PIO_STACK_LOCATION IrpSp;
633 NTSTATUS Status;
634 KEVENT Event;
635 PIRP Irp;
636
637 /* Always call the top of the device stack */
638 TopDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
639
640 KeInitializeEvent(
641 &Event,
642 NotificationEvent,
643 FALSE);
644
645 Irp = IoBuildSynchronousFsdRequest(
646 IRP_MJ_PNP,
647 TopDeviceObject,
648 NULL,
649 0,
650 NULL,
651 &Event,
652 IoStatusBlock);
653
654 /* PNP IRPs are always initialized with a status code of
655 STATUS_NOT_IMPLEMENTED */
656 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
657 Irp->IoStatus.Information = 0;
658
659 IrpSp = IoGetNextIrpStackLocation(Irp);
660 IrpSp->MinorFunction = MinorFunction;
661
662 if (Stack)
663 {
664 RtlMoveMemory(
665 &IrpSp->Parameters,
666 &Stack->Parameters,
667 sizeof(Stack->Parameters));
668 }
669
670 Status = IoCallDriver(TopDeviceObject, Irp);
671 if (Status == STATUS_PENDING)
672 {
673 KeWaitForSingleObject(
674 &Event,
675 Executive,
676 KernelMode,
677 FALSE,
678 NULL);
679 Status = IoStatusBlock->Status;
680 }
681
682 ObDereferenceObject(TopDeviceObject);
683
684 return Status;
685 }
686
687
688 NTSTATUS
689 IopTraverseDeviceTreeNode(
690 PDEVICETREE_TRAVERSE_CONTEXT Context)
691 {
692 PDEVICE_NODE ParentDeviceNode;
693 PDEVICE_NODE ChildDeviceNode;
694 NTSTATUS Status;
695
696 /* Copy context data so we don't overwrite it in subsequent calls to this function */
697 ParentDeviceNode = Context->DeviceNode;
698
699 /* Call the action routine */
700 Status = (Context->Action)(ParentDeviceNode, Context->Context);
701 if (!NT_SUCCESS(Status))
702 {
703 return Status;
704 }
705
706 /* Traversal of all children nodes */
707 for (ChildDeviceNode = ParentDeviceNode->Child;
708 ChildDeviceNode != NULL;
709 ChildDeviceNode = ChildDeviceNode->NextSibling)
710 {
711 /* Pass the current device node to the action routine */
712 Context->DeviceNode = ChildDeviceNode;
713
714 Status = IopTraverseDeviceTreeNode(Context);
715 if (!NT_SUCCESS(Status))
716 {
717 return Status;
718 }
719 }
720
721 return Status;
722 }
723
724
725 NTSTATUS
726 IopTraverseDeviceTree(
727 PDEVICETREE_TRAVERSE_CONTEXT Context)
728 {
729 NTSTATUS Status;
730
731 DPRINT("Context %x\n", Context);
732
733 DPRINT("IopTraverseDeviceTree(DeviceNode %x FirstDeviceNode %x Action %x Context %x)\n",
734 Context->DeviceNode, Context->FirstDeviceNode, Context->Action, Context->Context);
735
736 /* Start from the specified device node */
737 Context->DeviceNode = Context->FirstDeviceNode;
738
739 /* Recursively traverse the device tree */
740 Status = IopTraverseDeviceTreeNode(Context);
741 if (Status == STATUS_UNSUCCESSFUL)
742 {
743 /* The action routine just wanted to terminate the traversal with status
744 code STATUS_SUCCESS */
745 Status = STATUS_SUCCESS;
746 }
747
748 return Status;
749 }
750
751
752 static NTSTATUS
753 IopCreateDeviceKeyPath(PWSTR Path,
754 PHANDLE Handle)
755 {
756 OBJECT_ATTRIBUTES ObjectAttributes;
757 WCHAR KeyBuffer[MAX_PATH];
758 UNICODE_STRING KeyName;
759 HANDLE KeyHandle;
760 NTSTATUS Status;
761 PWCHAR Current;
762 PWCHAR Next;
763
764 *Handle = NULL;
765
766 if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
767 {
768 return STATUS_INVALID_PARAMETER;
769 }
770
771 wcsncpy (KeyBuffer, Path, MAX_PATH-1);
772
773 /* Skip \\Registry\\ */
774 Current = KeyBuffer;
775 Current = wcschr (Current, L'\\') + 1;
776 Current = wcschr (Current, L'\\') + 1;
777
778 while (TRUE)
779 {
780 Next = wcschr (Current, L'\\');
781 if (Next == NULL)
782 {
783 /* The end */
784 }
785 else
786 {
787 *Next = 0;
788 }
789
790 RtlInitUnicodeString (&KeyName, KeyBuffer);
791 InitializeObjectAttributes (&ObjectAttributes,
792 &KeyName,
793 OBJ_CASE_INSENSITIVE,
794 NULL,
795 NULL);
796
797 DPRINT("Create '%S'\n", KeyName.Buffer);
798
799 Status = ZwCreateKey (&KeyHandle,
800 KEY_ALL_ACCESS,
801 &ObjectAttributes,
802 0,
803 NULL,
804 0,
805 NULL);
806 if (!NT_SUCCESS (Status))
807 {
808 DPRINT ("ZwCreateKey() failed with status %x\n", Status);
809 return Status;
810 }
811
812 if (Next == NULL)
813 {
814 *Handle = KeyHandle;
815 return STATUS_SUCCESS;
816 }
817 else
818 {
819 ZwClose (KeyHandle);
820 *Next = L'\\';
821 }
822
823 Current = Next + 1;
824 }
825
826 return STATUS_UNSUCCESSFUL;
827 }
828
829
830 static NTSTATUS
831 IopSetDeviceInstanceData(HANDLE InstanceKey,
832 PDEVICE_NODE DeviceNode)
833 {
834 OBJECT_ATTRIBUTES ObjectAttributes;
835 UNICODE_STRING KeyName;
836 HANDLE LogConfKey;
837 ULONG ResCount;
838 ULONG ListSize;
839 NTSTATUS Status;
840
841 DPRINT("IopSetDeviceInstanceData() called\n");
842
843 /* Create the 'LogConf' key */
844 RtlInitUnicodeString(&KeyName,
845 L"LogConf");
846 InitializeObjectAttributes(&ObjectAttributes,
847 &KeyName,
848 OBJ_CASE_INSENSITIVE,
849 InstanceKey,
850 NULL);
851 Status = ZwCreateKey(&LogConfKey,
852 KEY_ALL_ACCESS,
853 &ObjectAttributes,
854 0,
855 NULL,
856 0,
857 NULL);
858 if (NT_SUCCESS(Status))
859 {
860 /* Set 'BootConfig' value */
861 if (DeviceNode->BootResources != NULL)
862 {
863 ResCount = DeviceNode->BootResources->Count;
864 if (ResCount != 0)
865 {
866 ListSize = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
867
868 RtlInitUnicodeString(&KeyName,
869 L"BootConfig");
870 Status = ZwSetValueKey(LogConfKey,
871 &KeyName,
872 0,
873 REG_RESOURCE_LIST,
874 &DeviceNode->BootResources,
875 ListSize);
876 }
877 }
878
879 /* Set 'BasicConfigVector' value */
880 if (DeviceNode->ResourceRequirements != NULL &&
881 DeviceNode->ResourceRequirements->ListSize != 0)
882 {
883 RtlInitUnicodeString(&KeyName,
884 L"BasicConfigVector");
885 Status = ZwSetValueKey(LogConfKey,
886 &KeyName,
887 0,
888 REG_RESOURCE_REQUIREMENTS_LIST,
889 &DeviceNode->ResourceRequirements,
890 DeviceNode->ResourceRequirements->ListSize);
891 }
892
893 ZwClose(LogConfKey);
894 }
895
896 DPRINT("IopSetDeviceInstanceData() done\n");
897
898 return STATUS_SUCCESS;
899 }
900
901
902 NTSTATUS
903 IopAssignDeviceResources(
904 PDEVICE_NODE DeviceNode)
905 {
906 PIO_RESOURCE_LIST ResourceList;
907 PIO_RESOURCE_DESCRIPTOR ResourceDescriptor;
908 PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorRaw, DescriptorTranslated;
909 ULONG NumberOfResources = 0;
910 ULONG i;
911 NTSTATUS Status;
912
913 /* Fill DeviceNode->ResourceList and DeviceNode->ResourceListTranslated;
914 * by using DeviceNode->ResourceRequirements */
915
916 if (!DeviceNode->ResourceRequirements
917 || DeviceNode->ResourceRequirements->AlternativeLists == 0)
918 {
919 DeviceNode->ResourceList = DeviceNode->ResourceListTranslated = NULL;
920 return STATUS_SUCCESS;
921 }
922
923 /* FIXME: that's here that PnP arbiter should go */
924 /* Actually, simply use resource list #0 as assigned resource list */
925 ResourceList = &DeviceNode->ResourceRequirements->List[0];
926 if (ResourceList->Version != 1 || ResourceList->Revision != 1)
927 {
928 Status = STATUS_REVISION_MISMATCH;
929 goto ByeBye;
930 }
931
932 DeviceNode->ResourceList = ExAllocatePool(PagedPool,
933 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
934 if (!DeviceNode->ResourceList)
935 {
936 Status = STATUS_INSUFFICIENT_RESOURCES;
937 goto ByeBye;
938 }
939
940 DeviceNode->ResourceListTranslated = ExAllocatePool(PagedPool,
941 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
942 if (!DeviceNode->ResourceListTranslated)
943 {
944 Status = STATUS_INSUFFICIENT_RESOURCES;
945 goto ByeBye;
946 }
947
948 DeviceNode->ResourceList->Count = 1;
949 DeviceNode->ResourceList->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
950 DeviceNode->ResourceList->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
951 DeviceNode->ResourceList->List[0].PartialResourceList.Version = 1;
952 DeviceNode->ResourceList->List[0].PartialResourceList.Revision = 1;
953
954 DeviceNode->ResourceListTranslated->Count = 1;
955 DeviceNode->ResourceListTranslated->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
956 DeviceNode->ResourceListTranslated->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
957 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Version = 1;
958 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Revision = 1;
959
960 for (i = 0; i < ResourceList->Count; i++)
961 {
962 ResourceDescriptor = &ResourceList->Descriptors[i];
963
964 if (ResourceDescriptor->Option == 0 || ResourceDescriptor->Option == IO_RESOURCE_PREFERRED)
965 {
966 DescriptorRaw = &DeviceNode->ResourceList->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
967 DescriptorTranslated = &DeviceNode->ResourceListTranslated->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
968 NumberOfResources++;
969
970 /* Copy ResourceDescriptor to DescriptorRaw and DescriptorTranslated */
971 DescriptorRaw->Type = DescriptorTranslated->Type = ResourceDescriptor->Type;
972 DescriptorRaw->ShareDisposition = DescriptorTranslated->ShareDisposition = ResourceDescriptor->ShareDisposition;
973 DescriptorRaw->Flags = DescriptorTranslated->Flags = ResourceDescriptor->Flags;
974 switch (ResourceDescriptor->Type)
975 {
976 case CmResourceTypePort:
977 {
978 ULONG AddressSpace = 0; /* IO space */
979 DescriptorRaw->u.Port.Start = ResourceDescriptor->u.Port.MinimumAddress;
980 DescriptorRaw->u.Port.Length = DescriptorTranslated->u.Port.Length
981 = ResourceDescriptor->u.Port.Length;
982 if (!HalTranslateBusAddress(
983 DeviceNode->ResourceRequirements->InterfaceType,
984 DeviceNode->ResourceRequirements->BusNumber,
985 DescriptorRaw->u.Port.Start,
986 &AddressSpace,
987 &DescriptorTranslated->u.Port.Start))
988 {
989 Status = STATUS_UNSUCCESSFUL;
990 goto ByeBye;
991 }
992 break;
993 }
994 case CmResourceTypeInterrupt:
995 {
996 DescriptorRaw->u.Interrupt.Level = 0;
997 /* FIXME: if IRQ 9 is in the possible range, use it.
998 * This should be a PCI device */
999 if (ResourceDescriptor->u.Interrupt.MinimumVector <= 9
1000 && ResourceDescriptor->u.Interrupt.MaximumVector >= 9)
1001 DescriptorRaw->u.Interrupt.Vector = 9;
1002 else
1003 DescriptorRaw->u.Interrupt.Vector = ResourceDescriptor->u.Interrupt.MinimumVector;
1004
1005 DescriptorTranslated->u.Interrupt.Vector = HalGetInterruptVector(
1006 DeviceNode->ResourceRequirements->InterfaceType,
1007 DeviceNode->ResourceRequirements->BusNumber,
1008 DescriptorRaw->u.Interrupt.Level,
1009 DescriptorRaw->u.Interrupt.Vector,
1010 (PKIRQL)&DescriptorTranslated->u.Interrupt.Level,
1011 &DescriptorRaw->u.Interrupt.Affinity);
1012 DescriptorTranslated->u.Interrupt.Affinity = DescriptorRaw->u.Interrupt.Affinity;
1013 break;
1014 }
1015 case CmResourceTypeMemory:
1016 {
1017 ULONG AddressSpace = 1; /* Memory space */
1018 DescriptorRaw->u.Memory.Start = ResourceDescriptor->u.Memory.MinimumAddress;
1019 DescriptorRaw->u.Memory.Length = DescriptorTranslated->u.Memory.Length
1020 = ResourceDescriptor->u.Memory.Length;
1021 if (!HalTranslateBusAddress(
1022 DeviceNode->ResourceRequirements->InterfaceType,
1023 DeviceNode->ResourceRequirements->BusNumber,
1024 DescriptorRaw->u.Memory.Start,
1025 &AddressSpace,
1026 &DescriptorTranslated->u.Memory.Start))
1027 {
1028 Status = STATUS_UNSUCCESSFUL;
1029 goto ByeBye;
1030 }
1031 break;
1032 }
1033 case CmResourceTypeDma:
1034 {
1035 DescriptorRaw->u.Dma.Channel = DescriptorTranslated->u.Dma.Channel
1036 = ResourceDescriptor->u.Dma.MinimumChannel;
1037 DescriptorRaw->u.Dma.Port = DescriptorTranslated->u.Dma.Port
1038 = 0; /* FIXME */
1039 DescriptorRaw->u.Dma.Reserved1 = DescriptorTranslated->u.Dma.Reserved1
1040 = 0;
1041 break;
1042 }
1043 /*case CmResourceTypeBusNumber:
1044 {
1045 DescriptorRaw->u.BusNumber.Start = DescriptorTranslated->u.BusNumber.Start
1046 = ResourceDescriptor->u.BusNumber.MinBusNumber;
1047 DescriptorRaw->u.BusNumber.Length = DescriptorTranslated->u.BusNumber.Length
1048 = ResourceDescriptor->u.BusNumber.Length;
1049 DescriptorRaw->u.BusNumber.Reserved = DescriptorTranslated->u.BusNumber.Reserved
1050 = ResourceDescriptor->u.BusNumber.Reserved;
1051 break;
1052 }*/
1053 /*CmResourceTypeDevicePrivate:
1054 case CmResourceTypePcCardConfig:
1055 case CmResourceTypeMfCardConfig:
1056 {
1057 RtlCopyMemory(
1058 &DescriptorRaw->u.DevicePrivate,
1059 &ResourceDescriptor->u.DevicePrivate,
1060 sizeof(ResourceDescriptor->u.DevicePrivate));
1061 RtlCopyMemory(
1062 &DescriptorTranslated->u.DevicePrivate,
1063 &ResourceDescriptor->u.DevicePrivate,
1064 sizeof(ResourceDescriptor->u.DevicePrivate));
1065 break;
1066 }*/
1067 default:
1068 DPRINT1("IopAssignDeviceResources(): unknown resource descriptor type 0x%x\n", ResourceDescriptor->Type);
1069 NumberOfResources--;
1070 }
1071 }
1072
1073 }
1074
1075 DeviceNode->ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
1076 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Count = NumberOfResources;
1077
1078 return STATUS_SUCCESS;
1079
1080 ByeBye:
1081 if (DeviceNode->ResourceList)
1082 {
1083 ExFreePool(DeviceNode->ResourceList);
1084 DeviceNode->ResourceList = NULL;
1085 }
1086 if (DeviceNode->ResourceListTranslated)
1087 {
1088 ExFreePool(DeviceNode->ResourceListTranslated);
1089 DeviceNode->ResourceListTranslated = NULL;
1090 }
1091
1092 return Status;
1093 }
1094
1095
1096 /*
1097 * IopActionInterrogateDeviceStack
1098 *
1099 * Retrieve information for all (direct) child nodes of a parent node.
1100 *
1101 * Parameters
1102 * DeviceNode
1103 * Pointer to device node.
1104 * Context
1105 * Pointer to parent node to retrieve child node information for.
1106 *
1107 * Remarks
1108 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1109 * when we reach a device node which is not a direct child of the device
1110 * node for which we retrieve information of child nodes for. Any errors
1111 * that occur is logged instead so that all child services have a chance
1112 * of being interrogated.
1113 */
1114
1115 NTSTATUS
1116 IopActionInterrogateDeviceStack(
1117 PDEVICE_NODE DeviceNode,
1118 PVOID Context)
1119 {
1120 IO_STATUS_BLOCK IoStatusBlock;
1121 PDEVICE_NODE ParentDeviceNode;
1122 WCHAR InstancePath[MAX_PATH];
1123 IO_STACK_LOCATION Stack;
1124 NTSTATUS Status;
1125 PWSTR KeyBuffer;
1126 PWSTR Ptr;
1127 USHORT Length;
1128 USHORT TotalLength;
1129 HANDLE InstanceKey = NULL;
1130 UNICODE_STRING ValueName;
1131 DEVICE_CAPABILITIES DeviceCapabilities;
1132
1133 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
1134 DPRINT("PDO %x\n", DeviceNode->PhysicalDeviceObject);
1135
1136 ParentDeviceNode = (PDEVICE_NODE)Context;
1137
1138 /*
1139 * We are called for the parent too, but we don't need to do special
1140 * handling for this node
1141 */
1142
1143 if (DeviceNode == ParentDeviceNode)
1144 {
1145 DPRINT("Success\n");
1146 return STATUS_SUCCESS;
1147 }
1148
1149 /*
1150 * Make sure this device node is a direct child of the parent device node
1151 * that is given as an argument
1152 */
1153
1154 if (DeviceNode->Parent != ParentDeviceNode)
1155 {
1156 /* Stop the traversal immediately and indicate successful operation */
1157 DPRINT("Stop\n");
1158 return STATUS_UNSUCCESSFUL;
1159 }
1160
1161 /*
1162 * FIXME: For critical errors, cleanup and disable device, but always
1163 * return STATUS_SUCCESS.
1164 */
1165
1166 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1167
1168 Stack.Parameters.QueryId.IdType = BusQueryDeviceID;
1169 Status = IopInitiatePnpIrp(
1170 DeviceNode->PhysicalDeviceObject,
1171 &IoStatusBlock,
1172 IRP_MN_QUERY_ID,
1173 &Stack);
1174 if (NT_SUCCESS(Status))
1175 {
1176 /* Copy the device id string */
1177 wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
1178
1179 /*
1180 * FIXME: Check for valid characters, if there is invalid characters
1181 * then bugcheck.
1182 */
1183 }
1184 else
1185 {
1186 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1187 }
1188
1189 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1190
1191 Stack.Parameters.QueryId.IdType = BusQueryInstanceID;
1192 Status = IopInitiatePnpIrp(
1193 DeviceNode->PhysicalDeviceObject,
1194 &IoStatusBlock,
1195 IRP_MN_QUERY_ID,
1196 &Stack);
1197 if (NT_SUCCESS(Status))
1198 {
1199 /* Append the instance id string */
1200 wcscat(InstancePath, L"\\");
1201 wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
1202
1203 /*
1204 * FIXME: Check for valid characters, if there is invalid characters
1205 * then bugcheck
1206 */
1207 }
1208 else
1209 {
1210 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1211 }
1212
1213 RtlZeroMemory(&DeviceCapabilities, sizeof(DEVICE_CAPABILITIES));
1214 DeviceCapabilities.Size = sizeof(DEVICE_CAPABILITIES);
1215 DeviceCapabilities.Version = 1;
1216 DeviceCapabilities.Address = -1;
1217 DeviceCapabilities.UINumber = -1;
1218
1219 Stack.Parameters.DeviceCapabilities.Capabilities = &DeviceCapabilities;
1220 Status = IopInitiatePnpIrp(
1221 DeviceNode->PhysicalDeviceObject,
1222 &IoStatusBlock,
1223 IRP_MN_QUERY_CAPABILITIES,
1224 &Stack);
1225 if (NT_SUCCESS(Status))
1226 {
1227 }
1228 else
1229 {
1230 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1231 }
1232
1233 DeviceNode->CapabilityFlags = *(PULONG)((ULONG_PTR)&DeviceCapabilities + 4);
1234 DeviceNode->Address = DeviceCapabilities.Address;
1235
1236 if (!DeviceCapabilities.UniqueID)
1237 {
1238 DPRINT("Instance ID is not unique\n");
1239 /* FIXME: Add information from parent bus driver to InstancePath */
1240 }
1241
1242 if (!IopCreateUnicodeString(&DeviceNode->InstancePath, InstancePath, PagedPool))
1243 {
1244 DPRINT("No resources\n");
1245 /* FIXME: Cleanup and disable device */
1246 }
1247
1248 DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
1249
1250 /*
1251 * Create registry key for the instance id, if it doesn't exist yet
1252 */
1253 KeyBuffer = ExAllocatePool(
1254 PagedPool,
1255 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
1256 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1257 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
1258 Status = IopCreateDeviceKeyPath(KeyBuffer,
1259 &InstanceKey);
1260 ExFreePool(KeyBuffer);
1261 if (!NT_SUCCESS(Status))
1262 {
1263 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
1264 }
1265
1266
1267 {
1268 /* Set 'Capabilities' value */
1269 RtlInitUnicodeString(&ValueName,
1270 L"Capabilities");
1271 Status = ZwSetValueKey(InstanceKey,
1272 &ValueName,
1273 0,
1274 REG_DWORD,
1275 (PVOID)&DeviceNode->CapabilityFlags,
1276 sizeof(ULONG));
1277
1278 /* Set 'UINumber' value */
1279 if (DeviceCapabilities.UINumber != (ULONG)-1)
1280 {
1281 RtlInitUnicodeString(&ValueName,
1282 L"UINumber");
1283 Status = ZwSetValueKey(InstanceKey,
1284 &ValueName,
1285 0,
1286 REG_DWORD,
1287 &DeviceCapabilities.UINumber,
1288 sizeof(ULONG));
1289 }
1290 }
1291
1292 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1293
1294 Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
1295 Status = IopInitiatePnpIrp(
1296 DeviceNode->PhysicalDeviceObject,
1297 &IoStatusBlock,
1298 IRP_MN_QUERY_ID,
1299 &Stack);
1300 if (NT_SUCCESS(Status))
1301 {
1302 /*
1303 * FIXME: Check for valid characters, if there is invalid characters
1304 * then bugcheck.
1305 */
1306 TotalLength = 0;
1307 Ptr = (PWSTR)IoStatusBlock.Information;
1308 DPRINT("Hardware IDs:\n");
1309 while (*Ptr)
1310 {
1311 DPRINT(" %S\n", Ptr);
1312 Length = wcslen(Ptr) + 1;
1313
1314 Ptr += Length;
1315 TotalLength += Length;
1316 }
1317 DPRINT("TotalLength: %hu\n", TotalLength);
1318 DPRINT("\n");
1319
1320 RtlInitUnicodeString(&ValueName,
1321 L"HardwareID");
1322 Status = ZwSetValueKey(InstanceKey,
1323 &ValueName,
1324 0,
1325 REG_MULTI_SZ,
1326 (PVOID)IoStatusBlock.Information,
1327 (TotalLength + 1) * sizeof(WCHAR));
1328 if (!NT_SUCCESS(Status))
1329 {
1330 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1331 }
1332 }
1333 else
1334 {
1335 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1336 }
1337
1338 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1339
1340 Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
1341 Status = IopInitiatePnpIrp(
1342 DeviceNode->PhysicalDeviceObject,
1343 &IoStatusBlock,
1344 IRP_MN_QUERY_ID,
1345 &Stack);
1346 if (NT_SUCCESS(Status))
1347 {
1348 /*
1349 * FIXME: Check for valid characters, if there is invalid characters
1350 * then bugcheck.
1351 */
1352 TotalLength = 0;
1353 Ptr = (PWSTR)IoStatusBlock.Information;
1354 DPRINT("Compatible IDs:\n");
1355 while (*Ptr)
1356 {
1357 DPRINT(" %S\n", Ptr);
1358 Length = wcslen(Ptr) + 1;
1359
1360 Ptr += Length;
1361 TotalLength += Length;
1362 }
1363 DPRINT("TotalLength: %hu\n", TotalLength);
1364 DPRINT("\n");
1365
1366 RtlInitUnicodeString(&ValueName,
1367 L"CompatibleIDs");
1368 Status = ZwSetValueKey(InstanceKey,
1369 &ValueName,
1370 0,
1371 REG_MULTI_SZ,
1372 (PVOID)IoStatusBlock.Information,
1373 (TotalLength + 1) * sizeof(WCHAR));
1374 if (!NT_SUCCESS(Status))
1375 {
1376 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1377 }
1378 }
1379 else
1380 {
1381 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1382 }
1383
1384
1385 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1386
1387 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
1388 Stack.Parameters.QueryDeviceText.LocaleId = 0; /* FIXME */
1389 Status = IopInitiatePnpIrp(
1390 DeviceNode->PhysicalDeviceObject,
1391 &IoStatusBlock,
1392 IRP_MN_QUERY_DEVICE_TEXT,
1393 &Stack);
1394 if (NT_SUCCESS(Status))
1395 {
1396 RtlInitUnicodeString(&ValueName,
1397 L"DeviceDesc");
1398 Status = ZwSetValueKey(InstanceKey,
1399 &ValueName,
1400 0,
1401 REG_SZ,
1402 (PVOID)IoStatusBlock.Information,
1403 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1404 if (!NT_SUCCESS(Status))
1405 {
1406 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1407 }
1408 }
1409 else
1410 {
1411 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1412 }
1413
1414 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1415
1416 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextLocationInformation;
1417 Stack.Parameters.QueryDeviceText.LocaleId = 0; // FIXME
1418 Status = IopInitiatePnpIrp(
1419 DeviceNode->PhysicalDeviceObject,
1420 &IoStatusBlock,
1421 IRP_MN_QUERY_DEVICE_TEXT,
1422 &Stack);
1423 if (NT_SUCCESS(Status))
1424 {
1425 DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
1426 RtlInitUnicodeString(&ValueName,
1427 L"LocationInformation");
1428 Status = ZwSetValueKey(InstanceKey,
1429 &ValueName,
1430 0,
1431 REG_SZ,
1432 (PVOID)IoStatusBlock.Information,
1433 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1434 if (!NT_SUCCESS(Status))
1435 {
1436 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1437 }
1438 }
1439 else
1440 {
1441 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1442 }
1443
1444 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1445
1446 Status = IopInitiatePnpIrp(
1447 DeviceNode->PhysicalDeviceObject,
1448 &IoStatusBlock,
1449 IRP_MN_QUERY_BUS_INFORMATION,
1450 NULL);
1451 if (NT_SUCCESS(Status))
1452 {
1453 PPNP_BUS_INFORMATION BusInformation =
1454 (PPNP_BUS_INFORMATION)IoStatusBlock.Information;
1455
1456 DeviceNode->ChildBusNumber = BusInformation->BusNumber;
1457 DeviceNode->ChildInterfaceType = BusInformation->LegacyBusType;
1458 memcpy(&DeviceNode->BusTypeGuid,
1459 &BusInformation->BusTypeGuid,
1460 sizeof(GUID));
1461 ExFreePool(BusInformation);
1462 }
1463 else
1464 {
1465 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1466
1467 DeviceNode->ChildBusNumber = -1;
1468 DeviceNode->ChildInterfaceType = -1;
1469 memset(&DeviceNode->BusTypeGuid,
1470 0,
1471 sizeof(GUID));
1472 }
1473
1474 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1475
1476 Status = IopInitiatePnpIrp(
1477 DeviceNode->PhysicalDeviceObject,
1478 &IoStatusBlock,
1479 IRP_MN_QUERY_RESOURCES,
1480 NULL);
1481 if (NT_SUCCESS(Status))
1482 {
1483 DeviceNode->BootResources =
1484 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
1485 DeviceNode->Flags |= DNF_HAS_BOOT_CONFIG;
1486 }
1487 else
1488 {
1489 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1490 DeviceNode->BootResources = NULL;
1491 }
1492
1493 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1494
1495 Status = IopInitiatePnpIrp(
1496 DeviceNode->PhysicalDeviceObject,
1497 &IoStatusBlock,
1498 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
1499 NULL);
1500 if (NT_SUCCESS(Status))
1501 {
1502 DeviceNode->ResourceRequirements =
1503 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
1504 }
1505 else
1506 {
1507 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1508 DeviceNode->ResourceRequirements = NULL;
1509 }
1510
1511
1512 if (InstanceKey != NULL)
1513 {
1514 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
1515 }
1516
1517 ZwClose(InstanceKey);
1518
1519 Status = IopAssignDeviceResources(DeviceNode);
1520 if (!NT_SUCCESS(Status))
1521 {
1522 DPRINT("IopAssignDeviceResources() failed (Status %x)\n", Status);
1523 }
1524
1525 DeviceNode->Flags |= DNF_PROCESSED;
1526
1527 /* Report the device to the user-mode pnp manager */
1528 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1529 &DeviceNode->InstancePath);
1530
1531 return STATUS_SUCCESS;
1532 }
1533
1534 /*
1535 * IopActionConfigureChildServices
1536 *
1537 * Retrieve configuration for all (direct) child nodes of a parent node.
1538 *
1539 * Parameters
1540 * DeviceNode
1541 * Pointer to device node.
1542 * Context
1543 * Pointer to parent node to retrieve child node configuration for.
1544 *
1545 * Remarks
1546 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1547 * when we reach a device node which is not a direct child of the device
1548 * node for which we configure child services for. Any errors that occur is
1549 * logged instead so that all child services have a chance of beeing
1550 * configured.
1551 */
1552
1553 NTSTATUS
1554 IopActionConfigureChildServices(
1555 PDEVICE_NODE DeviceNode,
1556 PVOID Context)
1557 {
1558 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
1559 PDEVICE_NODE ParentDeviceNode;
1560 PUNICODE_STRING Service;
1561 NTSTATUS Status;
1562
1563 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode, Context);
1564
1565 ParentDeviceNode = (PDEVICE_NODE)Context;
1566
1567 /*
1568 * We are called for the parent too, but we don't need to do special
1569 * handling for this node
1570 */
1571 if (DeviceNode == ParentDeviceNode)
1572 {
1573 DPRINT("Success\n");
1574 return STATUS_SUCCESS;
1575 }
1576
1577 /*
1578 * Make sure this device node is a direct child of the parent device node
1579 * that is given as an argument
1580 */
1581 if (DeviceNode->Parent != ParentDeviceNode)
1582 {
1583 /* Stop the traversal immediately and indicate successful operation */
1584 DPRINT("Stop\n");
1585 return STATUS_UNSUCCESSFUL;
1586 }
1587
1588 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED))
1589 {
1590 WCHAR RegKeyBuffer[MAX_PATH];
1591 UNICODE_STRING RegKey;
1592
1593 RegKey.Length = 0;
1594 RegKey.MaximumLength = sizeof(RegKeyBuffer);
1595 RegKey.Buffer = RegKeyBuffer;
1596
1597 /*
1598 * Retrieve configuration from Enum key
1599 */
1600
1601 Service = &DeviceNode->ServiceName;
1602
1603 RtlZeroMemory(QueryTable, sizeof(QueryTable));
1604 RtlInitUnicodeString(Service, NULL);
1605
1606 QueryTable[0].Name = L"Service";
1607 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1608 QueryTable[0].EntryContext = Service;
1609
1610 RtlAppendUnicodeToString(&RegKey, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1611 RtlAppendUnicodeStringToString(&RegKey, &DeviceNode->InstancePath);
1612
1613 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1614 RegKey.Buffer, QueryTable, NULL, NULL);
1615
1616 if (!NT_SUCCESS(Status))
1617 {
1618 DPRINT("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1619 /* FIXME: Log the error */
1620 CPRINT("Could not retrieve configuration for device %S (Status %x)\n",
1621 DeviceNode->InstancePath.Buffer, Status);
1622 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1623 return STATUS_SUCCESS;
1624 }
1625
1626 if (Service->Buffer == NULL)
1627 {
1628 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1629 return STATUS_SUCCESS;
1630 }
1631
1632 DPRINT("Got Service %S\n", Service->Buffer);
1633 }
1634
1635 return STATUS_SUCCESS;
1636 }
1637
1638 /*
1639 * IopActionInitChildServices
1640 *
1641 * Initialize the service for all (direct) child nodes of a parent node
1642 *
1643 * Parameters
1644 * DeviceNode
1645 * Pointer to device node.
1646 * Context
1647 * Pointer to parent node to initialize child node services for.
1648 * BootDrivers
1649 * Load only driver marked as boot start.
1650 *
1651 * Remarks
1652 * If the driver image for a service is not loaded and initialized
1653 * it is done here too. We only return a status code indicating an
1654 * error (STATUS_UNSUCCESSFUL) when we reach a device node which is
1655 * not a direct child of the device node for which we initialize
1656 * child services for. Any errors that occur is logged instead so
1657 * that all child services have a chance of being initialized.
1658 */
1659
1660 NTSTATUS
1661 IopActionInitChildServices(
1662 PDEVICE_NODE DeviceNode,
1663 PVOID Context,
1664 BOOLEAN BootDrivers)
1665 {
1666 PDEVICE_NODE ParentDeviceNode;
1667 NTSTATUS Status;
1668
1669 DPRINT("IopActionInitChildServices(%p, %p, %d)\n", DeviceNode, Context,
1670 BootDrivers);
1671
1672 ParentDeviceNode = (PDEVICE_NODE)Context;
1673
1674 /*
1675 * We are called for the parent too, but we don't need to do special
1676 * handling for this node
1677 */
1678 if (DeviceNode == ParentDeviceNode)
1679 {
1680 DPRINT("Success\n");
1681 return STATUS_SUCCESS;
1682 }
1683
1684 /*
1685 * Make sure this device node is a direct child of the parent device node
1686 * that is given as an argument
1687 */
1688 #if 0
1689 if (DeviceNode->Parent != ParentDeviceNode)
1690 {
1691 /*
1692 * Stop the traversal immediately and indicate unsuccessful operation
1693 */
1694 DPRINT("Stop\n");
1695 return STATUS_UNSUCCESSFUL;
1696 }
1697 #endif
1698
1699 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED) &&
1700 !IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) &&
1701 !IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED))
1702 {
1703 PMODULE_OBJECT ModuleObject;
1704 PDRIVER_OBJECT DriverObject;
1705
1706 Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
1707 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
1708 {
1709 if (Status != STATUS_IMAGE_ALREADY_LOADED)
1710 Status = IopInitializeDriverModule(DeviceNode, ModuleObject,
1711 &DeviceNode->ServiceName, FALSE, &DriverObject);
1712 else
1713 {
1714 /* get existing DriverObject pointer */
1715 Status = IopGetDriverObject(
1716 &DriverObject,
1717 &DeviceNode->ServiceName,
1718 FALSE);
1719 }
1720 if (NT_SUCCESS(Status))
1721 {
1722 /* Attach lower level filter drivers. */
1723 IopAttachFilterDrivers(DeviceNode, TRUE);
1724 /* Initialize the function driver for the device node */
1725 Status = IopInitializeDevice(DeviceNode, DriverObject);
1726 if (NT_SUCCESS(Status))
1727 {
1728 /* Attach upper level filter drivers. */
1729 IopAttachFilterDrivers(DeviceNode, FALSE);
1730 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
1731
1732 Status = IopStartDevice(DeviceNode);
1733 }
1734 }
1735 }
1736 else
1737 {
1738 /*
1739 * Don't disable when trying to load only boot drivers
1740 */
1741 if (!BootDrivers)
1742 {
1743 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1744 IopDeviceNodeSetFlag(DeviceNode, DNF_START_FAILED);
1745 }
1746 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
1747 CPRINT("Initialization of service %S failed (Status %x)\n",
1748 DeviceNode->ServiceName.Buffer, Status);
1749 }
1750 } else
1751 {
1752 DPRINT("Service %S is disabled or already initialized\n",
1753 DeviceNode->ServiceName.Buffer);
1754 }
1755
1756 return STATUS_SUCCESS;
1757 }
1758
1759 /*
1760 * IopActionInitAllServices
1761 *
1762 * Initialize the service for all (direct) child nodes of a parent node. This
1763 * function just calls IopActionInitChildServices with BootDrivers = FALSE.
1764 */
1765
1766 NTSTATUS
1767 IopActionInitAllServices(
1768 PDEVICE_NODE DeviceNode,
1769 PVOID Context)
1770 {
1771 return IopActionInitChildServices(DeviceNode, Context, FALSE);
1772 }
1773
1774 /*
1775 * IopActionInitBootServices
1776 *
1777 * Initialize the boot start services for all (direct) child nodes of a
1778 * parent node. This function just calls IopActionInitChildServices with
1779 * BootDrivers = TRUE.
1780 */
1781 NTSTATUS
1782 IopActionInitBootServices(
1783 PDEVICE_NODE DeviceNode,
1784 PVOID Context)
1785 {
1786 return IopActionInitChildServices(DeviceNode, Context, TRUE);
1787 }
1788
1789 /*
1790 * IopInitializePnpServices
1791 *
1792 * Initialize services for discovered children
1793 *
1794 * Parameters
1795 * DeviceNode
1796 * Top device node to start initializing services.
1797 *
1798 * BootDrivers
1799 * When set to TRUE, only drivers marked as boot start will
1800 * be loaded. Otherwise, all drivers will be loaded.
1801 *
1802 * Return Value
1803 * Status
1804 */
1805 NTSTATUS
1806 IopInitializePnpServices(
1807 IN PDEVICE_NODE DeviceNode,
1808 IN BOOLEAN BootDrivers)
1809 {
1810 DEVICETREE_TRAVERSE_CONTEXT Context;
1811
1812 DPRINT("IopInitializePnpServices(%p, %d)\n", DeviceNode, BootDrivers);
1813
1814 if (BootDrivers)
1815 {
1816 IopInitDeviceTreeTraverseContext(
1817 &Context,
1818 DeviceNode,
1819 IopActionInitBootServices,
1820 DeviceNode);
1821 } else
1822 {
1823 IopInitDeviceTreeTraverseContext(
1824 &Context,
1825 DeviceNode,
1826 IopActionInitAllServices,
1827 DeviceNode);
1828 }
1829
1830 return IopTraverseDeviceTree(&Context);
1831 }
1832
1833
1834 NTSTATUS
1835 IopInvalidateDeviceRelations(
1836 IN PDEVICE_NODE DeviceNode,
1837 IN DEVICE_RELATION_TYPE Type)
1838 {
1839 DEVICETREE_TRAVERSE_CONTEXT Context;
1840 PDEVICE_RELATIONS DeviceRelations;
1841 IO_STATUS_BLOCK IoStatusBlock;
1842 PDEVICE_NODE ChildDeviceNode;
1843 IO_STACK_LOCATION Stack;
1844 BOOL BootDrivers;
1845 OBJECT_ATTRIBUTES ObjectAttributes;
1846 UNICODE_STRING LinkName;
1847 HANDLE Handle;
1848 NTSTATUS Status;
1849 ULONG i;
1850
1851 DPRINT("DeviceNode %x\n", DeviceNode);
1852
1853 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
1854
1855 Stack.Parameters.QueryDeviceRelations.Type = Type/*BusRelations*/;
1856
1857 Status = IopInitiatePnpIrp(
1858 DeviceNode->PhysicalDeviceObject,
1859 &IoStatusBlock,
1860 IRP_MN_QUERY_DEVICE_RELATIONS,
1861 &Stack);
1862 if (!NT_SUCCESS(Status))
1863 {
1864 DPRINT("IopInitiatePnpIrp() failed\n");
1865 return Status;
1866 }
1867
1868 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
1869
1870 if ((!DeviceRelations) || (DeviceRelations->Count <= 0))
1871 {
1872 DPRINT("No PDOs\n");
1873 if (DeviceRelations)
1874 {
1875 ExFreePool(DeviceRelations);
1876 }
1877 return STATUS_SUCCESS;
1878 }
1879
1880 DPRINT("Got %d PDOs\n", DeviceRelations->Count);
1881
1882 /*
1883 * Create device nodes for all discovered devices
1884 */
1885
1886 for (i = 0; i < DeviceRelations->Count; i++)
1887 {
1888 Status = IopCreateDeviceNode(
1889 DeviceNode,
1890 DeviceRelations->Objects[i],
1891 &ChildDeviceNode);
1892 DeviceNode->Flags |= DNF_ENUMERATED;
1893 if (!NT_SUCCESS(Status))
1894 {
1895 DPRINT("No resources\n");
1896 for (i = 0; i < DeviceRelations->Count; i++)
1897 ObDereferenceObject(DeviceRelations->Objects[i]);
1898 ExFreePool(DeviceRelations);
1899 return STATUS_INSUFFICIENT_RESOURCES;
1900 }
1901 }
1902 ExFreePool(DeviceRelations);
1903
1904 /*
1905 * Retrieve information about all discovered children from the bus driver
1906 */
1907
1908 IopInitDeviceTreeTraverseContext(
1909 &Context,
1910 DeviceNode,
1911 IopActionInterrogateDeviceStack,
1912 DeviceNode);
1913
1914 Status = IopTraverseDeviceTree(&Context);
1915 if (!NT_SUCCESS(Status))
1916 {
1917 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
1918 return Status;
1919 }
1920
1921 /*
1922 * Retrieve configuration from the registry for discovered children
1923 */
1924
1925 IopInitDeviceTreeTraverseContext(
1926 &Context,
1927 DeviceNode,
1928 IopActionConfigureChildServices,
1929 DeviceNode);
1930
1931 Status = IopTraverseDeviceTree(&Context);
1932 if (!NT_SUCCESS(Status))
1933 {
1934 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
1935 return Status;
1936 }
1937
1938 /*
1939 * Get the state of the system boot. If the \\SystemRoot link isn't
1940 * created yet, we will assume that it's possible to load only boot
1941 * drivers.
1942 */
1943
1944 RtlInitUnicodeString(&LinkName, L"\\SystemRoot");
1945
1946 InitializeObjectAttributes(
1947 &ObjectAttributes,
1948 &LinkName,
1949 0,
1950 NULL,
1951 NULL);
1952
1953 Status = ZwOpenFile(
1954 &Handle,
1955 FILE_ALL_ACCESS,
1956 &ObjectAttributes,
1957 &IoStatusBlock,
1958 0,
1959 0);
1960 if(NT_SUCCESS(Status))
1961 {
1962 BootDrivers = FALSE;
1963 ZwClose(Handle);
1964 }
1965 else
1966 BootDrivers = TRUE;
1967
1968 /*
1969 * Initialize services for discovered children. Only boot drivers will
1970 * be loaded from boot driver!
1971 */
1972
1973 Status = IopInitializePnpServices(DeviceNode, BootDrivers);
1974 if (!NT_SUCCESS(Status))
1975 {
1976 DPRINT("IopInitializePnpServices() failed with status (%x)\n", Status);
1977 return Status;
1978 }
1979
1980 return STATUS_SUCCESS;
1981 }
1982
1983
1984 VOID INIT_FUNCTION
1985 PnpInit(VOID)
1986 {
1987 PDEVICE_OBJECT Pdo;
1988 NTSTATUS Status;
1989
1990 DPRINT("PnpInit()\n");
1991
1992 KeInitializeSpinLock(&IopDeviceTreeLock);
1993
1994 /* Initialize PnP-Event notification support */
1995 Status = IopInitPlugPlayEvents();
1996 if (!NT_SUCCESS(Status))
1997 {
1998 CPRINT("IopInitPlugPlayEvents() failed\n");
1999 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2000 }
2001
2002 /*
2003 * Create root device node
2004 */
2005
2006 Status = IopCreateDriverObject(&IopRootDriverObject, NULL, 0, FALSE, NULL, 0);
2007 if (!NT_SUCCESS(Status))
2008 {
2009 CPRINT("IoCreateDriverObject() failed\n");
2010 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2011 }
2012
2013 Status = IoCreateDevice(IopRootDriverObject, 0, NULL, FILE_DEVICE_CONTROLLER,
2014 0, FALSE, &Pdo);
2015 if (!NT_SUCCESS(Status))
2016 {
2017 CPRINT("IoCreateDevice() failed\n");
2018 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2019 }
2020
2021 Status = IopCreateDeviceNode(NULL, Pdo, &IopRootDeviceNode);
2022 if (!NT_SUCCESS(Status))
2023 {
2024 CPRINT("Insufficient resources\n");
2025 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2026 }
2027
2028 if (!IopCreateUnicodeString(&IopRootDeviceNode->InstancePath,
2029 L"HTREE\\Root\\0",
2030 PagedPool))
2031 {
2032 CPRINT("Failed to create the instance path!\n");
2033 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, STATUS_UNSUCCESSFUL, 0, 0, 0);
2034 }
2035
2036 /* Report the device to the user-mode pnp manager */
2037 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
2038 &IopRootDeviceNode->InstancePath);
2039
2040 IopRootDeviceNode->PhysicalDeviceObject->Flags |= DO_BUS_ENUMERATED_DEVICE;
2041 PnpRootDriverEntry(IopRootDriverObject, NULL);
2042 IopRootDriverObject->DriverExtension->AddDevice(
2043 IopRootDriverObject,
2044 IopRootDeviceNode->PhysicalDeviceObject);
2045 }
2046
2047 /* EOF */