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
))
81 InsertHeadList(&IopPnpEventQueueHead
,
82 &EventEntry
->ListEntry
);
83 KeSetEvent(&IopPnpNotifyEvent
,
87 return STATUS_SUCCESS
;
92 * Remove the current PnP event from the tail of the event queue
93 * and signal IopPnpNotifyEvent if there is yet another event in the queue.
96 IopRemovePlugPlayEvent(VOID
)
98 /* Remove a pnp event entry from the tail of the queue */
99 if (!IsListEmpty(&IopPnpEventQueueHead
))
101 ExFreePool(RemoveTailList(&IopPnpEventQueueHead
));
104 /* Signal the next pnp event in the queue */
105 if (!IsListEmpty(&IopPnpEventQueueHead
))
107 KeSetEvent(&IopPnpNotifyEvent
,
112 return STATUS_SUCCESS
;
115 static PDEVICE_OBJECT
116 IopTraverseDeviceNode(PDEVICE_NODE Node
, PUNICODE_STRING DeviceInstance
)
118 PDEVICE_OBJECT DeviceObject
;
119 PDEVICE_NODE ChildNode
;
121 if (RtlEqualUnicodeString(&Node
->InstancePath
,
122 DeviceInstance
, TRUE
))
124 ObReferenceObject(Node
->PhysicalDeviceObject
);
125 return Node
->PhysicalDeviceObject
;
128 /* Traversal of all children nodes */
129 for (ChildNode
= Node
->Child
;
131 ChildNode
= ChildNode
->Sibling
)
133 DeviceObject
= IopTraverseDeviceNode(ChildNode
, DeviceInstance
);
134 if (DeviceObject
!= NULL
)
144 static PDEVICE_OBJECT
145 IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance
)
147 if (IopRootDeviceNode
== NULL
)
150 if (DeviceInstance
== NULL
||
151 DeviceInstance
->Length
== 0
154 if (IopRootDeviceNode
->PhysicalDeviceObject
)
156 ObReferenceObject(IopRootDeviceNode
->PhysicalDeviceObject
);
157 return IopRootDeviceNode
->PhysicalDeviceObject
;
163 return IopTraverseDeviceNode(IopRootDeviceNode
, DeviceInstance
);
168 IopCaptureUnicodeString(PUNICODE_STRING DstName
, PUNICODE_STRING SrcName
)
170 NTSTATUS Status
= STATUS_SUCCESS
;
176 Name
.Length
= SrcName
->Length
;
177 Name
.MaximumLength
= SrcName
->MaximumLength
;
178 if (Name
.Length
> Name
.MaximumLength
)
180 Status
= STATUS_INVALID_PARAMETER
;
183 if (Name
.MaximumLength
)
185 ProbeForRead(SrcName
->Buffer
,
188 Name
.Buffer
= ExAllocatePool(NonPagedPool
, Name
.MaximumLength
);
189 if (Name
.Buffer
== NULL
)
191 Status
= STATUS_INSUFFICIENT_RESOURCES
;
194 memcpy(Name
.Buffer
, SrcName
->Buffer
, Name
.MaximumLength
);
198 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
200 Status
= _SEH2_GetExceptionCode();
204 if (!NT_SUCCESS(Status
) && Name
.Buffer
)
206 ExFreePool(Name
.Buffer
);
212 IopGetDeviceProperty(PPLUGPLAY_CONTROL_PROPERTY_DATA PropertyData
)
214 PDEVICE_OBJECT DeviceObject
= NULL
;
215 NTSTATUS Status
= STATUS_SUCCESS
;
216 UNICODE_STRING DeviceInstance
;
221 DPRINT("IopGetDeviceProperty() called\n");
222 DPRINT("Device name: %wZ\n", &PropertyData
->DeviceInstance
);
224 Status
= IopCaptureUnicodeString(&DeviceInstance
, &PropertyData
->DeviceInstance
);
225 if (!NT_SUCCESS(Status
))
232 Property
= PropertyData
->Property
;
233 BufferSize
= PropertyData
->BufferSize
;
234 ProbeForWrite(PropertyData
->Buffer
,
238 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
240 Status
= _SEH2_GetExceptionCode();
244 if (!NT_SUCCESS(Status
))
246 ExFreePool(DeviceInstance
.Buffer
);
250 /* Get the device object */
251 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
252 ExFreePool(DeviceInstance
.Buffer
);
253 if (DeviceObject
== NULL
)
255 return STATUS_NO_SUCH_DEVICE
;
258 Buffer
= ExAllocatePool(NonPagedPool
, BufferSize
);
261 return STATUS_INSUFFICIENT_RESOURCES
;
265 Status
= IoGetDeviceProperty(DeviceObject
,
271 ObDereferenceObject(DeviceObject
);
273 if (NT_SUCCESS(Status
))
277 memcpy(Buffer
, PropertyData
->Buffer
, BufferSize
);
278 PropertyData
->BufferSize
= BufferSize
;
280 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
282 Status
= _SEH2_GetExceptionCode();
292 IopGetRelatedDevice(PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA RelatedDeviceData
)
294 UNICODE_STRING RootDeviceName
;
295 PDEVICE_OBJECT DeviceObject
= NULL
;
296 PDEVICE_NODE DeviceNode
= NULL
;
297 PDEVICE_NODE RelatedDeviceNode
;
298 UNICODE_STRING TargetDeviceInstance
;
299 NTSTATUS Status
= STATUS_SUCCESS
;
301 ULONG MaximumLength
= 0;
303 DPRINT("IopGetRelatedDevice() called\n");
304 DPRINT("Device name: %wZ\n", &RelatedDeviceData
->TargetDeviceInstance
);
306 Status
= IopCaptureUnicodeString(&TargetDeviceInstance
, &RelatedDeviceData
->TargetDeviceInstance
);
307 if (!NT_SUCCESS(Status
))
314 Relation
= RelatedDeviceData
->Relation
;
315 MaximumLength
= RelatedDeviceData
->RelatedDeviceInstanceLength
;
316 ProbeForWrite(RelatedDeviceData
->RelatedDeviceInstance
,
320 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
322 Status
= _SEH2_GetExceptionCode();
326 if (!NT_SUCCESS(Status
))
328 ExFreePool(TargetDeviceInstance
.Buffer
);
332 RtlInitUnicodeString(&RootDeviceName
,
334 if (RtlEqualUnicodeString(&TargetDeviceInstance
,
338 DeviceNode
= IopRootDeviceNode
;
339 ExFreePool(TargetDeviceInstance
.Buffer
);
343 /* Get the device object */
344 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&TargetDeviceInstance
);
345 ExFreePool(TargetDeviceInstance
.Buffer
);
346 if (DeviceObject
== NULL
)
347 return STATUS_NO_SUCH_DEVICE
;
349 DeviceNode
= ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
354 case PNP_GET_PARENT_DEVICE
:
355 RelatedDeviceNode
= DeviceNode
->Parent
;
358 case PNP_GET_CHILD_DEVICE
:
359 RelatedDeviceNode
= DeviceNode
->Child
;
362 case PNP_GET_SIBLING_DEVICE
:
363 RelatedDeviceNode
= DeviceNode
->Sibling
;
367 if (DeviceObject
!= NULL
)
369 ObDereferenceObject(DeviceObject
);
372 return STATUS_INVALID_PARAMETER
;
375 if (RelatedDeviceNode
== NULL
)
379 ObDereferenceObject(DeviceObject
);
382 return STATUS_NO_SUCH_DEVICE
;
385 if (RelatedDeviceNode
->InstancePath
.Length
> MaximumLength
)
389 ObDereferenceObject(DeviceObject
);
392 return STATUS_BUFFER_TOO_SMALL
;
395 /* Copy related device instance name */
398 RtlCopyMemory(RelatedDeviceData
->RelatedDeviceInstance
,
399 RelatedDeviceNode
->InstancePath
.Buffer
,
400 RelatedDeviceNode
->InstancePath
.Length
);
401 RelatedDeviceData
->RelatedDeviceInstanceLength
= RelatedDeviceNode
->InstancePath
.Length
;
403 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
405 Status
= _SEH2_GetExceptionCode();
409 if (DeviceObject
!= NULL
)
411 ObDereferenceObject(DeviceObject
);
414 DPRINT("IopGetRelatedDevice() done\n");
421 IopDeviceStatus(PPLUGPLAY_CONTROL_STATUS_DATA StatusData
)
423 PDEVICE_OBJECT DeviceObject
;
424 PDEVICE_NODE DeviceNode
;
426 ULONG DeviceStatus
= 0;
427 ULONG DeviceProblem
= 0;
428 UNICODE_STRING DeviceInstance
;
429 NTSTATUS Status
= STATUS_SUCCESS
;
431 DPRINT("IopDeviceStatus() called\n");
433 Status
= IopCaptureUnicodeString(&DeviceInstance
, &StatusData
->DeviceInstance
);
434 if (!NT_SUCCESS(Status
))
436 DPRINT("Device name: '%wZ'\n", &DeviceInstance
);
440 Operation
= StatusData
->Operation
;
441 if (Operation
== PNP_SET_DEVICE_STATUS
)
443 DeviceStatus
= StatusData
->DeviceStatus
;
444 DeviceProblem
= StatusData
->DeviceProblem
;
447 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
449 Status
= _SEH2_GetExceptionCode();
453 if (!NT_SUCCESS(Status
))
455 if (DeviceInstance
.Buffer
)
456 ExFreePool(DeviceInstance
.Buffer
);
460 /* Get the device object */
461 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
462 ExFreePool(DeviceInstance
.Buffer
);
463 if (DeviceObject
== NULL
)
464 return STATUS_NO_SUCH_DEVICE
;
466 DeviceNode
= IopGetDeviceNode(DeviceObject
);
470 case PNP_GET_DEVICE_STATUS
:
471 DPRINT("Get status data\n");
472 DeviceStatus
= DeviceNode
->Flags
;
473 DeviceProblem
= DeviceNode
->Problem
;
476 case PNP_SET_DEVICE_STATUS
:
477 DPRINT("Set status data\n");
478 DeviceNode
->Flags
= DeviceStatus
;
479 DeviceNode
->Problem
= DeviceProblem
;
482 case PNP_CLEAR_DEVICE_STATUS
:
483 DPRINT1("FIXME: Clear status data!\n");
487 ObDereferenceObject(DeviceObject
);
489 if (Operation
== PNP_GET_DEVICE_STATUS
)
493 StatusData
->DeviceStatus
= DeviceStatus
;
494 StatusData
->DeviceProblem
= DeviceProblem
;
496 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
498 Status
= _SEH2_GetExceptionCode();
508 IopGetDeviceDepth(PPLUGPLAY_CONTROL_DEPTH_DATA DepthData
)
510 PDEVICE_OBJECT DeviceObject
;
511 PDEVICE_NODE DeviceNode
;
512 UNICODE_STRING DeviceInstance
;
513 NTSTATUS Status
= STATUS_SUCCESS
;
515 DPRINT("IopGetDeviceDepth() called\n");
516 DPRINT("Device name: %wZ\n", &DepthData
->DeviceInstance
);
518 Status
= IopCaptureUnicodeString(&DeviceInstance
, &DepthData
->DeviceInstance
);
519 if (!NT_SUCCESS(Status
))
524 /* Get the device object */
525 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
526 ExFreePool(DeviceInstance
.Buffer
);
527 if (DeviceObject
== NULL
)
528 return STATUS_NO_SUCH_DEVICE
;
530 DeviceNode
= IopGetDeviceNode(DeviceObject
);
534 DepthData
->Depth
= DeviceNode
->Level
;
536 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
538 Status
= _SEH2_GetExceptionCode();
542 ObDereferenceObject(DeviceObject
);
549 IopResetDevice(PPLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData
)
551 PDEVICE_OBJECT DeviceObject
;
552 PDEVICE_NODE DeviceNode
;
553 NTSTATUS Status
= STATUS_SUCCESS
;
554 UNICODE_STRING DeviceInstance
;
556 Status
= IopCaptureUnicodeString(&DeviceInstance
, &ResetDeviceData
->DeviceInstance
);
557 if (!NT_SUCCESS(Status
))
560 DPRINT("IopResetDevice(%wZ)\n", &DeviceInstance
);
562 /* Get the device object */
563 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
564 ExFreePool(DeviceInstance
.Buffer
);
565 if (DeviceObject
== NULL
)
566 return STATUS_NO_SUCH_DEVICE
;
568 DeviceNode
= IopGetDeviceNode(DeviceObject
);
570 /* FIXME: we should stop the device, before starting it again */
572 /* Start the device */
573 IopDeviceNodeClearFlag(DeviceNode
, DNF_DISABLED
);
574 Status
= IopActionConfigureChildServices(DeviceNode
, DeviceNode
->Parent
);
576 if (NT_SUCCESS(Status
))
577 Status
= IopActionInitChildServices(DeviceNode
, DeviceNode
->Parent
);
579 ObDereferenceObject(DeviceObject
);
584 /* PUBLIC FUNCTIONS **********************************************************/
587 * Plug and Play event structure used by NtGetPlugPlayEvent.
590 * Can be one of the following values:
591 * GUID_HWPROFILE_QUERY_CHANGE
592 * GUID_HWPROFILE_CHANGE_CANCELLED
593 * GUID_HWPROFILE_CHANGE_COMPLETE
594 * GUID_TARGET_DEVICE_QUERY_REMOVE
595 * GUID_TARGET_DEVICE_REMOVE_CANCELLED
596 * GUID_TARGET_DEVICE_REMOVE_COMPLETE
597 * GUID_PNP_CUSTOM_NOTIFICATION
598 * GUID_PNP_POWER_NOTIFICATION
599 * GUID_DEVICE_* (see above)
602 * Type of the event that happened.
611 * Size of the event block including the device IDs and other
612 * per category specific fields.
618 * Returns one Plug & Play event from a global queue.
623 * Always set to zero.
626 * The buffer that will be filled with the event information on
627 * successful return from the function.
630 * Size of the buffer pointed by the Buffer parameter. If the
631 * buffer size is not large enough to hold the whole event
632 * information, error STATUS_BUFFER_TOO_SMALL is returned and
633 * the buffer remains untouched.
636 * STATUS_PRIVILEGE_NOT_HELD
637 * STATUS_BUFFER_TOO_SMALL
641 * This function isn't multi-thread safe!
647 NtGetPlugPlayEvent(IN ULONG Reserved1
,
649 OUT PPLUGPLAY_EVENT_BLOCK Buffer
,
652 PPNP_EVENT_ENTRY Entry
;
655 DPRINT("NtGetPlugPlayEvent() called\n");
657 /* Function can only be called from user-mode */
658 if (KeGetPreviousMode() == KernelMode
)
660 DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
661 return STATUS_ACCESS_DENIED
;
664 /* Check for Tcb privilege */
665 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
,
668 DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
669 return STATUS_PRIVILEGE_NOT_HELD
;
672 /* Wait for a PnP event */
673 DPRINT("Waiting for pnp notification event\n");
674 Status
= KeWaitForSingleObject(&IopPnpNotifyEvent
,
679 if (!NT_SUCCESS(Status
))
681 DPRINT1("KeWaitForSingleObject() failed (Status %lx)\n", Status
);
685 /* Get entry from the tail of the queue */
686 Entry
= CONTAINING_RECORD(IopPnpEventQueueHead
.Blink
,
690 /* Check the buffer size */
691 if (BufferSize
< Entry
->Event
.TotalSize
)
693 DPRINT1("Buffer is too small for the pnp-event\n");
694 return STATUS_BUFFER_TOO_SMALL
;
697 /* Copy event data to the user buffer */
700 Entry
->Event
.TotalSize
);
702 DPRINT("NtGetPlugPlayEvent() done\n");
704 return STATUS_SUCCESS
;
710 * A function for doing various Plug & Play operations from user mode.
713 * PlugPlayControlClass
714 * 0x00 Reenumerate device tree
716 * Buffer points to UNICODE_STRING decribing the instance
717 * path (like "HTREE\ROOT\0" or "Root\ACPI_HAL\0000"). For
718 * more information about instance paths see !devnode command
719 * in kernel debugger or look at "Inside Windows 2000" book,
720 * chapter "Driver Loading, Initialization, and Installation".
722 * 0x01 Register new device
723 * 0x02 Deregister device
724 * 0x03 Initialize device
726 * 0x06 Query and remove device
729 * Called after processing the message from NtGetPlugPlayEvent.
731 * 0x08 Generate legacy device
732 * 0x09 Get interface device list
733 * 0x0A Get property data
734 * 0x0B Device class association (Registration)
735 * 0x0C Get related device
736 * 0x0D Get device interface alias
737 * 0x0E Get/set/clear device status
738 * 0x0F Get device depth
739 * 0x10 Query device relations
740 * 0x11 Query target device relation
741 * 0x12 Query conflict list
742 * 0x13 Retrieve dock data
745 * 0x16 Get blocked driver data
748 * The buffer contains information that is specific to each control
749 * code. The buffer is read-only.
752 * Size of the buffer pointed by the Buffer parameter. If the
753 * buffer size specifies incorrect value for specified control
754 * code, error ??? is returned.
757 * STATUS_PRIVILEGE_NOT_HELD
765 NtPlugPlayControl(IN PLUGPLAY_CONTROL_CLASS PlugPlayControlClass
,
767 IN ULONG BufferLength
)
769 NTSTATUS Status
= STATUS_SUCCESS
;
771 DPRINT("NtPlugPlayControl(%lu %p %lu) called\n",
772 PlugPlayControlClass
, Buffer
, BufferLength
);
774 /* Function can only be called from user-mode */
775 if (KeGetPreviousMode() == KernelMode
)
777 DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
778 return STATUS_ACCESS_DENIED
;
781 /* Check for Tcb privilege */
782 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
,
785 DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
786 return STATUS_PRIVILEGE_NOT_HELD
;
789 /* Probe the buffer */
792 ProbeForWrite(Buffer
,
796 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
798 Status
= _SEH2_GetExceptionCode();
802 if (!NT_SUCCESS(Status
))
807 switch (PlugPlayControlClass
)
809 case PlugPlayControlUserResponse
:
810 if (Buffer
|| BufferLength
!= 0)
811 return STATUS_INVALID_PARAMETER
;
812 return IopRemovePlugPlayEvent();
814 case PlugPlayControlProperty
:
815 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_PROPERTY_DATA
))
816 return STATUS_INVALID_PARAMETER
;
817 return IopGetDeviceProperty((PPLUGPLAY_CONTROL_PROPERTY_DATA
)Buffer
);
819 case PlugPlayControlGetRelatedDevice
:
820 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_RELATED_DEVICE_DATA
))
821 return STATUS_INVALID_PARAMETER
;
822 return IopGetRelatedDevice((PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA
)Buffer
);
824 case PlugPlayControlDeviceStatus
:
825 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_STATUS_DATA
))
826 return STATUS_INVALID_PARAMETER
;
827 return IopDeviceStatus((PPLUGPLAY_CONTROL_STATUS_DATA
)Buffer
);
829 case PlugPlayControlGetDeviceDepth
:
830 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_DEPTH_DATA
))
831 return STATUS_INVALID_PARAMETER
;
832 return IopGetDeviceDepth((PPLUGPLAY_CONTROL_DEPTH_DATA
)Buffer
);
834 case PlugPlayControlResetDevice
:
835 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA
))
836 return STATUS_INVALID_PARAMETER
;
837 return IopResetDevice((PPLUGPLAY_CONTROL_RESET_DEVICE_DATA
)Buffer
);
840 return STATUS_NOT_IMPLEMENTED
;
843 return STATUS_NOT_IMPLEMENTED
;