2 * PROJECT: ReactOS Kernel
3 * COPYRIGHT: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/pnpmgr/pnpreport.c
5 * PURPOSE: Device Changes Reporting Functions
6 * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* TYPES *******************************************************************/
18 typedef struct _INTERNAL_WORK_QUEUE_ITEM
20 WORK_QUEUE_ITEM WorkItem
;
21 PDEVICE_OBJECT PhysicalDeviceObject
;
22 PDEVICE_CHANGE_COMPLETE_CALLBACK Callback
;
24 PTARGET_DEVICE_CUSTOM_NOTIFICATION NotificationStructure
;
25 } INTERNAL_WORK_QUEUE_ITEM
, *PINTERNAL_WORK_QUEUE_ITEM
;
29 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
30 IN ULONG CreateOptions
,
34 IopSetDeviceInstanceData(HANDLE InstanceKey
,
35 PDEVICE_NODE DeviceNode
);
38 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode
,
42 PpSetCustomTargetEvent(IN PDEVICE_OBJECT DeviceObject
,
43 IN OUT PKEVENT SyncEvent OPTIONAL
,
44 IN OUT PNTSTATUS SyncStatus OPTIONAL
,
45 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL
,
46 IN PVOID Context OPTIONAL
,
47 IN PTARGET_DEVICE_CUSTOM_NOTIFICATION NotificationStructure
);
49 /* PRIVATE FUNCTIONS *********************************************************/
52 IopGetInterfaceTypeString(INTERFACE_TYPE IfType
)
66 return L
"MicroChannel";
69 return L
"TurboChannel";
92 case ProcessorInternal
:
93 return L
"ProcessorInternal";
105 DPRINT1("Invalid bus type: %d\n", IfType
);
112 IopReportTargetDeviceChangeAsyncWorker(PVOID Context
)
114 PINTERNAL_WORK_QUEUE_ITEM Item
;
116 Item
= (PINTERNAL_WORK_QUEUE_ITEM
)Context
;
117 PpSetCustomTargetEvent(Item
->PhysicalDeviceObject
, NULL
, NULL
, Item
->Callback
, Item
->Context
, Item
->NotificationStructure
);
118 ObDereferenceObject(Item
->PhysicalDeviceObject
);
119 ExFreePoolWithTag(Context
, ' pP');
123 PpSetCustomTargetEvent(IN PDEVICE_OBJECT DeviceObject
,
124 IN OUT PKEVENT SyncEvent OPTIONAL
,
125 IN OUT PNTSTATUS SyncStatus OPTIONAL
,
126 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL
,
127 IN PVOID Context OPTIONAL
,
128 IN PTARGET_DEVICE_CUSTOM_NOTIFICATION NotificationStructure
)
130 ASSERT(NotificationStructure
!= NULL
);
131 ASSERT(DeviceObject
!= NULL
);
136 *SyncStatus
= STATUS_PENDING
;
139 /* That call is totally wrong but notifications handler must be fixed first */
140 IopNotifyPlugPlayNotification(DeviceObject
,
141 EventCategoryTargetDeviceChange
,
142 &GUID_PNP_CUSTOM_NOTIFICATION
,
143 NotificationStructure
,
148 KeSetEvent(SyncEvent
, IO_NO_INCREMENT
, FALSE
);
149 *SyncStatus
= STATUS_SUCCESS
;
152 return STATUS_SUCCESS
;
155 /* PUBLIC FUNCTIONS **********************************************************/
162 IoReportDetectedDevice(IN PDRIVER_OBJECT DriverObject
,
163 IN INTERFACE_TYPE LegacyBusType
,
166 IN PCM_RESOURCE_LIST ResourceList
,
167 IN PIO_RESOURCE_REQUIREMENTS_LIST ResourceRequirements OPTIONAL
,
168 IN BOOLEAN ResourceAssigned
,
169 IN OUT PDEVICE_OBJECT
*DeviceObject OPTIONAL
)
171 PDEVICE_NODE DeviceNode
;
175 ULONG RequiredLength
;
176 UNICODE_STRING ValueName
, ServiceName
;
177 WCHAR HardwareId
[256];
181 DPRINT("IoReportDetectedDevice (DeviceObject %p, *DeviceObject %p)\n",
182 DeviceObject
, DeviceObject
? *DeviceObject
: NULL
);
184 /* Create the service name (eg. ACPI_HAL) */
185 ServiceName
.Buffer
= DriverObject
->DriverName
.Buffer
+
186 sizeof(DRIVER_ROOT_NAME
) / sizeof(WCHAR
) - 1;
187 ServiceName
.Length
= DriverObject
->DriverName
.Length
-
188 sizeof(DRIVER_ROOT_NAME
) + sizeof(WCHAR
);
189 ServiceName
.MaximumLength
= ServiceName
.Length
;
191 /* If the interface type is unknown, treat it as internal */
192 if (LegacyBusType
== InterfaceTypeUndefined
)
193 LegacyBusType
= Internal
;
195 /* Get the string equivalent of the interface type */
196 IfString
= IopGetInterfaceTypeString(LegacyBusType
);
198 /* If NULL is returned then it's a bad type */
200 return STATUS_INVALID_PARAMETER
;
202 /* We use the caller's PDO if they supplied one */
203 if (DeviceObject
&& *DeviceObject
)
210 Status
= PnpRootCreateDevice(&ServiceName
,
214 if (!NT_SUCCESS(Status
))
216 DPRINT("PnpRootCreateDevice() failed (Status 0x%08lx)\n", Status
);
221 /* Create the device node for the new PDO */
222 Status
= IopCreateDeviceNode(IopRootDeviceNode
,
227 if (!NT_SUCCESS(Status
))
229 DPRINT("IopCreateDeviceNode() failed (Status 0x%08lx)\n", Status
);
233 /* We're enumerated already */
234 IopDeviceNodeSetFlag(DeviceNode
, DNF_ENUMERATED
);
236 /* We don't call AddDevice for devices reported this way */
237 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
239 /* We don't send IRP_MN_START_DEVICE */
240 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
242 /* We need to get device IDs */
244 IopDeviceNodeSetFlag(DeviceNode
, DNF_NEED_QUERY_IDS
);
247 /* This is a legacy driver for this device */
248 IopDeviceNodeSetFlag(DeviceNode
, DNF_LEGACY_DRIVER
);
250 /* Perform a manual configuration of our device */
251 IopActionInterrogateDeviceStack(DeviceNode
, DeviceNode
->Parent
);
252 IopActionConfigureChildServices(DeviceNode
, DeviceNode
->Parent
);
254 /* Open a handle to the instance path key */
255 /* REG_OPTION_VOLATILE is a HACK!!! */
256 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceKey
);
257 if (!NT_SUCCESS(Status
))
260 /* Add DETECTEDInterfaceType\DriverName */
262 IdLength
+= swprintf(&HardwareId
[IdLength
],
268 /* Add DETECTED\DriverName */
269 IdLength
+= swprintf(&HardwareId
[IdLength
],
274 /* Terminate the string with another null */
275 HardwareId
[IdLength
++] = UNICODE_NULL
;
277 /* Store the value for CompatibleIDs */
278 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
279 Status
= ZwSetValueKey(InstanceKey
, &ValueName
, 0, REG_MULTI_SZ
, HardwareId
, IdLength
* sizeof(WCHAR
));
280 if (!NT_SUCCESS(Status
))
282 DPRINT("Failed to write the compatible IDs: 0x%x\n", Status
);
283 ZwClose(InstanceKey
);
287 /* Add a hardware ID if the driver didn't report one */
288 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
289 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
291 /* Just use our most specific compatible ID */
293 IdLength
+= swprintf(&HardwareId
[IdLength
],
299 HardwareId
[IdLength
++] = UNICODE_NULL
;
301 /* Write the value to the registry */
302 Status
= ZwSetValueKey(InstanceKey
, &ValueName
, 0, REG_MULTI_SZ
, HardwareId
, IdLength
* sizeof(WCHAR
));
303 if (!NT_SUCCESS(Status
))
305 DPRINT("Failed to write the hardware ID: 0x%x\n", Status
);
306 ZwClose(InstanceKey
);
311 /* Assign the resources to the device node */
312 DeviceNode
->BootResources
= ResourceList
;
313 DeviceNode
->ResourceRequirements
= ResourceRequirements
;
315 /* Set appropriate flags */
316 if (DeviceNode
->BootResources
)
317 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
319 if (!DeviceNode
->ResourceRequirements
&& !DeviceNode
->BootResources
)
320 IopDeviceNodeSetFlag(DeviceNode
, DNF_NO_RESOURCE_REQUIRED
);
322 /* Write the resource information to the registry */
323 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
325 /* If the caller didn't get the resources assigned for us, do it now */
326 if (!ResourceAssigned
)
328 Status
= IopAssignDeviceResources(DeviceNode
);
330 /* See if we failed */
331 if (!NT_SUCCESS(Status
))
333 DPRINT("Assigning resources failed: 0x%x\n", Status
);
334 ZwClose(InstanceKey
);
339 /* Close the instance key handle */
340 ZwClose(InstanceKey
);
342 /* Register the given DO with PnP root if required */
343 if (DeviceObject
&& *DeviceObject
)
344 PnpRootRegisterDevice(*DeviceObject
);
346 /* Report the device's enumeration to umpnpmgr */
347 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
348 &DeviceNode
->InstancePath
);
350 /* Report the device's arrival to umpnpmgr */
351 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
352 &DeviceNode
->InstancePath
);
354 DPRINT("Reported device: %S (%wZ)\n", HardwareId
, &DeviceNode
->InstancePath
);
357 if (DeviceObject
) *DeviceObject
= Pdo
;
359 return STATUS_SUCCESS
;
367 IoReportResourceForDetection(IN PDRIVER_OBJECT DriverObject
,
368 IN PCM_RESOURCE_LIST DriverList OPTIONAL
,
369 IN ULONG DriverListSize OPTIONAL
,
370 IN PDEVICE_OBJECT DeviceObject OPTIONAL
,
371 IN PCM_RESOURCE_LIST DeviceList OPTIONAL
,
372 IN ULONG DeviceListSize OPTIONAL
,
373 OUT PBOOLEAN ConflictDetected
)
375 PCM_RESOURCE_LIST ResourceList
;
378 *ConflictDetected
= FALSE
;
380 if (!DriverList
&& !DeviceList
)
381 return STATUS_INVALID_PARAMETER
;
383 /* Find the real list */
385 ResourceList
= DeviceList
;
387 ResourceList
= DriverList
;
389 /* Look for a resource conflict */
390 Status
= IopDetectResourceConflict(ResourceList
, FALSE
, NULL
);
391 if (Status
== STATUS_CONFLICTING_ADDRESSES
)
394 *ConflictDetected
= TRUE
;
396 else if (NT_SUCCESS(Status
))
398 /* Looks like we're good to go */
400 /* TODO: Claim the resources in the ResourceMap */
408 IopSetEvent(IN PVOID Context
)
410 PKEVENT Event
= Context
;
413 KeSetEvent(Event
, IO_NO_INCREMENT
, FALSE
);
421 IoReportTargetDeviceChange(IN PDEVICE_OBJECT PhysicalDeviceObject
,
422 IN PVOID NotificationStructure
)
425 NTSTATUS Status
, NotifyStatus
;
426 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct
= (PTARGET_DEVICE_CUSTOM_NOTIFICATION
)NotificationStructure
;
428 ASSERT(notifyStruct
);
430 /* Check for valid PDO */
431 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject
))
433 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
, 0x2, (ULONG
)PhysicalDeviceObject
, 0, 0);
436 /* FileObject must be null. PnP will fill in it */
437 ASSERT(notifyStruct
->FileObject
== NULL
);
439 /* Do not handle system PnP events */
440 if ((RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_QUERY_REMOVE
), sizeof(GUID
)) != sizeof(GUID
)) ||
441 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED
), sizeof(GUID
)) != sizeof(GUID
)) ||
442 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE
), sizeof(GUID
)) != sizeof(GUID
)))
444 return STATUS_INVALID_DEVICE_REQUEST
;
447 if (notifyStruct
->Version
!= 1)
449 return STATUS_INVALID_DEVICE_REQUEST
;
452 /* Initialize even that will let us know when PnP will have finished notify */
453 KeInitializeEvent(&NotifyEvent
, NotificationEvent
, FALSE
);
455 Status
= PpSetCustomTargetEvent(PhysicalDeviceObject
, &NotifyEvent
, &NotifyStatus
, NULL
, NULL
, notifyStruct
);
456 /* If no error, wait for the notify to end and return the status of the notify and not of the event */
457 if (NT_SUCCESS(Status
))
459 KeWaitForSingleObject(&NotifyEvent
, Executive
, KernelMode
, FALSE
, NULL
);
460 Status
= NotifyStatus
;
471 IoReportTargetDeviceChangeAsynchronous(IN PDEVICE_OBJECT PhysicalDeviceObject
,
472 IN PVOID NotificationStructure
,
473 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL
,
474 IN PVOID Context OPTIONAL
)
476 PINTERNAL_WORK_QUEUE_ITEM Item
= NULL
;
477 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct
= (PTARGET_DEVICE_CUSTOM_NOTIFICATION
)NotificationStructure
;
479 ASSERT(notifyStruct
);
481 /* Check for valid PDO */
482 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject
))
484 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
, 0x2, (ULONG
)PhysicalDeviceObject
, 0, 0);
487 /* FileObject must be null. PnP will fill in it */
488 ASSERT(notifyStruct
->FileObject
== NULL
);
490 /* Do not handle system PnP events */
491 if ((RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_QUERY_REMOVE
), sizeof(GUID
)) != sizeof(GUID
)) ||
492 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED
), sizeof(GUID
)) != sizeof(GUID
)) ||
493 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE
), sizeof(GUID
)) != sizeof(GUID
)))
495 return STATUS_INVALID_DEVICE_REQUEST
;
498 if (notifyStruct
->Version
!= 1)
500 return STATUS_INVALID_DEVICE_REQUEST
;
503 /* We need to store all the data given by the caller with the WorkItem, so use our own struct */
504 Item
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(INTERNAL_WORK_QUEUE_ITEM
), ' pP');
505 if (!Item
) return STATUS_INSUFFICIENT_RESOURCES
;
507 /* Initialize all stuff */
508 ObReferenceObject(PhysicalDeviceObject
);
509 Item
->NotificationStructure
= notifyStruct
;
510 Item
->PhysicalDeviceObject
= PhysicalDeviceObject
;
511 Item
->Callback
= Callback
;
512 Item
->Context
= Context
;
513 ExInitializeWorkItem(&(Item
->WorkItem
), IopReportTargetDeviceChangeAsyncWorker
, Item
);
515 /* Finally, queue the item, our work here is done */
516 ExQueueWorkItem(&(Item
->WorkItem
), DelayedWorkQueue
);
518 return STATUS_PENDING
;