2 * PROJECT: ReactOS Kernel
3 * COPYRIGHT: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/pnpmgr/plugplay.c
5 * PURPOSE: Plug-and-play interface routines
6 * PROGRAMMERS: Eric Kohl <eric.kohl@t-online.de>
9 /* INCLUDES *****************************************************************/
15 #if defined (ALLOC_PRAGMA)
16 #pragma alloc_text(INIT, IopInitPlugPlayEvents)
19 typedef struct _PNP_EVENT_ENTRY
22 PLUGPLAY_EVENT_BLOCK Event
;
23 } PNP_EVENT_ENTRY
, *PPNP_EVENT_ENTRY
;
26 /* GLOBALS *******************************************************************/
28 static LIST_ENTRY IopPnpEventQueueHead
;
29 static KEVENT IopPnpNotifyEvent
;
31 /* FUNCTIONS *****************************************************************/
33 NTSTATUS INIT_FUNCTION
34 IopInitPlugPlayEvents(VOID
)
36 InitializeListHead(&IopPnpEventQueueHead
);
38 KeInitializeEvent(&IopPnpNotifyEvent
,
42 return STATUS_SUCCESS
;
46 IopQueueTargetDeviceEvent(const GUID
*Guid
,
47 PUNICODE_STRING DeviceIds
)
49 PPNP_EVENT_ENTRY EventEntry
;
56 /* Allocate a big enough buffer */
58 Copy
.MaximumLength
= DeviceIds
->Length
+ sizeof(UNICODE_NULL
);
60 FIELD_OFFSET(PLUGPLAY_EVENT_BLOCK
, TargetDevice
.DeviceIds
) +
63 EventEntry
= ExAllocatePool(NonPagedPool
,
64 TotalSize
+ FIELD_OFFSET(PNP_EVENT_ENTRY
, Event
));
66 return STATUS_INSUFFICIENT_RESOURCES
;
68 /* Fill the buffer with the event GUID */
69 RtlCopyMemory(&EventEntry
->Event
.EventGuid
,
72 EventEntry
->Event
.EventCategory
= TargetDeviceChangeEvent
;
73 EventEntry
->Event
.TotalSize
= TotalSize
;
75 /* Fill the device id */
76 Copy
.Buffer
= EventEntry
->Event
.TargetDevice
.DeviceIds
;
77 Status
= RtlAppendUnicodeStringToString(&Copy
, DeviceIds
);
78 if (!NT_SUCCESS(Status
))
80 ExFreePool(EventEntry
);
84 InsertHeadList(&IopPnpEventQueueHead
,
85 &EventEntry
->ListEntry
);
86 KeSetEvent(&IopPnpNotifyEvent
,
90 return STATUS_SUCCESS
;
95 * Remove the current PnP event from the tail of the event queue
96 * and signal IopPnpNotifyEvent if there is yet another event in the queue.
99 IopRemovePlugPlayEvent(VOID
)
101 /* Remove a pnp event entry from the tail of the queue */
102 if (!IsListEmpty(&IopPnpEventQueueHead
))
104 ExFreePool(CONTAINING_RECORD(RemoveTailList(&IopPnpEventQueueHead
), PNP_EVENT_ENTRY
, ListEntry
));
107 /* Signal the next pnp event in the queue */
108 if (!IsListEmpty(&IopPnpEventQueueHead
))
110 KeSetEvent(&IopPnpNotifyEvent
,
115 return STATUS_SUCCESS
;
118 static PDEVICE_OBJECT
119 IopTraverseDeviceNode(PDEVICE_NODE Node
, PUNICODE_STRING DeviceInstance
)
121 PDEVICE_OBJECT DeviceObject
;
122 PDEVICE_NODE ChildNode
;
124 if (RtlEqualUnicodeString(&Node
->InstancePath
,
125 DeviceInstance
, TRUE
))
127 ObReferenceObject(Node
->PhysicalDeviceObject
);
128 return Node
->PhysicalDeviceObject
;
131 /* Traversal of all children nodes */
132 for (ChildNode
= Node
->Child
;
134 ChildNode
= ChildNode
->Sibling
)
136 DeviceObject
= IopTraverseDeviceNode(ChildNode
, DeviceInstance
);
137 if (DeviceObject
!= NULL
)
148 IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance
)
150 if (IopRootDeviceNode
== NULL
)
153 if (DeviceInstance
== NULL
||
154 DeviceInstance
->Length
== 0)
156 if (IopRootDeviceNode
->PhysicalDeviceObject
)
158 ObReferenceObject(IopRootDeviceNode
->PhysicalDeviceObject
);
159 return IopRootDeviceNode
->PhysicalDeviceObject
;
165 return IopTraverseDeviceNode(IopRootDeviceNode
, DeviceInstance
);
169 IopCaptureUnicodeString(PUNICODE_STRING DstName
, PUNICODE_STRING SrcName
)
171 NTSTATUS Status
= STATUS_SUCCESS
;
172 volatile UNICODE_STRING Name
;
177 Name
.Length
= SrcName
->Length
;
178 Name
.MaximumLength
= SrcName
->MaximumLength
;
179 if (Name
.Length
> Name
.MaximumLength
)
181 Status
= STATUS_INVALID_PARAMETER
;
185 if (Name
.MaximumLength
)
187 ProbeForRead(SrcName
->Buffer
,
190 Name
.Buffer
= ExAllocatePool(NonPagedPool
, Name
.MaximumLength
);
191 if (Name
.Buffer
== NULL
)
193 Status
= STATUS_INSUFFICIENT_RESOURCES
;
197 memcpy(Name
.Buffer
, SrcName
->Buffer
, Name
.MaximumLength
);
202 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
206 ExFreePool(Name
.Buffer
);
208 Status
= _SEH2_GetExceptionCode();
216 IopGetInterfaceDeviceList(PPLUGPLAY_CONTROL_INTERFACE_DEVICE_LIST_DATA DeviceList
)
219 PLUGPLAY_CONTROL_INTERFACE_DEVICE_LIST_DATA StackList
;
220 UNICODE_STRING DeviceInstance
;
221 PDEVICE_OBJECT DeviceObject
= NULL
;
223 PZZWSTR SymbolicLinkList
= NULL
, LinkList
;
228 RtlCopyMemory(&StackList
, DeviceList
, sizeof(PLUGPLAY_CONTROL_INTERFACE_DEVICE_LIST_DATA
));
230 ProbeForRead(StackList
.FilterGuid
, sizeof(GUID
), sizeof(UCHAR
));
231 RtlCopyMemory(&FilterGuid
, StackList
.FilterGuid
, sizeof(GUID
));
233 if (StackList
.Buffer
!= NULL
&& StackList
.BufferSize
!= 0)
235 ProbeForWrite(StackList
.Buffer
, StackList
.BufferSize
, sizeof(UCHAR
));
238 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
240 _SEH2_YIELD(return _SEH2_GetExceptionCode());
244 Status
= IopCaptureUnicodeString(&DeviceInstance
, &StackList
.DeviceInstance
);
245 if (NT_SUCCESS(Status
))
247 /* Get the device object */
248 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
249 if (DeviceInstance
.Buffer
!= NULL
)
251 ExFreePool(DeviceInstance
.Buffer
);
255 Status
= IoGetDeviceInterfaces(&FilterGuid
, DeviceObject
, StackList
.Flags
, &SymbolicLinkList
);
256 ObDereferenceObject(DeviceObject
);
258 if (!NT_SUCCESS(Status
))
264 LinkList
= SymbolicLinkList
;
265 while (*SymbolicLinkList
!= UNICODE_NULL
)
267 SymbolicLinkList
+= wcslen(SymbolicLinkList
) + (sizeof(UNICODE_NULL
) / sizeof(WCHAR
));
269 TotalLength
= ((SymbolicLinkList
- LinkList
+ 1) * sizeof(WCHAR
));
273 if (StackList
.Buffer
!= NULL
&&
274 StackList
.BufferSize
>= TotalLength
)
276 // We've already probed the buffer for writing above.
277 RtlCopyMemory(StackList
.Buffer
, LinkList
, TotalLength
);
280 DeviceList
->BufferSize
= TotalLength
;
282 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
284 ExFreePool(LinkList
);
285 _SEH2_YIELD(return _SEH2_GetExceptionCode());
289 ExFreePool(LinkList
);
290 return STATUS_SUCCESS
;
294 IopGetDeviceProperty(PPLUGPLAY_CONTROL_PROPERTY_DATA PropertyData
)
296 PDEVICE_OBJECT DeviceObject
= NULL
;
297 PDEVICE_NODE DeviceNode
;
298 UNICODE_STRING DeviceInstance
;
301 DEVICE_REGISTRY_PROPERTY DeviceProperty
;
305 DPRINT("IopGetDeviceProperty() called\n");
306 DPRINT("Device name: %wZ\n", &PropertyData
->DeviceInstance
);
308 Status
= IopCaptureUnicodeString(&DeviceInstance
, &PropertyData
->DeviceInstance
);
309 if (!NT_SUCCESS(Status
))
316 Property
= PropertyData
->Property
;
317 BufferSize
= PropertyData
->BufferSize
;
318 ProbeForWrite(PropertyData
->Buffer
,
322 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
324 if (DeviceInstance
.Buffer
!= NULL
)
326 ExFreePool(DeviceInstance
.Buffer
);
328 _SEH2_YIELD(return _SEH2_GetExceptionCode());
332 /* Get the device object */
333 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
334 if (DeviceInstance
.Buffer
!= NULL
)
336 ExFreePool(DeviceInstance
.Buffer
);
338 if (DeviceObject
== NULL
)
340 return STATUS_NO_SUCH_DEVICE
;
343 Buffer
= ExAllocatePool(NonPagedPool
, BufferSize
);
346 ObDereferenceObject(DeviceObject
);
347 return STATUS_INSUFFICIENT_RESOURCES
;
351 DeviceNode
= ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
353 if (Property
== PNP_PROPERTY_POWER_DATA
)
355 if (BufferSize
< sizeof(CM_POWER_DATA
))
358 Status
= STATUS_BUFFER_TOO_SMALL
;
362 DEVICE_CAPABILITIES DeviceCapabilities
;
363 PCM_POWER_DATA PowerData
;
364 IO_STACK_LOCATION Stack
;
365 IO_STATUS_BLOCK IoStatusBlock
;
367 PowerData
= (PCM_POWER_DATA
)Buffer
;
368 RtlZeroMemory(PowerData
, sizeof(CM_POWER_DATA
));
369 PowerData
->PD_Size
= sizeof(CM_POWER_DATA
);
371 RtlZeroMemory(&DeviceCapabilities
, sizeof(DEVICE_CAPABILITIES
));
372 DeviceCapabilities
.Size
= sizeof(DEVICE_CAPABILITIES
);
373 DeviceCapabilities
.Version
= 1;
374 DeviceCapabilities
.Address
= -1;
375 DeviceCapabilities
.UINumber
= -1;
377 Stack
.Parameters
.DeviceCapabilities
.Capabilities
= &DeviceCapabilities
;
379 Status
= IopInitiatePnpIrp(DeviceObject
,
381 IRP_MN_QUERY_CAPABILITIES
,
383 if (NT_SUCCESS(Status
))
385 DPRINT("Got device capabiliities\n");
387 PowerData
->PD_MostRecentPowerState
= PowerDeviceD0
; // FIXME
388 if (DeviceCapabilities
.DeviceD1
)
389 PowerData
->PD_Capabilities
|= PDCAP_D1_SUPPORTED
;
390 if (DeviceCapabilities
.DeviceD2
)
391 PowerData
->PD_Capabilities
|= PDCAP_D2_SUPPORTED
;
392 if (DeviceCapabilities
.WakeFromD0
)
393 PowerData
->PD_Capabilities
|= PDCAP_WAKE_FROM_D0_SUPPORTED
;
394 if (DeviceCapabilities
.WakeFromD1
)
395 PowerData
->PD_Capabilities
|= PDCAP_WAKE_FROM_D1_SUPPORTED
;
396 if (DeviceCapabilities
.WakeFromD2
)
397 PowerData
->PD_Capabilities
|= PDCAP_WAKE_FROM_D2_SUPPORTED
;
398 if (DeviceCapabilities
.WakeFromD3
)
399 PowerData
->PD_Capabilities
|= PDCAP_WAKE_FROM_D3_SUPPORTED
;
400 if (DeviceCapabilities
.WarmEjectSupported
)
401 PowerData
->PD_Capabilities
|= PDCAP_WARM_EJECT_SUPPORTED
;
402 PowerData
->PD_D1Latency
= DeviceCapabilities
.D1Latency
;
403 PowerData
->PD_D2Latency
= DeviceCapabilities
.D2Latency
;
404 PowerData
->PD_D3Latency
= DeviceCapabilities
.D3Latency
;
405 RtlCopyMemory(&PowerData
->PD_PowerStateMapping
,
406 &DeviceCapabilities
.DeviceState
,
407 sizeof(DeviceCapabilities
.DeviceState
));
408 PowerData
->PD_DeepestSystemWake
= DeviceCapabilities
.SystemWake
;
412 DPRINT("IRP_MN_QUERY_CAPABILITIES failed (Status 0x%08lx)\n", Status
);
414 PowerData
->PD_Capabilities
= PDCAP_D0_SUPPORTED
| PDCAP_D3_SUPPORTED
;
415 PowerData
->PD_MostRecentPowerState
= PowerDeviceD0
;
419 else if (Property
== PNP_PROPERTY_REMOVAL_POLICY_OVERRIDE
)
422 else if (Property
== PNP_PROPERTY_REMOVAL_POLICY_HARDWARE_DEFAULT
)
424 if (BufferSize
< sizeof(DeviceNode
->HardwareRemovalPolicy
))
427 Status
= STATUS_BUFFER_TOO_SMALL
;
431 BufferSize
= sizeof(DeviceNode
->HardwareRemovalPolicy
);
432 RtlCopyMemory(Buffer
,
433 &DeviceNode
->HardwareRemovalPolicy
,
441 case PNP_PROPERTY_UI_NUMBER
:
442 DeviceProperty
= DevicePropertyUINumber
;
445 case PNP_PROPERTY_PHYSICAL_DEVICE_OBJECT_NAME
:
446 DeviceProperty
= DevicePropertyPhysicalDeviceObjectName
;
449 case PNP_PROPERTY_BUSTYPEGUID
:
450 DeviceProperty
= DevicePropertyBusTypeGuid
;
453 case PNP_PROPERTY_LEGACYBUSTYPE
:
454 DeviceProperty
= DevicePropertyLegacyBusType
;
457 case PNP_PROPERTY_BUSNUMBER
:
458 DeviceProperty
= DevicePropertyBusNumber
;
461 case PNP_PROPERTY_REMOVAL_POLICY
:
462 DeviceProperty
= DevicePropertyRemovalPolicy
;
465 case PNP_PROPERTY_ADDRESS
:
466 DeviceProperty
= DevicePropertyAddress
;
469 case PNP_PROPERTY_ENUMERATOR_NAME
:
470 DeviceProperty
= DevicePropertyEnumeratorName
;
473 case PNP_PROPERTY_INSTALL_STATE
:
474 DeviceProperty
= DevicePropertyInstallState
;
477 #if (WINVER >= _WIN32_WINNT_WS03)
478 case PNP_PROPERTY_LOCATION_PATHS
:
482 #if (WINVER >= _WIN32_WINNT_WIN7)
483 case PNP_PROPERTY_CONTAINERID
:
484 DeviceProperty
= DevicePropertyContainerID
;
490 Status
= STATUS_INVALID_PARAMETER
;
494 if (Status
== STATUS_SUCCESS
)
496 Status
= IoGetDeviceProperty(DeviceObject
,
504 ObDereferenceObject(DeviceObject
);
506 if (NT_SUCCESS(Status
))
510 RtlCopyMemory(PropertyData
->Buffer
, Buffer
, BufferSize
);
511 PropertyData
->BufferSize
= BufferSize
;
513 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
515 Status
= _SEH2_GetExceptionCode();
526 IopGetRelatedDevice(PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA RelatedDeviceData
)
528 UNICODE_STRING RootDeviceName
;
529 PDEVICE_OBJECT DeviceObject
= NULL
;
530 PDEVICE_NODE DeviceNode
= NULL
;
531 PDEVICE_NODE RelatedDeviceNode
;
532 UNICODE_STRING TargetDeviceInstance
;
533 NTSTATUS Status
= STATUS_SUCCESS
;
535 ULONG MaximumLength
= 0;
537 DPRINT("IopGetRelatedDevice() called\n");
538 DPRINT("Device name: %wZ\n", &RelatedDeviceData
->TargetDeviceInstance
);
540 Status
= IopCaptureUnicodeString(&TargetDeviceInstance
, &RelatedDeviceData
->TargetDeviceInstance
);
541 if (!NT_SUCCESS(Status
))
548 Relation
= RelatedDeviceData
->Relation
;
549 MaximumLength
= RelatedDeviceData
->RelatedDeviceInstanceLength
;
550 ProbeForWrite(RelatedDeviceData
->RelatedDeviceInstance
,
554 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
556 if (TargetDeviceInstance
.Buffer
!= NULL
)
558 ExFreePool(TargetDeviceInstance
.Buffer
);
560 _SEH2_YIELD(return _SEH2_GetExceptionCode());
564 RtlInitUnicodeString(&RootDeviceName
,
566 if (RtlEqualUnicodeString(&TargetDeviceInstance
,
570 DeviceNode
= IopRootDeviceNode
;
571 if (TargetDeviceInstance
.Buffer
!= NULL
)
573 ExFreePool(TargetDeviceInstance
.Buffer
);
578 /* Get the device object */
579 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&TargetDeviceInstance
);
580 if (TargetDeviceInstance
.Buffer
!= NULL
)
582 ExFreePool(TargetDeviceInstance
.Buffer
);
584 if (DeviceObject
== NULL
)
585 return STATUS_NO_SUCH_DEVICE
;
587 DeviceNode
= ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
592 case PNP_GET_PARENT_DEVICE
:
593 RelatedDeviceNode
= DeviceNode
->Parent
;
596 case PNP_GET_CHILD_DEVICE
:
597 RelatedDeviceNode
= DeviceNode
->Child
;
600 case PNP_GET_SIBLING_DEVICE
:
601 RelatedDeviceNode
= DeviceNode
->Sibling
;
605 if (DeviceObject
!= NULL
)
607 ObDereferenceObject(DeviceObject
);
610 return STATUS_INVALID_PARAMETER
;
613 if (RelatedDeviceNode
== NULL
)
617 ObDereferenceObject(DeviceObject
);
620 return STATUS_NO_SUCH_DEVICE
;
623 if (RelatedDeviceNode
->InstancePath
.Length
> MaximumLength
)
627 ObDereferenceObject(DeviceObject
);
630 return STATUS_BUFFER_TOO_SMALL
;
633 /* Copy related device instance name */
636 RtlCopyMemory(RelatedDeviceData
->RelatedDeviceInstance
,
637 RelatedDeviceNode
->InstancePath
.Buffer
,
638 RelatedDeviceNode
->InstancePath
.Length
);
639 RelatedDeviceData
->RelatedDeviceInstanceLength
= RelatedDeviceNode
->InstancePath
.Length
;
641 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
643 Status
= _SEH2_GetExceptionCode();
647 if (DeviceObject
!= NULL
)
649 ObDereferenceObject(DeviceObject
);
652 DPRINT("IopGetRelatedDevice() done\n");
658 IopGetDeviceNodeStatus(PDEVICE_NODE DeviceNode
)
662 if (DeviceNode
->Parent
== IopRootDeviceNode
)
663 Output
|= DN_ROOT_ENUMERATED
;
665 if (DeviceNode
->Flags
& DNF_ADDED
)
666 Output
|= DN_DRIVER_LOADED
;
668 /* FIXME: DN_ENUM_LOADED */
670 if (DeviceNode
->Flags
& DNF_STARTED
)
671 Output
|= DN_STARTED
;
675 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
676 Output
|= DN_NEED_TO_ENUM
;
678 /* DN_NOT_FIRST_TIME is 9x only */
680 /* FIXME: DN_HARDWARE_ENUM */
682 /* DN_LIAR and DN_HAS_MARK are 9x only */
684 if (DeviceNode
->Problem
!= 0)
685 Output
|= DN_HAS_PROBLEM
;
687 /* FIXME: DN_FILTERED */
689 if (DeviceNode
->Flags
& DNF_LEGACY_DRIVER
)
690 Output
|= DN_LEGACY_DRIVER
;
692 if (DeviceNode
->UserFlags
& DNUF_DONT_SHOW_IN_UI
)
693 Output
|= DN_NO_SHOW_IN_DM
;
695 if (!(DeviceNode
->UserFlags
& DNUF_NOT_DISABLEABLE
))
696 Output
|= DN_DISABLEABLE
;
698 /* FIXME: Implement the rest */
700 Output
|= DN_NT_ENUMERATOR
| DN_NT_DRIVER
;
706 IopDeviceStatus(PPLUGPLAY_CONTROL_STATUS_DATA StatusData
)
708 PDEVICE_OBJECT DeviceObject
;
709 PDEVICE_NODE DeviceNode
;
711 ULONG DeviceStatus
= 0;
712 ULONG DeviceProblem
= 0;
713 UNICODE_STRING DeviceInstance
;
716 DPRINT("IopDeviceStatus() called\n");
718 Status
= IopCaptureUnicodeString(&DeviceInstance
, &StatusData
->DeviceInstance
);
719 if (!NT_SUCCESS(Status
))
724 DPRINT("Device name: '%wZ'\n", &DeviceInstance
);
728 Operation
= StatusData
->Operation
;
729 if (Operation
== PNP_SET_DEVICE_STATUS
)
731 DeviceStatus
= StatusData
->DeviceStatus
;
732 DeviceProblem
= StatusData
->DeviceProblem
;
735 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
737 if (DeviceInstance
.Buffer
!= NULL
)
739 ExFreePool(DeviceInstance
.Buffer
);
741 _SEH2_YIELD(return _SEH2_GetExceptionCode());
745 /* Get the device object */
746 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
747 if (DeviceInstance
.Buffer
!= NULL
)
749 ExFreePool(DeviceInstance
.Buffer
);
751 if (DeviceObject
== NULL
)
753 return STATUS_NO_SUCH_DEVICE
;
756 DeviceNode
= IopGetDeviceNode(DeviceObject
);
760 case PNP_GET_DEVICE_STATUS
:
761 DPRINT("Get status data\n");
762 DeviceStatus
= IopGetDeviceNodeStatus(DeviceNode
);
763 DeviceProblem
= DeviceNode
->Problem
;
766 case PNP_SET_DEVICE_STATUS
:
767 DPRINT1("Set status data is NOT SUPPORTED\n");
770 case PNP_CLEAR_DEVICE_STATUS
:
771 DPRINT1("FIXME: Clear status data!\n");
775 ObDereferenceObject(DeviceObject
);
777 if (Operation
== PNP_GET_DEVICE_STATUS
)
781 StatusData
->DeviceStatus
= DeviceStatus
;
782 StatusData
->DeviceProblem
= DeviceProblem
;
784 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
786 Status
= _SEH2_GetExceptionCode();
796 IopGetDeviceRelations(PPLUGPLAY_CONTROL_DEVICE_RELATIONS_DATA RelationsData
)
798 UNICODE_STRING DeviceInstance
;
799 PDEVICE_OBJECT DeviceObject
= NULL
;
800 IO_STACK_LOCATION Stack
;
801 IO_STATUS_BLOCK IoStatusBlock
;
802 PDEVICE_RELATIONS DeviceRelations
= NULL
;
803 PDEVICE_OBJECT ChildDeviceObject
;
804 PDEVICE_NODE ChildDeviceNode
;
807 ULONG BufferSize
, RequiredSize
;
810 NTSTATUS Status
= STATUS_SUCCESS
;
812 DPRINT("IopGetDeviceRelations() called\n");
813 DPRINT("Device name: %wZ\n", &RelationsData
->DeviceInstance
);
814 DPRINT("Relations: %lu\n", RelationsData
->Relations
);
815 DPRINT("BufferSize: %lu\n", RelationsData
->BufferSize
);
816 DPRINT("Buffer: %p\n", RelationsData
->Buffer
);
820 Relations
= RelationsData
->Relations
;
821 BufferSize
= RelationsData
->BufferSize
;
822 Buffer
= RelationsData
->Buffer
;
824 ProbeForWrite(Buffer
, BufferSize
, sizeof(CHAR
));
826 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
828 _SEH2_YIELD(return _SEH2_GetExceptionCode());
832 Status
= IopCaptureUnicodeString(&DeviceInstance
, &RelationsData
->DeviceInstance
);
833 if (!NT_SUCCESS(Status
))
835 DPRINT1("IopCaptureUnicodeString() failed (Status 0x%08lx)\n", Status
);
839 /* Get the device object */
840 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
841 if (DeviceObject
== NULL
)
843 DPRINT1("IopGetDeviceObjectFromDeviceInstance() returned NULL\n");
844 Status
= STATUS_NO_SUCH_DEVICE
;
850 case PNP_EJECT_RELATIONS
:
851 Stack
.Parameters
.QueryDeviceRelations
.Type
= EjectionRelations
;
854 case PNP_REMOVAL_RELATIONS
:
855 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
858 case PNP_POWER_RELATIONS
:
859 Stack
.Parameters
.QueryDeviceRelations
.Type
= PowerRelations
;
862 case PNP_BUS_RELATIONS
:
863 Stack
.Parameters
.QueryDeviceRelations
.Type
= BusRelations
;
867 Status
= STATUS_INVALID_PARAMETER
;
871 Status
= IopInitiatePnpIrp(DeviceObject
,
873 IRP_MN_QUERY_DEVICE_RELATIONS
,
875 if (!NT_SUCCESS(Status
))
877 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
881 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
883 DPRINT("Found %d device relations\n", DeviceRelations
->Count
);
888 BufferLeft
= BufferSize
;
891 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
893 ChildDeviceObject
= DeviceRelations
->Objects
[i
];
895 ChildDeviceNode
= IopGetDeviceNode(ChildDeviceObject
);
898 DPRINT("Device instance: %wZ\n", &ChildDeviceNode
->InstancePath
);
899 DPRINT("RequiredSize: %hu\n", ChildDeviceNode
->InstancePath
.Length
+ sizeof(WCHAR
));
903 if (BufferLeft
< ChildDeviceNode
->InstancePath
.Length
+ 2 * sizeof(WCHAR
))
905 Status
= STATUS_BUFFER_TOO_SMALL
;
910 ChildDeviceNode
->InstancePath
.Buffer
,
911 ChildDeviceNode
->InstancePath
.Length
);
912 Ptr
= (PWCHAR
)((ULONG_PTR
)Ptr
+ ChildDeviceNode
->InstancePath
.Length
);
914 Ptr
= (PWCHAR
)((ULONG_PTR
)Ptr
+ sizeof(WCHAR
));
916 BufferLeft
-= (ChildDeviceNode
->InstancePath
.Length
+ sizeof(WCHAR
));
919 RequiredSize
+= (ChildDeviceNode
->InstancePath
.Length
+ sizeof(WCHAR
));
923 if (Ptr
!= NULL
&& BufferLeft
>= sizeof(WCHAR
))
926 if (RequiredSize
> 0)
927 RequiredSize
+= sizeof(WCHAR
);
929 DPRINT("BufferSize: %lu RequiredSize: %lu\n", RelationsData
->BufferSize
, RequiredSize
);
931 RelationsData
->BufferSize
= RequiredSize
;
933 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
935 Status
= _SEH2_GetExceptionCode();
940 if (DeviceRelations
!= NULL
)
941 ExFreePool(DeviceRelations
);
943 if (DeviceObject
!= NULL
)
944 ObDereferenceObject(DeviceObject
);
946 if (DeviceInstance
.Buffer
!= NULL
)
947 ExFreePool(DeviceInstance
.Buffer
);
953 IopGetDeviceDepth(PPLUGPLAY_CONTROL_DEPTH_DATA DepthData
)
955 PDEVICE_OBJECT DeviceObject
;
956 PDEVICE_NODE DeviceNode
;
957 UNICODE_STRING DeviceInstance
;
958 NTSTATUS Status
= STATUS_SUCCESS
;
960 DPRINT("IopGetDeviceDepth() called\n");
961 DPRINT("Device name: %wZ\n", &DepthData
->DeviceInstance
);
963 Status
= IopCaptureUnicodeString(&DeviceInstance
, &DepthData
->DeviceInstance
);
964 if (!NT_SUCCESS(Status
))
969 /* Get the device object */
970 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
971 if (DeviceInstance
.Buffer
!= NULL
)
973 ExFreePool(DeviceInstance
.Buffer
);
975 if (DeviceObject
== NULL
)
977 return STATUS_NO_SUCH_DEVICE
;
980 DeviceNode
= IopGetDeviceNode(DeviceObject
);
984 DepthData
->Depth
= DeviceNode
->Level
;
986 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
988 Status
= _SEH2_GetExceptionCode();
992 ObDereferenceObject(DeviceObject
);
999 IopResetDevice(PPLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData
)
1001 PDEVICE_OBJECT DeviceObject
;
1002 PDEVICE_NODE DeviceNode
;
1003 NTSTATUS Status
= STATUS_SUCCESS
;
1004 UNICODE_STRING DeviceInstance
;
1006 Status
= IopCaptureUnicodeString(&DeviceInstance
, &ResetDeviceData
->DeviceInstance
);
1007 if (!NT_SUCCESS(Status
))
1012 DPRINT("IopResetDevice(%wZ)\n", &DeviceInstance
);
1014 /* Get the device object */
1015 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
1016 if (DeviceInstance
.Buffer
!= NULL
)
1018 ExFreePool(DeviceInstance
.Buffer
);
1020 if (DeviceObject
== NULL
)
1022 return STATUS_NO_SUCH_DEVICE
;
1025 /* Get the device node */
1026 DeviceNode
= IopGetDeviceNode(DeviceObject
);
1028 ASSERT(DeviceNode
->Flags
& DNF_ENUMERATED
);
1029 ASSERT(DeviceNode
->Flags
& DNF_PROCESSED
);
1031 /* Check if there's already a driver loaded for this device */
1032 if (DeviceNode
->Flags
& DNF_ADDED
)
1035 /* Remove the device node */
1036 Status
= IopRemoveDevice(DeviceNode
);
1037 if (NT_SUCCESS(Status
))
1039 /* Invalidate device relations for the parent to reenumerate the device */
1040 DPRINT1("A new driver will be loaded for '%wZ' (FDO above removed)\n", &DeviceNode
->InstancePath
);
1041 Status
= IoSynchronousInvalidateDeviceRelations(DeviceNode
->Parent
->PhysicalDeviceObject
, BusRelations
);
1046 /* A driver has already been loaded for this device */
1047 DPRINT1("A reboot is required for the current driver for '%wZ' to be replaced\n", &DeviceNode
->InstancePath
);
1048 DeviceNode
->Problem
= CM_PROB_NEED_RESTART
;
1053 /* FIXME: What if the device really is disabled? */
1054 DeviceNode
->Flags
&= ~DNF_DISABLED
;
1055 DeviceNode
->Problem
= 0;
1057 /* Load service data from the registry */
1058 Status
= IopActionConfigureChildServices(DeviceNode
, DeviceNode
->Parent
);
1060 if (NT_SUCCESS(Status
))
1062 /* Start the service and begin PnP initialization of the device again */
1063 DPRINT1("A new driver will be loaded for '%wZ' (no FDO above)\n", &DeviceNode
->InstancePath
);
1064 Status
= IopActionInitChildServices(DeviceNode
, DeviceNode
->Parent
);
1068 ObDereferenceObject(DeviceObject
);
1073 /* PUBLIC FUNCTIONS **********************************************************/
1076 * Plug and Play event structure used by NtGetPlugPlayEvent.
1079 * Can be one of the following values:
1080 * GUID_HWPROFILE_QUERY_CHANGE
1081 * GUID_HWPROFILE_CHANGE_CANCELLED
1082 * GUID_HWPROFILE_CHANGE_COMPLETE
1083 * GUID_TARGET_DEVICE_QUERY_REMOVE
1084 * GUID_TARGET_DEVICE_REMOVE_CANCELLED
1085 * GUID_TARGET_DEVICE_REMOVE_COMPLETE
1086 * GUID_PNP_CUSTOM_NOTIFICATION
1087 * GUID_PNP_POWER_NOTIFICATION
1088 * GUID_DEVICE_* (see above)
1091 * Type of the event that happened.
1100 * Size of the event block including the device IDs and other
1101 * per category specific fields.
1105 * NtGetPlugPlayEvent
1107 * Returns one Plug & Play event from a global queue.
1112 * Always set to zero.
1115 * The buffer that will be filled with the event information on
1116 * successful return from the function.
1119 * Size of the buffer pointed by the Buffer parameter. If the
1120 * buffer size is not large enough to hold the whole event
1121 * information, error STATUS_BUFFER_TOO_SMALL is returned and
1122 * the buffer remains untouched.
1125 * STATUS_PRIVILEGE_NOT_HELD
1126 * STATUS_BUFFER_TOO_SMALL
1130 * This function isn't multi-thread safe!
1136 NtGetPlugPlayEvent(IN ULONG Reserved1
,
1138 OUT PPLUGPLAY_EVENT_BLOCK Buffer
,
1139 IN ULONG BufferSize
)
1141 PPNP_EVENT_ENTRY Entry
;
1144 DPRINT("NtGetPlugPlayEvent() called\n");
1146 /* Function can only be called from user-mode */
1147 if (KeGetPreviousMode() == KernelMode
)
1149 DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
1150 return STATUS_ACCESS_DENIED
;
1153 /* Check for Tcb privilege */
1154 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
,
1157 DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
1158 return STATUS_PRIVILEGE_NOT_HELD
;
1161 /* Wait for a PnP event */
1162 DPRINT("Waiting for pnp notification event\n");
1163 Status
= KeWaitForSingleObject(&IopPnpNotifyEvent
,
1168 if (!NT_SUCCESS(Status
) || Status
== STATUS_USER_APC
)
1170 DPRINT("KeWaitForSingleObject() failed (Status %lx)\n", Status
);
1171 ASSERT(Status
== STATUS_USER_APC
);
1175 /* Get entry from the tail of the queue */
1176 Entry
= CONTAINING_RECORD(IopPnpEventQueueHead
.Blink
,
1180 /* Check the buffer size */
1181 if (BufferSize
< Entry
->Event
.TotalSize
)
1183 DPRINT1("Buffer is too small for the pnp-event\n");
1184 return STATUS_BUFFER_TOO_SMALL
;
1187 /* Copy event data to the user buffer */
1190 ProbeForWrite(Buffer
,
1191 Entry
->Event
.TotalSize
,
1193 RtlCopyMemory(Buffer
,
1195 Entry
->Event
.TotalSize
);
1197 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1199 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1203 DPRINT("NtGetPlugPlayEvent() done\n");
1205 return STATUS_SUCCESS
;
1211 * A function for doing various Plug & Play operations from user mode.
1214 * PlugPlayControlClass
1215 * 0x00 Reenumerate device tree
1217 * Buffer points to UNICODE_STRING decribing the instance
1218 * path (like "HTREE\ROOT\0" or "Root\ACPI_HAL\0000"). For
1219 * more information about instance paths see !devnode command
1220 * in kernel debugger or look at "Inside Windows 2000" book,
1221 * chapter "Driver Loading, Initialization, and Installation".
1223 * 0x01 Register new device
1224 * 0x02 Deregister device
1225 * 0x03 Initialize device
1227 * 0x06 Query and remove device
1228 * 0x07 User response
1230 * Called after processing the message from NtGetPlugPlayEvent.
1232 * 0x08 Generate legacy device
1233 * 0x09 Get interface device list
1234 * 0x0A Get property data
1235 * 0x0B Device class association (Registration)
1236 * 0x0C Get related device
1237 * 0x0D Get device interface alias
1238 * 0x0E Get/set/clear device status
1239 * 0x0F Get device depth
1240 * 0x10 Query device relations
1241 * 0x11 Query target device relation
1242 * 0x12 Query conflict list
1243 * 0x13 Retrieve dock data
1246 * 0x16 Get blocked driver data
1249 * The buffer contains information that is specific to each control
1250 * code. The buffer is read-only.
1253 * Size of the buffer pointed by the Buffer parameter. If the
1254 * buffer size specifies incorrect value for specified control
1255 * code, error ??? is returned.
1258 * STATUS_PRIVILEGE_NOT_HELD
1266 NtPlugPlayControl(IN PLUGPLAY_CONTROL_CLASS PlugPlayControlClass
,
1267 IN OUT PVOID Buffer
,
1268 IN ULONG BufferLength
)
1270 DPRINT("NtPlugPlayControl(%d %p %lu) called\n",
1271 PlugPlayControlClass
, Buffer
, BufferLength
);
1273 /* Function can only be called from user-mode */
1274 if (KeGetPreviousMode() == KernelMode
)
1276 DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
1277 return STATUS_ACCESS_DENIED
;
1280 /* Check for Tcb privilege */
1281 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
,
1284 DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
1285 return STATUS_PRIVILEGE_NOT_HELD
;
1288 /* Probe the buffer */
1291 ProbeForWrite(Buffer
,
1295 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1297 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1301 switch (PlugPlayControlClass
)
1303 // case PlugPlayControlEnumerateDevice:
1304 // case PlugPlayControlRegisterNewDevice:
1305 // case PlugPlayControlDeregisterDevice:
1306 // case PlugPlayControlInitializeDevice:
1307 // case PlugPlayControlStartDevice:
1308 // case PlugPlayControlUnlockDevice:
1309 // case PlugPlayControlQueryAndRemoveDevice:
1311 case PlugPlayControlUserResponse
:
1312 if (Buffer
|| BufferLength
!= 0)
1313 return STATUS_INVALID_PARAMETER
;
1314 return IopRemovePlugPlayEvent();
1316 // case PlugPlayControlGenerateLegacyDevice:
1318 case PlugPlayControlGetInterfaceDeviceList
:
1319 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_INTERFACE_DEVICE_LIST_DATA
))
1320 return STATUS_INVALID_PARAMETER
;
1321 return IopGetInterfaceDeviceList((PPLUGPLAY_CONTROL_INTERFACE_DEVICE_LIST_DATA
)Buffer
);
1323 case PlugPlayControlProperty
:
1324 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_PROPERTY_DATA
))
1325 return STATUS_INVALID_PARAMETER
;
1326 return IopGetDeviceProperty((PPLUGPLAY_CONTROL_PROPERTY_DATA
)Buffer
);
1328 // case PlugPlayControlDeviceClassAssociation:
1330 case PlugPlayControlGetRelatedDevice
:
1331 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_RELATED_DEVICE_DATA
))
1332 return STATUS_INVALID_PARAMETER
;
1333 return IopGetRelatedDevice((PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA
)Buffer
);
1335 // case PlugPlayControlGetInterfaceDeviceAlias:
1337 case PlugPlayControlDeviceStatus
:
1338 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_STATUS_DATA
))
1339 return STATUS_INVALID_PARAMETER
;
1340 return IopDeviceStatus((PPLUGPLAY_CONTROL_STATUS_DATA
)Buffer
);
1342 case PlugPlayControlGetDeviceDepth
:
1343 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_DEPTH_DATA
))
1344 return STATUS_INVALID_PARAMETER
;
1345 return IopGetDeviceDepth((PPLUGPLAY_CONTROL_DEPTH_DATA
)Buffer
);
1347 case PlugPlayControlQueryDeviceRelations
:
1348 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_DEVICE_RELATIONS_DATA
))
1349 return STATUS_INVALID_PARAMETER
;
1350 return IopGetDeviceRelations((PPLUGPLAY_CONTROL_DEVICE_RELATIONS_DATA
)Buffer
);
1352 // case PlugPlayControlTargetDeviceRelation:
1353 // case PlugPlayControlQueryConflictList:
1354 // case PlugPlayControlRetrieveDock:
1356 case PlugPlayControlResetDevice
:
1357 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA
))
1358 return STATUS_INVALID_PARAMETER
;
1359 return IopResetDevice((PPLUGPLAY_CONTROL_RESET_DEVICE_DATA
)Buffer
);
1361 // case PlugPlayControlHaltDevice:
1362 // case PlugPlayControlGetBlockedDriverList:
1365 return STATUS_NOT_IMPLEMENTED
;
1368 return STATUS_NOT_IMPLEMENTED
;