[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / io / pnpmgr / pnpreport.c
1 /*
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)
7 * Pierre Schweitzer
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 /* TYPES *******************************************************************/
17
18 typedef struct _INTERNAL_WORK_QUEUE_ITEM
19 {
20 WORK_QUEUE_ITEM WorkItem;
21 PDEVICE_OBJECT PhysicalDeviceObject;
22 PDEVICE_CHANGE_COMPLETE_CALLBACK Callback;
23 PVOID Context;
24 PTARGET_DEVICE_CUSTOM_NOTIFICATION NotificationStructure;
25 } INTERNAL_WORK_QUEUE_ITEM, *PINTERNAL_WORK_QUEUE_ITEM;
26
27 NTSTATUS
28 NTAPI
29 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath,
30 IN ULONG CreateOptions,
31 OUT PHANDLE Handle);
32
33 NTSTATUS
34 IopSetDeviceInstanceData(HANDLE InstanceKey,
35 PDEVICE_NODE DeviceNode);
36
37 NTSTATUS
38 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode,
39 PVOID Context);
40
41 NTSTATUS
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);
48
49 /* PRIVATE FUNCTIONS *********************************************************/
50
51 PWCHAR
52 IopGetInterfaceTypeString(INTERFACE_TYPE IfType)
53 {
54 switch (IfType)
55 {
56 case Internal:
57 return L"Internal";
58
59 case Isa:
60 return L"Isa";
61
62 case Eisa:
63 return L"Eisa";
64
65 case MicroChannel:
66 return L"MicroChannel";
67
68 case TurboChannel:
69 return L"TurboChannel";
70
71 case PCIBus:
72 return L"PCIBus";
73
74 case VMEBus:
75 return L"VMEBus";
76
77 case NuBus:
78 return L"NuBus";
79
80 case PCMCIABus:
81 return L"PCMCIABus";
82
83 case CBus:
84 return L"CBus";
85
86 case MPIBus:
87 return L"MPIBus";
88
89 case MPSABus:
90 return L"MPSABus";
91
92 case ProcessorInternal:
93 return L"ProcessorInternal";
94
95 case PNPISABus:
96 return L"PNPISABus";
97
98 case PNPBus:
99 return L"PNPBus";
100
101 case Vmcs:
102 return L"Vmcs";
103
104 default:
105 DPRINT1("Invalid bus type: %d\n", IfType);
106 return NULL;
107 }
108 }
109
110 VOID
111 IopReportTargetDeviceChangeAsyncWorker(PVOID Context)
112 {
113 PINTERNAL_WORK_QUEUE_ITEM Item;
114
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');
119 }
120
121 NTSTATUS
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)
128 {
129 ASSERT(NotificationStructure != NULL);
130 ASSERT(DeviceObject != NULL);
131
132 if (SyncEvent)
133 {
134 ASSERT(SyncStatus);
135 *SyncStatus = STATUS_PENDING;
136 }
137
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,
143 NULL);
144
145 if (SyncEvent)
146 {
147 KeSetEvent(SyncEvent, IO_NO_INCREMENT, FALSE);
148 *SyncStatus = STATUS_SUCCESS;
149 }
150
151 return STATUS_SUCCESS;
152 }
153
154 /* PUBLIC FUNCTIONS **********************************************************/
155
156 /*
157 * @implemented
158 */
159 NTSTATUS
160 NTAPI
161 IoReportDetectedDevice(IN PDRIVER_OBJECT DriverObject,
162 IN INTERFACE_TYPE LegacyBusType,
163 IN ULONG BusNumber,
164 IN ULONG SlotNumber,
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)
169 {
170 PDEVICE_NODE DeviceNode;
171 PDEVICE_OBJECT Pdo;
172 NTSTATUS Status;
173 HANDLE InstanceKey;
174 ULONG RequiredLength;
175 UNICODE_STRING ValueName, ServiceName;
176 WCHAR HardwareId[256];
177 PWCHAR IfString;
178 ULONG IdLength;
179
180 DPRINT("IoReportDetectedDevice (DeviceObject %p, *DeviceObject %p)\n",
181 DeviceObject, DeviceObject ? *DeviceObject : NULL);
182
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;
189
190 /* If the interface type is unknown, treat it as internal */
191 if (LegacyBusType == InterfaceTypeUndefined)
192 LegacyBusType = Internal;
193
194 /* Get the string equivalent of the interface type */
195 IfString = IopGetInterfaceTypeString(LegacyBusType);
196
197 /* If NULL is returned then it's a bad type */
198 if (!IfString)
199 return STATUS_INVALID_PARAMETER;
200
201 /* We use the caller's PDO if they supplied one */
202 if (DeviceObject && *DeviceObject)
203 {
204 Pdo = *DeviceObject;
205 DeviceNode = IopGetDeviceNode(*DeviceObject);
206 }
207 else
208 {
209 /* Create the PDO */
210 Status = PnpRootCreateDevice(&ServiceName,
211 &Pdo,
212 NULL);
213 if (!NT_SUCCESS(Status))
214 {
215 DPRINT("PnpRootCreateDevice() failed (Status 0x%08lx)\n", Status);
216 return Status;
217 }
218
219 /* Create the device node for the new PDO */
220 Status = IopCreateDeviceNode(IopRootDeviceNode,
221 Pdo,
222 NULL,
223 &DeviceNode);
224
225 if (!NT_SUCCESS(Status))
226 {
227 DPRINT("IopCreateDeviceNode() failed (Status 0x%08lx)\n", Status);
228 return Status;
229 }
230 }
231
232 /* We don't call AddDevice for devices reported this way */
233 IopDeviceNodeSetFlag(DeviceNode, DNF_ADDED);
234
235 /* We don't send IRP_MN_START_DEVICE */
236 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
237
238 /* We need to get device IDs */
239 #if 0
240 IopDeviceNodeSetFlag(DeviceNode, DNF_NEED_QUERY_IDS);
241 #endif
242
243 /* This is a legacy driver for this device */
244 IopDeviceNodeSetFlag(DeviceNode, DNF_LEGACY_DRIVER);
245
246 /* Perform a manual configuration of our device */
247 IopActionInterrogateDeviceStack(DeviceNode, DeviceNode->Parent);
248 IopActionConfigureChildServices(DeviceNode, DeviceNode->Parent);
249
250 /* Open a handle to the instance path key */
251 /* REG_OPTION_VOLATILE is a HACK!!! */
252 Status = IopCreateDeviceKeyPath(&DeviceNode->InstancePath, REG_OPTION_VOLATILE, &InstanceKey);
253 if (!NT_SUCCESS(Status))
254 return Status;
255
256 /* Add DETECTEDInterfaceType\DriverName */
257 IdLength = 0;
258 IdLength += swprintf(&HardwareId[IdLength],
259 L"DETECTED%ls\\%wZ",
260 IfString,
261 &ServiceName);
262 HardwareId[IdLength++] = UNICODE_NULL;
263
264 /* Add DETECTED\DriverName */
265 IdLength += swprintf(&HardwareId[IdLength],
266 L"DETECTED\\%wZ",
267 &ServiceName);
268 HardwareId[IdLength++] = UNICODE_NULL;
269
270 /* Terminate the string with another null */
271 HardwareId[IdLength] = UNICODE_NULL;
272
273 /* Store the value for CompatibleIDs */
274 RtlInitUnicodeString(&ValueName, L"CompatibleIDs");
275 Status = ZwSetValueKey(InstanceKey, &ValueName, 0, REG_MULTI_SZ, HardwareId, IdLength * sizeof(WCHAR));
276 if (!NT_SUCCESS(Status))
277 {
278 DPRINT("Failed to write the compatible IDs: 0x%x\n", Status);
279 ZwClose(InstanceKey);
280 return Status;
281 }
282
283 /* Add a hardware ID if the driver didn't report one */
284 RtlInitUnicodeString(&ValueName, L"HardwareID");
285 if (ZwQueryValueKey(InstanceKey, &ValueName, KeyValueBasicInformation, NULL, 0, &RequiredLength) == STATUS_OBJECT_NAME_NOT_FOUND)
286 {
287 /* Just use our most specific compatible ID */
288 IdLength = 0;
289 IdLength += swprintf(&HardwareId[IdLength],
290 L"DETECTED%ls\\%wZ",
291 IfString,
292 &ServiceName);
293 HardwareId[++IdLength] = UNICODE_NULL;
294
295 /* Write the value to the registry */
296 Status = ZwSetValueKey(InstanceKey, &ValueName, 0, REG_SZ, HardwareId, IdLength * sizeof(WCHAR));
297 if (!NT_SUCCESS(Status))
298 {
299 DPRINT("Failed to write the hardware ID: 0x%x\n", Status);
300 ZwClose(InstanceKey);
301 return Status;
302 }
303 }
304
305 /* Assign the resources to the device node */
306 DeviceNode->BootResources = ResourceList;
307 DeviceNode->ResourceRequirements = ResourceRequirements;
308
309 /* Set appropriate flags */
310 if (DeviceNode->BootResources)
311 IopDeviceNodeSetFlag(DeviceNode, DNF_HAS_BOOT_CONFIG);
312
313 if (!DeviceNode->ResourceRequirements && !DeviceNode->BootResources)
314 IopDeviceNodeSetFlag(DeviceNode, DNF_NO_RESOURCE_REQUIRED);
315
316 /* Write the resource information to the registry */
317 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
318
319 /* If the caller didn't get the resources assigned for us, do it now */
320 if (!ResourceAssigned)
321 {
322 Status = IopAssignDeviceResources(DeviceNode);
323
324 /* See if we failed */
325 if (!NT_SUCCESS(Status))
326 {
327 DPRINT("Assigning resources failed: 0x%x\n", Status);
328 ZwClose(InstanceKey);
329 return Status;
330 }
331 }
332
333 /* Close the instance key handle */
334 ZwClose(InstanceKey);
335
336 /* Report the device's enumeration to umpnpmgr */
337 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED,
338 &DeviceNode->InstancePath);
339
340 /* Report the device's arrival to umpnpmgr */
341 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
342 &DeviceNode->InstancePath);
343
344 DPRINT1("Reported device: %S (%wZ)\n", HardwareId, &DeviceNode->InstancePath);
345
346 /* Return the PDO */
347 if (DeviceObject) *DeviceObject = Pdo;
348
349 return STATUS_SUCCESS;
350 }
351
352 /*
353 * @halfplemented
354 */
355 NTSTATUS
356 NTAPI
357 IoReportResourceForDetection(IN PDRIVER_OBJECT DriverObject,
358 IN PCM_RESOURCE_LIST DriverList OPTIONAL,
359 IN ULONG DriverListSize OPTIONAL,
360 IN PDEVICE_OBJECT DeviceObject OPTIONAL,
361 IN PCM_RESOURCE_LIST DeviceList OPTIONAL,
362 IN ULONG DeviceListSize OPTIONAL,
363 OUT PBOOLEAN ConflictDetected)
364 {
365 PCM_RESOURCE_LIST ResourceList;
366 NTSTATUS Status;
367
368 *ConflictDetected = FALSE;
369
370 if (!DriverList && !DeviceList)
371 return STATUS_INVALID_PARAMETER;
372
373 /* Find the real list */
374 if (!DriverList)
375 ResourceList = DeviceList;
376 else
377 ResourceList = DriverList;
378
379 /* Look for a resource conflict */
380 Status = IopDetectResourceConflict(ResourceList, FALSE, NULL);
381 if (Status == STATUS_CONFLICTING_ADDRESSES)
382 {
383 /* Oh noes */
384 *ConflictDetected = TRUE;
385 }
386 else if (NT_SUCCESS(Status))
387 {
388 /* Looks like we're good to go */
389
390 /* TODO: Claim the resources in the ResourceMap */
391 }
392
393 return Status;
394 }
395
396 VOID
397 NTAPI
398 IopSetEvent(IN PVOID Context)
399 {
400 PKEVENT Event = Context;
401
402 /* Set the event */
403 KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
404 }
405
406 /*
407 * @implemented
408 */
409 NTSTATUS
410 NTAPI
411 IoReportTargetDeviceChange(IN PDEVICE_OBJECT PhysicalDeviceObject,
412 IN PVOID NotificationStructure)
413 {
414 KEVENT NotifyEvent;
415 NTSTATUS Status, NotifyStatus;
416 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct = (PTARGET_DEVICE_CUSTOM_NOTIFICATION)NotificationStructure;
417
418 ASSERT(notifyStruct);
419
420 /* Check for valid PDO */
421 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject))
422 {
423 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR, 0x2, (ULONG)PhysicalDeviceObject, 0, 0);
424 }
425
426 /* FileObject must be null. PnP will fill in it */
427 ASSERT(notifyStruct->FileObject == NULL);
428
429 /* Do not handle system PnP events */
430 if ((RtlCompareMemory(&(notifyStruct->Event), &(GUID_TARGET_DEVICE_QUERY_REMOVE), sizeof(GUID)) != sizeof(GUID)) ||
431 (RtlCompareMemory(&(notifyStruct->Event), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED), sizeof(GUID)) != sizeof(GUID)) ||
432 (RtlCompareMemory(&(notifyStruct->Event), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE), sizeof(GUID)) != sizeof(GUID)))
433 {
434 return STATUS_INVALID_DEVICE_REQUEST;
435 }
436
437 if (notifyStruct->Version != 1)
438 {
439 return STATUS_INVALID_DEVICE_REQUEST;
440 }
441
442 /* Initialize even that will let us know when PnP will have finished notify */
443 KeInitializeEvent(&NotifyEvent, NotificationEvent, FALSE);
444
445 Status = PpSetCustomTargetEvent(PhysicalDeviceObject, &NotifyEvent, &NotifyStatus, NULL, NULL, notifyStruct);
446 /* If no error, wait for the notify to end and return the status of the notify and not of the event */
447 if (NT_SUCCESS(Status))
448 {
449 KeWaitForSingleObject(&NotifyEvent, Executive, KernelMode, FALSE, NULL);
450 Status = NotifyStatus;
451 }
452
453 return Status;
454 }
455
456 /*
457 * @implemented
458 */
459 NTSTATUS
460 NTAPI
461 IoReportTargetDeviceChangeAsynchronous(IN PDEVICE_OBJECT PhysicalDeviceObject,
462 IN PVOID NotificationStructure,
463 IN PDEVICE_CHANGE_COMPLETE_CALLBACK Callback OPTIONAL,
464 IN PVOID Context OPTIONAL)
465 {
466 PINTERNAL_WORK_QUEUE_ITEM Item = NULL;
467 PTARGET_DEVICE_CUSTOM_NOTIFICATION notifyStruct = (PTARGET_DEVICE_CUSTOM_NOTIFICATION)NotificationStructure;
468
469 ASSERT(notifyStruct);
470
471 /* Check for valid PDO */
472 if (!IopIsValidPhysicalDeviceObject(PhysicalDeviceObject))
473 {
474 KeBugCheckEx(PNP_DETECTED_FATAL_ERROR, 0x2, (ULONG)PhysicalDeviceObject, 0, 0);
475 }
476
477 /* FileObject must be null. PnP will fill in it */
478 ASSERT(notifyStruct->FileObject == NULL);
479
480 /* Do not handle system PnP events */
481 if ((RtlCompareMemory(&(notifyStruct->Event), &(GUID_TARGET_DEVICE_QUERY_REMOVE), sizeof(GUID)) != sizeof(GUID)) ||
482 (RtlCompareMemory(&(notifyStruct->Event), &(GUID_TARGET_DEVICE_REMOVE_CANCELLED), sizeof(GUID)) != sizeof(GUID)) ||
483 (RtlCompareMemory(&(notifyStruct->Event), &(GUID_TARGET_DEVICE_REMOVE_COMPLETE), sizeof(GUID)) != sizeof(GUID)))
484 {
485 return STATUS_INVALID_DEVICE_REQUEST;
486 }
487
488 if (notifyStruct->Version != 1)
489 {
490 return STATUS_INVALID_DEVICE_REQUEST;
491 }
492
493 /* We need to store all the data given by the caller with the WorkItem, so use our own struct */
494 Item = ExAllocatePoolWithTag(NonPagedPool, sizeof(INTERNAL_WORK_QUEUE_ITEM), ' pP');
495 if (!Item) return STATUS_INSUFFICIENT_RESOURCES;
496
497 /* Initialize all stuff */
498 ObReferenceObject(PhysicalDeviceObject);
499 Item->NotificationStructure = notifyStruct;
500 Item->PhysicalDeviceObject = PhysicalDeviceObject;
501 Item->Callback = Callback;
502 Item->Context = Context;
503 ExInitializeWorkItem(&(Item->WorkItem), (PWORKER_THREAD_ROUTINE)IopReportTargetDeviceChangeAsyncWorker, Item);
504
505 /* Finally, queue the item, our work here is done */
506 ExQueueWorkItem(&(Item->WorkItem), DelayedWorkQueue);
507
508 return STATUS_PENDING;
509 }