8 struct acpi_device
*Device
,
9 PFDO_DEVICE_DATA FdoData
13 #pragma alloc_text (PAGE, Bus_PnP)
14 #pragma alloc_text (PAGE, Bus_PlugInDevice)
15 #pragma alloc_text (PAGE, Bus_InitializePdo)
16 #pragma alloc_text (PAGE, Bus_DestroyPdo)
17 #pragma alloc_text (PAGE, Bus_FDO_PnP)
18 #pragma alloc_text (PAGE, Bus_StartFdo)
19 #pragma alloc_text (PAGE, Bus_SendIrpSynchronously)
26 PDEVICE_OBJECT DeviceObject
,
30 PIO_STACK_LOCATION irpStack
;
32 PCOMMON_DEVICE_DATA commonData
;
36 irpStack
= IoGetCurrentIrpStackLocation (Irp
);
37 ASSERT (IRP_MJ_PNP
== irpStack
->MajorFunction
);
39 commonData
= (PCOMMON_DEVICE_DATA
) DeviceObject
->DeviceExtension
;
42 if (commonData
->IsFDO
) {
43 DPRINT("FDO %s IRP:0x%p\n",
44 PnPMinorFunctionString(irpStack
->MinorFunction
),
47 // Request is for the bus FDO
49 status
= Bus_FDO_PnP (
53 (PFDO_DEVICE_DATA
) commonData
);
55 DPRINT("PDO %s IRP: 0x%p\n",
56 PnPMinorFunctionString(irpStack
->MinorFunction
),
59 // Request is for the child PDO.
61 status
= Bus_PDO_PnP (
65 (PPDO_DEVICE_DATA
) commonData
);
73 PDEVICE_OBJECT DeviceObject
,
75 PIO_STACK_LOCATION IrpStack
,
76 PFDO_DEVICE_DATA DeviceData
80 ULONG length
, prevcount
, numPdosPresent
;
82 PPDO_DEVICE_DATA pdoData
;
83 PDEVICE_RELATIONS relations
, oldRelations
;
87 switch (IrpStack
->MinorFunction
) {
89 case IRP_MN_START_DEVICE
:
91 status
= Bus_StartFdo (DeviceData
, Irp
);
95 // We must now complete the IRP, since we stopped it in the
96 // completion routine with MORE_PROCESSING_REQUIRED.
99 Irp
->IoStatus
.Status
= status
;
100 IoCompleteRequest (Irp
, IO_NO_INCREMENT
);
104 case IRP_MN_QUERY_STOP_DEVICE
:
107 // The PnP manager is trying to stop the device
108 // for resource rebalancing.
110 SET_NEW_PNP_STATE(DeviceData
->Common
, StopPending
);
111 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
114 case IRP_MN_CANCEL_STOP_DEVICE
:
117 // The PnP Manager sends this IRP, at some point after an
118 // IRP_MN_QUERY_STOP_DEVICE, to inform the drivers for a
119 // device that the device will not be stopped for
120 // resource reconfiguration.
123 // First check to see whether you have received cancel-stop
124 // without first receiving a query-stop. This could happen if
125 // someone above us fails a query-stop and passes down the subsequent
129 if (StopPending
== DeviceData
->Common
.DevicePnPState
)
132 // We did receive a query-stop, so restore.
134 RESTORE_PREVIOUS_PNP_STATE(DeviceData
->Common
);
135 ASSERT(DeviceData
->Common
.DevicePnPState
== Started
);
137 Irp
->IoStatus
.Status
= STATUS_SUCCESS
; // We must not fail the IRP.
140 case IRP_MN_QUERY_DEVICE_RELATIONS
:
142 DPRINT("\tQueryDeviceRelation Type: %s\n",
143 DbgDeviceRelationString(\
144 IrpStack
->Parameters
.QueryDeviceRelations
.Type
));
146 if (BusRelations
!= IrpStack
->Parameters
.QueryDeviceRelations
.Type
) {
148 // We don't support any other Device Relations
154 ExAcquireFastMutex (&DeviceData
->Mutex
);
156 oldRelations
= (PDEVICE_RELATIONS
) Irp
->IoStatus
.Information
;
158 prevcount
= oldRelations
->Count
;
159 if (!DeviceData
->NumPDOs
) {
161 // There is a device relations struct already present and we have
162 // nothing to add to it, so just call IoSkip and IoCall
164 ExReleaseFastMutex (&DeviceData
->Mutex
);
173 // Calculate the number of PDOs actually present on the bus
176 for (entry
= DeviceData
->ListOfPDOs
.Flink
;
177 entry
!= &DeviceData
->ListOfPDOs
;
178 entry
= entry
->Flink
) {
179 pdoData
= CONTAINING_RECORD (entry
, PDO_DEVICE_DATA
, Link
);
184 // Need to allocate a new relations structure and add our
188 length
= sizeof(DEVICE_RELATIONS
) +
189 (((numPdosPresent
+ prevcount
) - 1) * sizeof (PDEVICE_OBJECT
));
191 relations
= (PDEVICE_RELATIONS
) ExAllocatePoolWithTag (PagedPool
,
194 if (NULL
== relations
) {
198 ExReleaseFastMutex (&DeviceData
->Mutex
);
199 Irp
->IoStatus
.Status
= status
= STATUS_INSUFFICIENT_RESOURCES
;
200 IoCompleteRequest (Irp
, IO_NO_INCREMENT
);
206 // Copy in the device objects so far
209 RtlCopyMemory (relations
->Objects
, oldRelations
->Objects
,
210 prevcount
* sizeof (PDEVICE_OBJECT
));
213 relations
->Count
= prevcount
+ numPdosPresent
;
216 // For each PDO present on this bus add a pointer to the device relations
217 // buffer, being sure to take out a reference to that object.
218 // The Plug & Play system will dereference the object when it is done
219 // with it and free the device relations buffer.
222 for (entry
= DeviceData
->ListOfPDOs
.Flink
;
223 entry
!= &DeviceData
->ListOfPDOs
;
224 entry
= entry
->Flink
) {
226 pdoData
= CONTAINING_RECORD (entry
, PDO_DEVICE_DATA
, Link
);
227 relations
->Objects
[prevcount
] = pdoData
->Common
.Self
;
228 ObReferenceObject (pdoData
->Common
.Self
);
232 DPRINT("\t#PDOs present = %d\n\t#PDOs reported = %d\n",
233 DeviceData
->NumPDOs
, relations
->Count
);
236 // Replace the relations structure in the IRP with the new
240 ExFreePool (oldRelations
);
242 Irp
->IoStatus
.Information
= (ULONG_PTR
) relations
;
244 ExReleaseFastMutex (&DeviceData
->Mutex
);
247 // Set up and pass the IRP further down the stack
249 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
255 // In the default case we merely call the next driver.
256 // We must not modify Irp->IoStatus.Status or complete the IRP.
262 IoSkipCurrentIrpStackLocation (Irp
);
263 status
= IoCallDriver (DeviceData
->NextLowerDriver
, Irp
);
264 return STATUS_SUCCESS
;
269 PFDO_DEVICE_DATA FdoData
,
272 NTSTATUS status
= STATUS_SUCCESS
;
273 POWER_STATE powerState
;
274 ACPI_STATUS AcpiStatus
;
278 FdoData
->Common
.DevicePowerState
= PowerDeviceD0
;
279 powerState
.DeviceState
= PowerDeviceD0
;
280 PoSetPowerState ( FdoData
->Common
.Self
, DevicePowerState
, powerState
);
282 SET_NEW_PNP_STATE(FdoData
->Common
, Started
);
284 AcpiStatus
= AcpiInitializeSubsystem();
285 if(ACPI_FAILURE(AcpiStatus
)){
286 DPRINT1("Unable to AcpiInitializeSubsystem\n");
287 return STATUS_UNSUCCESSFUL
;
291 AcpiStatus
= AcpiInitializeTables(NULL
, 16, 0);
292 if (ACPI_FAILURE(status
)){
293 DPRINT1("Unable to AcpiInitializeSubsystem\n");
294 return STATUS_UNSUCCESSFUL
;
297 AcpiStatus
= AcpiLoadTables();
298 if(ACPI_FAILURE(AcpiStatus
)){
299 DPRINT1("Unable to AcpiLoadTables\n");
301 return STATUS_UNSUCCESSFUL
;
304 DPRINT("Acpi subsystem init\n");
305 /* Initialize ACPI bus manager */
306 AcpiStatus
= acpi_init();
307 if (!ACPI_SUCCESS(AcpiStatus
)) {
308 DPRINT1("acpi_init() failed with status 0x%X\n", AcpiStatus
);
310 return STATUS_UNSUCCESSFUL
;
312 status
= ACPIEnumerateDevices(FdoData
);
318 Bus_SendIrpSynchronously (
319 PDEVICE_OBJECT DeviceObject
,
328 KeInitializeEvent(&event
, NotificationEvent
, FALSE
);
330 IoCopyCurrentIrpStackLocationToNext(Irp
);
332 IoSetCompletionRoutine(Irp
,
333 Bus_CompletionRoutine
,
340 status
= IoCallDriver(DeviceObject
, Irp
);
343 // Wait for lower drivers to be done with the Irp.
344 // Important thing to note here is when you allocate
345 // the memory for an event in the stack you must do a
346 // KernelMode wait instead of UserMode to prevent
347 // the stack from getting paged out.
350 if (status
== STATUS_PENDING
) {
351 KeWaitForSingleObject(&event
,
357 status
= Irp
->IoStatus
.Status
;
365 Bus_CompletionRoutine(
366 PDEVICE_OBJECT DeviceObject
,
371 UNREFERENCED_PARAMETER (DeviceObject
);
374 // If the lower driver didn't return STATUS_PENDING, we don't need to
375 // set the event because we won't be waiting on it.
376 // This optimization avoids grabbing the dispatcher lock and improves perf.
378 if (Irp
->PendingReturned
== TRUE
) {
380 KeSetEvent ((PKEVENT
) Context
, IO_NO_INCREMENT
, FALSE
);
382 return STATUS_MORE_PROCESSING_REQUIRED
; // Keep this IRP
387 PDEVICE_OBJECT Device
,
388 PPDO_DEVICE_DATA PdoData
394 // BusEnum does not queue any irps at this time so we have nothing to do.
398 // Free any resources.
401 if (PdoData
->HardwareIDs
) {
402 ExFreePoolWithTag (PdoData
->HardwareIDs
, 'IPCA');
403 PdoData
->HardwareIDs
= NULL
;
406 DPRINT("\tDeleting PDO: 0x%p\n", Device
);
407 IoDeleteDevice (Device
);
408 return STATUS_SUCCESS
;
415 PFDO_DEVICE_DATA FdoData
418 PPDO_DEVICE_DATA pdoData
;
420 DEVICE_POWER_STATE ntState
;
424 pdoData
= (PPDO_DEVICE_DATA
) Pdo
->DeviceExtension
;
426 DPRINT("pdo 0x%p, extension 0x%p\n", Pdo
, pdoData
);
428 if (pdoData
->AcpiHandle
)
429 acpi_bus_get_power(pdoData
->AcpiHandle
, &acpistate
);
431 acpistate
= ACPI_STATE_D0
;
436 ntState
= PowerDeviceD0
;
439 ntState
= PowerDeviceD1
;
442 ntState
= PowerDeviceD2
;
445 ntState
= PowerDeviceD3
;
448 DPRINT1("Unknown power state (%d) returned by acpi\n",acpistate
);
449 ntState
= PowerDeviceUnspecified
;
454 // Initialize the rest
456 pdoData
->Common
.IsFDO
= FALSE
;
457 pdoData
->Common
.Self
= Pdo
;
459 pdoData
->ParentFdo
= FdoData
->Common
.Self
;
462 INITIALIZE_PNP_STATE(pdoData
->Common
);
464 pdoData
->Common
.DevicePowerState
= ntState
;
465 pdoData
->Common
.SystemPowerState
= FdoData
->Common
.SystemPowerState
;
467 Pdo
->Flags
|= DO_POWER_PAGABLE
;
469 ExAcquireFastMutex (&FdoData
->Mutex
);
470 InsertTailList(&FdoData
->ListOfPDOs
, &pdoData
->Link
);
472 ExReleaseFastMutex (&FdoData
->Mutex
);
474 // This should be the last step in initialization.
475 Pdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
482 PnPMinorFunctionString (
486 switch (MinorFunction
)
488 case IRP_MN_START_DEVICE
:
489 return "IRP_MN_START_DEVICE";
490 case IRP_MN_QUERY_REMOVE_DEVICE
:
491 return "IRP_MN_QUERY_REMOVE_DEVICE";
492 case IRP_MN_REMOVE_DEVICE
:
493 return "IRP_MN_REMOVE_DEVICE";
494 case IRP_MN_CANCEL_REMOVE_DEVICE
:
495 return "IRP_MN_CANCEL_REMOVE_DEVICE";
496 case IRP_MN_STOP_DEVICE
:
497 return "IRP_MN_STOP_DEVICE";
498 case IRP_MN_QUERY_STOP_DEVICE
:
499 return "IRP_MN_QUERY_STOP_DEVICE";
500 case IRP_MN_CANCEL_STOP_DEVICE
:
501 return "IRP_MN_CANCEL_STOP_DEVICE";
502 case IRP_MN_QUERY_DEVICE_RELATIONS
:
503 return "IRP_MN_QUERY_DEVICE_RELATIONS";
504 case IRP_MN_QUERY_INTERFACE
:
505 return "IRP_MN_QUERY_INTERFACE";
506 case IRP_MN_QUERY_CAPABILITIES
:
507 return "IRP_MN_QUERY_CAPABILITIES";
508 case IRP_MN_QUERY_RESOURCES
:
509 return "IRP_MN_QUERY_RESOURCES";
510 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS
:
511 return "IRP_MN_QUERY_RESOURCE_REQUIREMENTS";
512 case IRP_MN_QUERY_DEVICE_TEXT
:
513 return "IRP_MN_QUERY_DEVICE_TEXT";
514 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
515 return "IRP_MN_FILTER_RESOURCE_REQUIREMENTS";
516 case IRP_MN_READ_CONFIG
:
517 return "IRP_MN_READ_CONFIG";
518 case IRP_MN_WRITE_CONFIG
:
519 return "IRP_MN_WRITE_CONFIG";
521 return "IRP_MN_EJECT";
522 case IRP_MN_SET_LOCK
:
523 return "IRP_MN_SET_LOCK";
524 case IRP_MN_QUERY_ID
:
525 return "IRP_MN_QUERY_ID";
526 case IRP_MN_QUERY_PNP_DEVICE_STATE
:
527 return "IRP_MN_QUERY_PNP_DEVICE_STATE";
528 case IRP_MN_QUERY_BUS_INFORMATION
:
529 return "IRP_MN_QUERY_BUS_INFORMATION";
530 case IRP_MN_DEVICE_USAGE_NOTIFICATION
:
531 return "IRP_MN_DEVICE_USAGE_NOTIFICATION";
532 case IRP_MN_SURPRISE_REMOVAL
:
533 return "IRP_MN_SURPRISE_REMOVAL";
534 case IRP_MN_QUERY_LEGACY_BUS_INFORMATION
:
535 return "IRP_MN_QUERY_LEGACY_BUS_INFORMATION";
537 return "unknown_pnp_irp";
542 DbgDeviceRelationString(
543 DEVICE_RELATION_TYPE Type
549 return "BusRelations";
550 case EjectionRelations
:
551 return "EjectionRelations";
552 case RemovalRelations
:
553 return "RemovalRelations";
554 case TargetDeviceRelation
:
555 return "TargetDeviceRelation";
557 return "UnKnown Relation";
563 BUS_QUERY_ID_TYPE Type
568 case BusQueryDeviceID
:
569 return "BusQueryDeviceID";
570 case BusQueryHardwareIDs
:
571 return "BusQueryHardwareIDs";
572 case BusQueryCompatibleIDs
:
573 return "BusQueryCompatibleIDs";
574 case BusQueryInstanceID
:
575 return "BusQueryInstanceID";
576 case BusQueryDeviceSerialNumber
:
577 return "BusQueryDeviceSerialNumber";