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
);
111 IopReportTargetDeviceChangeAsyncWorker(PVOID Context
)
113 PINTERNAL_WORK_QUEUE_ITEM Item
;
115 Item
= (PINTERNAL_WORK_QUEUE_ITEM
)Context
;
116 PpSetCustomTargetEvent(Item
->PhysicalDeviceObject
, NULL
, NULL
, Item
->Callback
, Item
->Context
, Item
->NotificationStructure
);
117 ObDereferenceObject(Item
->PhysicalDeviceObject
);
118 ExFreePoolWithTag(Context
, ' pP');
122 PpSetCustomTargetEvent(IN PDEVICE_OBJECT DeviceObject
,
123 IN OUT PKEVENT SyncEvent OPTIONAL
,
124 IN OUT PNTSTATUS SyncStatus OPTIONAL
,
125 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL
,
126 IN PVOID Context OPTIONAL
,
127 IN PTARGET_DEVICE_CUSTOM_NOTIFICATION NotificationStructure
)
129 ASSERT(NotificationStructure
!= NULL
);
130 ASSERT(DeviceObject
!= NULL
);
135 *SyncStatus
= STATUS_PENDING
;
138 /* That call is totally wrong but notifications handler must be fixed first */
139 IopNotifyPlugPlayNotification(DeviceObject
,
140 EventCategoryTargetDeviceChange
,
141 &GUID_PNP_CUSTOM_NOTIFICATION
,
142 NotificationStructure
,
147 KeSetEvent(SyncEvent
, IO_NO_INCREMENT
, FALSE
);
148 *SyncStatus
= STATUS_SUCCESS
;
151 return STATUS_SUCCESS
;
154 /* PUBLIC FUNCTIONS **********************************************************/
161 IoReportDetectedDevice(IN PDRIVER_OBJECT DriverObject
,
162 IN INTERFACE_TYPE LegacyBusType
,
165 IN PCM_RESOURCE_LIST ResourceList
,
166 IN PIO_RESOURCE_REQUIREMENTS_LIST ResourceRequirements OPTIONAL
,
167 IN BOOLEAN ResourceAssigned
,
168 IN OUT PDEVICE_OBJECT
*DeviceObject OPTIONAL
)
170 PDEVICE_NODE DeviceNode
;
174 ULONG RequiredLength
;
175 UNICODE_STRING ValueName
, ServiceName
;
176 WCHAR HardwareId
[256];
180 DPRINT("IoReportDetectedDevice (DeviceObject %p, *DeviceObject %p)\n",
181 DeviceObject
, DeviceObject
? *DeviceObject
: NULL
);
183 /* Create the service name (eg. ACPI_HAL) */
184 ServiceName
.Buffer
= DriverObject
->DriverName
.Buffer
+
185 sizeof(DRIVER_ROOT_NAME
) / sizeof(WCHAR
) - 1;
186 ServiceName
.Length
= DriverObject
->DriverName
.Length
-
187 sizeof(DRIVER_ROOT_NAME
) + sizeof(WCHAR
);
188 ServiceName
.MaximumLength
= ServiceName
.Length
;
190 /* If the interface type is unknown, treat it as internal */
191 if (LegacyBusType
== InterfaceTypeUndefined
)
192 LegacyBusType
= Internal
;
194 /* Get the string equivalent of the interface type */
195 IfString
= IopGetInterfaceTypeString(LegacyBusType
);
197 /* If NULL is returned then it's a bad type */
199 return STATUS_INVALID_PARAMETER
;
201 /* We use the caller's PDO if they supplied one */
202 if (DeviceObject
&& *DeviceObject
)
209 Status
= PnpRootCreateDevice(&ServiceName
,
213 if (!NT_SUCCESS(Status
))
215 DPRINT("PnpRootCreateDevice() failed (Status 0x%08lx)\n", Status
);
220 /* Create the device node for the new PDO */
221 Status
= IopCreateDeviceNode(IopRootDeviceNode
,
226 if (!NT_SUCCESS(Status
))
228 DPRINT("IopCreateDeviceNode() failed (Status 0x%08lx)\n", Status
);
232 /* We're enumerated already */
233 IopDeviceNodeSetFlag(DeviceNode
, DNF_ENUMERATED
);
235 /* We don't call AddDevice for devices reported this way */
236 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
238 /* We don't send IRP_MN_START_DEVICE */
239 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
241 /* We need to get device IDs */
243 IopDeviceNodeSetFlag(DeviceNode
, DNF_NEED_QUERY_IDS
);
246 /* This is a legacy driver for this device */
247 IopDeviceNodeSetFlag(DeviceNode
, DNF_LEGACY_DRIVER
);
249 /* Perform a manual configuration of our device */
250 IopActionInterrogateDeviceStack(DeviceNode
, DeviceNode
->Parent
);
251 IopActionConfigureChildServices(DeviceNode
, DeviceNode
->Parent
);
253 /* Open a handle to the instance path key */
254 /* REG_OPTION_VOLATILE is a HACK!!! */
255 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceKey
);
256 if (!NT_SUCCESS(Status
))
259 /* Add DETECTEDInterfaceType\DriverName */
261 IdLength
+= swprintf(&HardwareId
[IdLength
],
267 /* Add DETECTED\DriverName */
268 IdLength
+= swprintf(&HardwareId
[IdLength
],
273 /* Terminate the string with another null */
274 HardwareId
[IdLength
++] = UNICODE_NULL
;
276 /* Store the value for CompatibleIDs */
277 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
278 Status
= ZwSetValueKey(InstanceKey
, &ValueName
, 0, REG_MULTI_SZ
, HardwareId
, IdLength
* sizeof(WCHAR
));
279 if (!NT_SUCCESS(Status
))
281 DPRINT("Failed to write the compatible IDs: 0x%x\n", Status
);
282 ZwClose(InstanceKey
);
286 /* Add a hardware ID if the driver didn't report one */
287 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
288 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
290 /* Just use our most specific compatible ID */
292 IdLength
+= swprintf(&HardwareId
[IdLength
],
298 HardwareId
[IdLength
++] = UNICODE_NULL
;
300 /* Write the value to the registry */
301 Status
= ZwSetValueKey(InstanceKey
, &ValueName
, 0, REG_MULTI_SZ
, HardwareId
, IdLength
* sizeof(WCHAR
));
302 if (!NT_SUCCESS(Status
))
304 DPRINT("Failed to write the hardware ID: 0x%x\n", Status
);
305 ZwClose(InstanceKey
);
310 /* Assign the resources to the device node */
311 DeviceNode
->BootResources
= ResourceList
;
312 DeviceNode
->ResourceRequirements
= ResourceRequirements
;
314 /* Set appropriate flags */
315 if (DeviceNode
->BootResources
)
316 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
318 if (!DeviceNode
->ResourceRequirements
&& !DeviceNode
->BootResources
)
319 IopDeviceNodeSetFlag(DeviceNode
, DNF_NO_RESOURCE_REQUIRED
);
321 /* Write the resource information to the registry */
322 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
324 /* If the caller didn't get the resources assigned for us, do it now */
325 if (!ResourceAssigned
)
327 Status
= IopAssignDeviceResources(DeviceNode
);
329 /* See if we failed */
330 if (!NT_SUCCESS(Status
))
332 DPRINT("Assigning resources failed: 0x%x\n", Status
);
333 ZwClose(InstanceKey
);
338 /* Close the instance key handle */
339 ZwClose(InstanceKey
);
341 /* Register the given DO with PnP root if required */
342 if (DeviceObject
&& *DeviceObject
)
343 PnpRootRegisterDevice(*DeviceObject
);
345 /* Report the device's enumeration to umpnpmgr */
346 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
347 &DeviceNode
->InstancePath
);
349 /* Report the device's arrival to umpnpmgr */
350 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
351 &DeviceNode
->InstancePath
);
353 DPRINT1("Reported device: %S (%wZ)\n", HardwareId
, &DeviceNode
->InstancePath
);
356 if (DeviceObject
) *DeviceObject
= Pdo
;
358 return STATUS_SUCCESS
;
366 IoReportResourceForDetection(IN PDRIVER_OBJECT DriverObject
,
367 IN PCM_RESOURCE_LIST DriverList OPTIONAL
,
368 IN ULONG DriverListSize OPTIONAL
,
369 IN PDEVICE_OBJECT DeviceObject OPTIONAL
,
370 IN PCM_RESOURCE_LIST DeviceList OPTIONAL
,
371 IN ULONG DeviceListSize OPTIONAL
,
372 OUT PBOOLEAN ConflictDetected
)
374 PCM_RESOURCE_LIST ResourceList
;
377 *ConflictDetected
= FALSE
;
379 if (!DriverList
&& !DeviceList
)
380 return STATUS_INVALID_PARAMETER
;
382 /* Find the real list */
384 ResourceList
= DeviceList
;
386 ResourceList
= DriverList
;
388 /* Look for a resource conflict */
389 Status
= IopDetectResourceConflict(ResourceList
, FALSE
, NULL
);
390 if (Status
== STATUS_CONFLICTING_ADDRESSES
)
393 *ConflictDetected
= TRUE
;
395 else if (NT_SUCCESS(Status
))
397 /* Looks like we're good to go */
399 /* TODO: Claim the resources in the ResourceMap */
407 IopSetEvent(IN PVOID Context
)
409 PKEVENT Event
= Context
;
412 KeSetEvent(Event
, IO_NO_INCREMENT
, FALSE
);
420 IoReportTargetDeviceChange(IN PDEVICE_OBJECT PhysicalDeviceObject
,
421 IN PVOID NotificationStructure
)
424 NTSTATUS Status
, NotifyStatus
;
425 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct
= (PTARGET_DEVICE_CUSTOM_NOTIFICATION
)NotificationStructure
;
427 ASSERT(notifyStruct
);
429 /* Check for valid PDO */
430 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject
))
432 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
, 0x2, (ULONG
)PhysicalDeviceObject
, 0, 0);
435 /* FileObject must be null. PnP will fill in it */
436 ASSERT(notifyStruct
->FileObject
== NULL
);
438 /* Do not handle system PnP events */
439 if ((RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_QUERY_REMOVE
), sizeof(GUID
)) != sizeof(GUID
)) ||
440 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED
), sizeof(GUID
)) != sizeof(GUID
)) ||
441 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE
), sizeof(GUID
)) != sizeof(GUID
)))
443 return STATUS_INVALID_DEVICE_REQUEST
;
446 if (notifyStruct
->Version
!= 1)
448 return STATUS_INVALID_DEVICE_REQUEST
;
451 /* Initialize even that will let us know when PnP will have finished notify */
452 KeInitializeEvent(&NotifyEvent
, NotificationEvent
, FALSE
);
454 Status
= PpSetCustomTargetEvent(PhysicalDeviceObject
, &NotifyEvent
, &NotifyStatus
, NULL
, NULL
, notifyStruct
);
455 /* If no error, wait for the notify to end and return the status of the notify and not of the event */
456 if (NT_SUCCESS(Status
))
458 KeWaitForSingleObject(&NotifyEvent
, Executive
, KernelMode
, FALSE
, NULL
);
459 Status
= NotifyStatus
;
470 IoReportTargetDeviceChangeAsynchronous(IN PDEVICE_OBJECT PhysicalDeviceObject
,
471 IN PVOID NotificationStructure
,
472 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL
,
473 IN PVOID Context OPTIONAL
)
475 PINTERNAL_WORK_QUEUE_ITEM Item
= NULL
;
476 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct
= (PTARGET_DEVICE_CUSTOM_NOTIFICATION
)NotificationStructure
;
478 ASSERT(notifyStruct
);
480 /* Check for valid PDO */
481 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject
))
483 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR
, 0x2, (ULONG
)PhysicalDeviceObject
, 0, 0);
486 /* FileObject must be null. PnP will fill in it */
487 ASSERT(notifyStruct
->FileObject
== NULL
);
489 /* Do not handle system PnP events */
490 if ((RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_QUERY_REMOVE
), sizeof(GUID
)) != sizeof(GUID
)) ||
491 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED
), sizeof(GUID
)) != sizeof(GUID
)) ||
492 (RtlCompareMemory(&(notifyStruct
->Event
), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE
), sizeof(GUID
)) != sizeof(GUID
)))
494 return STATUS_INVALID_DEVICE_REQUEST
;
497 if (notifyStruct
->Version
!= 1)
499 return STATUS_INVALID_DEVICE_REQUEST
;
502 /* We need to store all the data given by the caller with the WorkItem, so use our own struct */
503 Item
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(INTERNAL_WORK_QUEUE_ITEM
), ' pP');
504 if (!Item
) return STATUS_INSUFFICIENT_RESOURCES
;
506 /* Initialize all stuff */
507 ObReferenceObject(PhysicalDeviceObject
);
508 Item
->NotificationStructure
= notifyStruct
;
509 Item
->PhysicalDeviceObject
= PhysicalDeviceObject
;
510 Item
->Callback
= Callback
;
511 Item
->Context
= Context
;
512 ExInitializeWorkItem(&(Item
->WorkItem
), (PWORKER_THREAD_ROUTINE
)IopReportTargetDeviceChangeAsyncWorker
, Item
);
514 /* Finally, queue the item, our work here is done */
515 ExQueueWorkItem(&(Item
->WorkItem
), DelayedWorkQueue
);
517 return STATUS_PENDING
;