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
);
200 Status
= _SEH_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
,
240 Status
= _SEH_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
;
282 Status
= _SEH_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
,
322 Status
= _SEH_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
;
405 Status
= _SEH_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
;
449 Status
= _SEH_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
;
498 Status
= _SEH_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
);
532 DepthData
->Depth
= DeviceNode
->Level
;
534 ObDereferenceObject(DeviceObject
);
538 DepthData
->Depth
= DeviceNode
->Level
;
542 Status
= _SEH_GetExceptionCode();
551 IopResetDevice(PPLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData
)
553 PDEVICE_OBJECT DeviceObject
;
554 PDEVICE_NODE DeviceNode
;
555 NTSTATUS Status
= STATUS_SUCCESS
;
556 UNICODE_STRING DeviceInstance
;
558 Status
= IopCaptureUnicodeString(&DeviceInstance
, &ResetDeviceData
->DeviceInstance
);
559 if (!NT_SUCCESS(Status
))
562 DPRINT("IopResetDevice(%wZ)\n", &DeviceInstance
);
564 /* Get the device object */
565 DeviceObject
= IopGetDeviceObjectFromDeviceInstance(&DeviceInstance
);
566 ExFreePool(DeviceInstance
.Buffer
);
567 if (DeviceObject
== NULL
)
568 return STATUS_NO_SUCH_DEVICE
;
570 DeviceNode
= IopGetDeviceNode(DeviceObject
);
572 /* FIXME: we should stop the device, before starting it again */
574 /* Start the device */
575 IopDeviceNodeClearFlag(DeviceNode
, DNF_DISABLED
);
576 Status
= IopActionConfigureChildServices(DeviceNode
, DeviceNode
->Parent
);
578 if (NT_SUCCESS(Status
))
579 Status
= IopActionInitChildServices(DeviceNode
, DeviceNode
->Parent
);
581 ObDereferenceObject(DeviceObject
);
586 /* PUBLIC FUNCTIONS **********************************************************/
589 * Plug and Play event structure used by NtGetPlugPlayEvent.
592 * Can be one of the following values:
593 * GUID_HWPROFILE_QUERY_CHANGE
594 * GUID_HWPROFILE_CHANGE_CANCELLED
595 * GUID_HWPROFILE_CHANGE_COMPLETE
596 * GUID_TARGET_DEVICE_QUERY_REMOVE
597 * GUID_TARGET_DEVICE_REMOVE_CANCELLED
598 * GUID_TARGET_DEVICE_REMOVE_COMPLETE
599 * GUID_PNP_CUSTOM_NOTIFICATION
600 * GUID_PNP_POWER_NOTIFICATION
601 * GUID_DEVICE_* (see above)
604 * Type of the event that happened.
613 * Size of the event block including the device IDs and other
614 * per category specific fields.
620 * Returns one Plug & Play event from a global queue.
625 * Always set to zero.
628 * The buffer that will be filled with the event information on
629 * successful return from the function.
632 * Size of the buffer pointed by the Buffer parameter. If the
633 * buffer size is not large enough to hold the whole event
634 * information, error STATUS_BUFFER_TOO_SMALL is returned and
635 * the buffer remains untouched.
638 * STATUS_PRIVILEGE_NOT_HELD
639 * STATUS_BUFFER_TOO_SMALL
643 * This function isn't multi-thread safe!
649 NtGetPlugPlayEvent(IN ULONG Reserved1
,
651 OUT PPLUGPLAY_EVENT_BLOCK Buffer
,
654 PPNP_EVENT_ENTRY Entry
;
657 DPRINT("NtGetPlugPlayEvent() called\n");
659 /* Function can only be called from user-mode */
660 if (KeGetPreviousMode() == KernelMode
)
662 DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
663 return STATUS_ACCESS_DENIED
;
666 /* Check for Tcb privilege */
667 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
,
670 DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
671 return STATUS_PRIVILEGE_NOT_HELD
;
674 /* Wait for a PnP event */
675 DPRINT("Waiting for pnp notification event\n");
676 Status
= KeWaitForSingleObject(&IopPnpNotifyEvent
,
681 if (!NT_SUCCESS(Status
))
683 DPRINT1("KeWaitForSingleObject() failed (Status %lx)\n", Status
);
687 /* Get entry from the tail of the queue */
688 Entry
= CONTAINING_RECORD(IopPnpEventQueueHead
.Blink
,
692 /* Check the buffer size */
693 if (BufferSize
< Entry
->Event
.TotalSize
)
695 DPRINT1("Buffer is too small for the pnp-event\n");
696 return STATUS_BUFFER_TOO_SMALL
;
699 /* Copy event data to the user buffer */
702 Entry
->Event
.TotalSize
);
704 DPRINT("NtGetPlugPlayEvent() done\n");
706 return STATUS_SUCCESS
;
712 * A function for doing various Plug & Play operations from user mode.
715 * PlugPlayControlClass
716 * 0x00 Reenumerate device tree
718 * Buffer points to UNICODE_STRING decribing the instance
719 * path (like "HTREE\ROOT\0" or "Root\ACPI_HAL\0000"). For
720 * more information about instance paths see !devnode command
721 * in kernel debugger or look at "Inside Windows 2000" book,
722 * chapter "Driver Loading, Initialization, and Installation".
724 * 0x01 Register new device
725 * 0x02 Deregister device
726 * 0x03 Initialize device
728 * 0x06 Query and remove device
731 * Called after processing the message from NtGetPlugPlayEvent.
733 * 0x08 Generate legacy device
734 * 0x09 Get interface device list
735 * 0x0A Get property data
736 * 0x0B Device class association (Registration)
737 * 0x0C Get related device
738 * 0x0D Get device interface alias
739 * 0x0E Get/set/clear device status
740 * 0x0F Get device depth
741 * 0x10 Query device relations
742 * 0x11 Query target device relation
743 * 0x12 Query conflict list
744 * 0x13 Retrieve dock data
747 * 0x16 Get blocked driver data
750 * The buffer contains information that is specific to each control
751 * code. The buffer is read-only.
754 * Size of the buffer pointed by the Buffer parameter. If the
755 * buffer size specifies incorrect value for specified control
756 * code, error ??? is returned.
759 * STATUS_PRIVILEGE_NOT_HELD
767 NtPlugPlayControl(IN PLUGPLAY_CONTROL_CLASS PlugPlayControlClass
,
769 IN ULONG BufferLength
)
771 NTSTATUS Status
= STATUS_SUCCESS
;
773 DPRINT("NtPlugPlayControl(%lu %p %lu) called\n",
774 PlugPlayControlClass
, Buffer
, BufferLength
);
776 /* Function can only be called from user-mode */
777 if (KeGetPreviousMode() == KernelMode
)
779 DPRINT1("NtGetPlugPlayEvent cannot be called from kernel mode!\n");
780 return STATUS_ACCESS_DENIED
;
783 /* Check for Tcb privilege */
784 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
,
787 DPRINT1("NtGetPlugPlayEvent: Caller does not hold the SeTcbPrivilege privilege!\n");
788 return STATUS_PRIVILEGE_NOT_HELD
;
791 /* Probe the buffer */
794 ProbeForWrite(Buffer
,
800 Status
= _SEH_GetExceptionCode();
804 if (!NT_SUCCESS(Status
))
809 switch (PlugPlayControlClass
)
811 case PlugPlayControlUserResponse
:
812 if (Buffer
|| BufferLength
!= 0)
813 return STATUS_INVALID_PARAMETER
;
814 return IopRemovePlugPlayEvent();
816 case PlugPlayControlProperty
:
817 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_PROPERTY_DATA
))
818 return STATUS_INVALID_PARAMETER
;
819 return IopGetDeviceProperty((PPLUGPLAY_CONTROL_PROPERTY_DATA
)Buffer
);
821 case PlugPlayControlGetRelatedDevice
:
822 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_RELATED_DEVICE_DATA
))
823 return STATUS_INVALID_PARAMETER
;
824 return IopGetRelatedDevice((PPLUGPLAY_CONTROL_RELATED_DEVICE_DATA
)Buffer
);
826 case PlugPlayControlDeviceStatus
:
827 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_STATUS_DATA
))
828 return STATUS_INVALID_PARAMETER
;
829 return IopDeviceStatus((PPLUGPLAY_CONTROL_STATUS_DATA
)Buffer
);
831 case PlugPlayControlGetDeviceDepth
:
832 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_DEPTH_DATA
))
833 return STATUS_INVALID_PARAMETER
;
834 return IopGetDeviceDepth((PPLUGPLAY_CONTROL_DEPTH_DATA
)Buffer
);
836 case PlugPlayControlResetDevice
:
837 if (!Buffer
|| BufferLength
< sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA
))
838 return STATUS_INVALID_PARAMETER
;
839 return IopResetDevice((PPLUGPLAY_CONTROL_RESET_DEVICE_DATA
)Buffer
);
842 return STATUS_NOT_IMPLEMENTED
;
845 return STATUS_NOT_IMPLEMENTED
;