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 ServiceName
= DriverObject
->DriverExtension
->ServiceKeyName
;
186 /* If the interface type is unknown, treat it as internal */
187 if (LegacyBusType
== InterfaceTypeUndefined
)
188 LegacyBusType
= Internal
;
190 /* Get the string equivalent of the interface type */
191 IfString
= IopGetInterfaceTypeString(LegacyBusType
);
193 /* If NULL is returned then it's a bad type */
195 return STATUS_INVALID_PARAMETER
;
197 /* We use the caller's PDO if they supplied one */
198 if (DeviceObject
&& *DeviceObject
)
205 Status
= PnpRootCreateDevice(&ServiceName
,
209 if (!NT_SUCCESS(Status
))
211 DPRINT("PnpRootCreateDevice() failed (Status 0x%08lx)\n", Status
);
216 /* Create the device node for the new PDO */
217 Status
= IopCreateDeviceNode(IopRootDeviceNode
,
222 if (!NT_SUCCESS(Status
))
224 DPRINT("IopCreateDeviceNode() failed (Status 0x%08lx)\n", Status
);
228 /* We're enumerated already */
229 IopDeviceNodeSetFlag(DeviceNode
, DNF_ENUMERATED
);
231 /* We don't call AddDevice for devices reported this way */
232 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
234 /* We don't send IRP_MN_START_DEVICE */
235 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
237 /* We need to get device IDs */
239 IopDeviceNodeSetFlag(DeviceNode
, DNF_NEED_QUERY_IDS
);
242 /* This is a legacy driver for this device */
243 IopDeviceNodeSetFlag(DeviceNode
, DNF_LEGACY_DRIVER
);
245 /* Perform a manual configuration of our device */
246 IopActionInterrogateDeviceStack(DeviceNode
, DeviceNode
->Parent
);
247 IopActionConfigureChildServices(DeviceNode
, DeviceNode
->Parent
);
249 /* Open a handle to the instance path key */
250 /* REG_OPTION_VOLATILE is a HACK!!! */
251 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceKey
);
252 if (!NT_SUCCESS(Status
))
255 /* Add DETECTEDInterfaceType\DriverName */
257 IdLength
+= swprintf(&HardwareId
[IdLength
],
263 /* Add DETECTED\DriverName */
264 IdLength
+= swprintf(&HardwareId
[IdLength
],
269 /* Terminate the string with another null */
270 HardwareId
[IdLength
++] = UNICODE_NULL
;
272 /* Store the value for CompatibleIDs */
273 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
274 Status
= ZwSetValueKey(InstanceKey
, &ValueName
, 0, REG_MULTI_SZ
, HardwareId
, IdLength
* sizeof(WCHAR
));
275 if (!NT_SUCCESS(Status
))
277 DPRINT("Failed to write the compatible IDs: 0x%x\n", Status
);
278 ZwClose(InstanceKey
);
282 /* Add a hardware ID if the driver didn't report one */
283 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
284 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
286 /* Just use our most specific compatible ID */
288 IdLength
+= swprintf(&HardwareId
[IdLength
],
294 HardwareId
[IdLength
++] = UNICODE_NULL
;
296 /* Write the value to the registry */
297 Status
= ZwSetValueKey(InstanceKey
, &ValueName
, 0, REG_MULTI_SZ
, HardwareId
, IdLength
* sizeof(WCHAR
));
298 if (!NT_SUCCESS(Status
))
300 DPRINT("Failed to write the hardware ID: 0x%x\n", Status
);
301 ZwClose(InstanceKey
);
306 /* Assign the resources to the device node */
307 DeviceNode
->BootResources
= ResourceList
;
308 DeviceNode
->ResourceRequirements
= ResourceRequirements
;
310 /* Set appropriate flags */
311 if (DeviceNode
->BootResources
)
312 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
314 if (!DeviceNode
->ResourceRequirements
&& !DeviceNode
->BootResources
)
315 IopDeviceNodeSetFlag(DeviceNode
, DNF_NO_RESOURCE_REQUIRED
);
317 /* Write the resource information to the registry */
318 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
320 /* If the caller didn't get the resources assigned for us, do it now */
321 if (!ResourceAssigned
)
323 Status
= IopAssignDeviceResources(DeviceNode
);
325 /* See if we failed */
326 if (!NT_SUCCESS(Status
))
328 DPRINT("Assigning resources failed: 0x%x\n", Status
);
329 ZwClose(InstanceKey
);
334 /* Close the instance key handle */
335 ZwClose(InstanceKey
);
337 /* Register the given DO with PnP root if required */
338 if (DeviceObject
&& *DeviceObject
)
339 PnpRootRegisterDevice(*DeviceObject
);
341 /* Report the device's enumeration to umpnpmgr */
342 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
343 &DeviceNode
->InstancePath
);
345 /* Report the device's arrival to umpnpmgr */
346 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
347 &DeviceNode
->InstancePath
);
349 DPRINT("Reported device: %S (%wZ)\n", HardwareId
, &DeviceNode
->InstancePath
);
352 if (DeviceObject
) *DeviceObject
= Pdo
;
354 return STATUS_SUCCESS
;
362 IoReportResourceForDetection(IN PDRIVER_OBJECT DriverObject
,
363 IN PCM_RESOURCE_LIST DriverList OPTIONAL
,
364 IN ULONG DriverListSize OPTIONAL
,
365 IN PDEVICE_OBJECT DeviceObject OPTIONAL
,
366 IN PCM_RESOURCE_LIST DeviceList OPTIONAL
,
367 IN ULONG DeviceListSize OPTIONAL
,
368 OUT PBOOLEAN ConflictDetected
)
370 PCM_RESOURCE_LIST ResourceList
;
373 *ConflictDetected
= FALSE
;
375 if (!DriverList
&& !DeviceList
)
376 return STATUS_INVALID_PARAMETER
;
378 /* Find the real list */
380 ResourceList
= DeviceList
;
382 ResourceList
= DriverList
;
384 /* Look for a resource conflict */
385 Status
= IopDetectResourceConflict(ResourceList
, FALSE
, NULL
);
386 if (Status
== STATUS_CONFLICTING_ADDRESSES
)
389 *ConflictDetected
= TRUE
;
391 else if (NT_SUCCESS(Status
))
393 /* Looks like we're good to go */
395 /* TODO: Claim the resources in the ResourceMap */
403 IopSetEvent(IN PVOID Context
)
405 PKEVENT Event
= Context
;
408 KeSetEvent(Event
, IO_NO_INCREMENT
, FALSE
);
416 IoReportTargetDeviceChange(IN PDEVICE_OBJECT PhysicalDeviceObject
,
417 IN PVOID NotificationStructure
)
420 NTSTATUS Status
, NotifyStatus
;
421 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct
= (PTARGET_DEVICE_CUSTOM_NOTIFICATION
)NotificationStructure
;
423 ASSERT(notifyStruct
);
425 /* Check for valid PDO */
426 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject
))
428 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
, 0x2, (ULONG
)PhysicalDeviceObject
, 0, 0);
431 /* FileObject must be null. PnP will fill in it */
432 ASSERT(notifyStruct
->FileObject
== NULL
);
434 /* Do not handle system PnP events */
435 if ((RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_QUERY_REMOVE
), sizeof(GUID
)) != sizeof(GUID
)) ||
436 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED
), sizeof(GUID
)) != sizeof(GUID
)) ||
437 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE
), sizeof(GUID
)) != sizeof(GUID
)))
439 return STATUS_INVALID_DEVICE_REQUEST
;
442 if (notifyStruct
->Version
!= 1)
444 return STATUS_INVALID_DEVICE_REQUEST
;
447 /* Initialize even that will let us know when PnP will have finished notify */
448 KeInitializeEvent(&NotifyEvent
, NotificationEvent
, FALSE
);
450 Status
= PpSetCustomTargetEvent(PhysicalDeviceObject
, &NotifyEvent
, &NotifyStatus
, NULL
, NULL
, notifyStruct
);
451 /* If no error, wait for the notify to end and return the status of the notify and not of the event */
452 if (NT_SUCCESS(Status
))
454 KeWaitForSingleObject(&NotifyEvent
, Executive
, KernelMode
, FALSE
, NULL
);
455 Status
= NotifyStatus
;
466 IoReportTargetDeviceChangeAsynchronous(IN PDEVICE_OBJECT PhysicalDeviceObject
,
467 IN PVOID NotificationStructure
,
468 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL
,
469 IN PVOID Context OPTIONAL
)
471 PINTERNAL_WORK_QUEUE_ITEM Item
= NULL
;
472 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct
= (PTARGET_DEVICE_CUSTOM_NOTIFICATION
)NotificationStructure
;
474 ASSERT(notifyStruct
);
476 /* Check for valid PDO */
477 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject
))
479 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
, 0x2, (ULONG
)PhysicalDeviceObject
, 0, 0);
482 /* FileObject must be null. PnP will fill in it */
483 ASSERT(notifyStruct
->FileObject
== NULL
);
485 /* Do not handle system PnP events */
486 if ((RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_QUERY_REMOVE
), sizeof(GUID
)) != sizeof(GUID
)) ||
487 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED
), sizeof(GUID
)) != sizeof(GUID
)) ||
488 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE
), sizeof(GUID
)) != sizeof(GUID
)))
490 return STATUS_INVALID_DEVICE_REQUEST
;
493 if (notifyStruct
->Version
!= 1)
495 return STATUS_INVALID_DEVICE_REQUEST
;
498 /* We need to store all the data given by the caller with the WorkItem, so use our own struct */
499 Item
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(INTERNAL_WORK_QUEUE_ITEM
), ' pP');
500 if (!Item
) return STATUS_INSUFFICIENT_RESOURCES
;
502 /* Initialize all stuff */
503 ObReferenceObject(PhysicalDeviceObject
);
504 Item
->NotificationStructure
= notifyStruct
;
505 Item
->PhysicalDeviceObject
= PhysicalDeviceObject
;
506 Item
->Callback
= Callback
;
507 Item
->Context
= Context
;
508 ExInitializeWorkItem(&(Item
->WorkItem
), IopReportTargetDeviceChangeAsyncWorker
, Item
);
510 /* Finally, queue the item, our work here is done */
511 ExQueueWorkItem(&(Item
->WorkItem
), DelayedWorkQueue
);
513 return STATUS_PENDING
;