7 #include <acpi_drivers.h>
15 #pragma alloc_text (INIT, DriverEntry)
16 #pragma alloc_text (PAGE, Bus_AddDevice)
25 PDRIVER_OBJECT DriverObject
,
26 PDEVICE_OBJECT PhysicalDeviceObject
31 PDEVICE_OBJECT deviceObject
= NULL
;
32 PFDO_DEVICE_DATA deviceData
= NULL
;
33 PWCHAR deviceName
= NULL
;
40 DPRINT("Add Device: 0x%p\n", PhysicalDeviceObject
);
42 DPRINT("#################### Bus_CreateClose Creating FDO Device ####################\n");
43 status
= IoCreateDevice(DriverObject
,
44 sizeof(FDO_DEVICE_DATA
),
47 FILE_DEVICE_SECURE_OPEN
,
50 if (!NT_SUCCESS(status
))
52 DPRINT1("IoCreateDevice() failed with status 0x%X\n", status
);
56 deviceData
= (PFDO_DEVICE_DATA
) deviceObject
->DeviceExtension
;
57 RtlZeroMemory (deviceData
, sizeof (FDO_DEVICE_DATA
));
60 // Set the initial state of the FDO
63 INITIALIZE_PNP_STATE(deviceData
->Common
);
65 deviceData
->Common
.IsFDO
= TRUE
;
67 deviceData
->Common
.Self
= deviceObject
;
69 ExInitializeFastMutex (&deviceData
->Mutex
);
71 InitializeListHead (&deviceData
->ListOfPDOs
);
73 // Set the PDO for use with PlugPlay functions
75 deviceData
->UnderlyingPDO
= PhysicalDeviceObject
;
78 // Set the initial powerstate of the FDO
81 deviceData
->Common
.DevicePowerState
= PowerDeviceUnspecified
;
82 deviceData
->Common
.SystemPowerState
= PowerSystemWorking
;
84 deviceObject
->Flags
|= DO_POWER_PAGABLE
;
87 // Attach our FDO to the device stack.
88 // The return value of IoAttachDeviceToDeviceStack is the top of the
89 // attachment chain. This is where all the IRPs should be routed.
92 deviceData
->NextLowerDriver
= IoAttachDeviceToDeviceStack (
94 PhysicalDeviceObject
);
96 if (NULL
== deviceData
->NextLowerDriver
) {
98 status
= STATUS_NO_SUCH_DEVICE
;
105 // We will demonstrate here the step to retrieve the name of the PDO
108 status
= IoGetDeviceProperty (PhysicalDeviceObject
,
109 DevicePropertyPhysicalDeviceObjectName
,
114 if (status
!= STATUS_BUFFER_TOO_SMALL
)
116 DPRINT1("AddDevice:IoGDP failed (0x%x)\n", status
);
120 deviceName
= ExAllocatePoolWithTag (NonPagedPool
,
123 if (NULL
== deviceName
) {
124 DPRINT1("AddDevice: no memory to alloc for deviceName(0x%x)\n", nameLength
);
125 status
= STATUS_INSUFFICIENT_RESOURCES
;
129 status
= IoGetDeviceProperty (PhysicalDeviceObject
,
130 DevicePropertyPhysicalDeviceObjectName
,
135 if (!NT_SUCCESS (status
)) {
137 DPRINT1("AddDevice:IoGDP(2) failed (0x%x)", status
);
141 DPRINT("AddDevice: %p to %p->%p (%ws) \n",
143 deviceData
->NextLowerDriver
,
144 PhysicalDeviceObject
,
150 // We are done with initializing, so let's indicate that and return.
151 // This should be the final step in the AddDevice process.
153 deviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
157 ExFreePool(deviceName
);
159 if (!NT_SUCCESS(status
) && deviceObject
){
160 if (deviceData
&& deviceData
->NextLowerDriver
){
161 IoDetachDevice (deviceData
->NextLowerDriver
);
163 IoDeleteDevice (deviceObject
);
171 ACPIDispatchDeviceControl(
172 IN PDEVICE_OBJECT DeviceObject
,
175 PIO_STACK_LOCATION irpStack
;
176 NTSTATUS status
= STATUS_NOT_SUPPORTED
;
177 PCOMMON_DEVICE_DATA commonData
;
181 irpStack
= IoGetCurrentIrpStackLocation (Irp
);
182 ASSERT (IRP_MJ_DEVICE_CONTROL
== irpStack
->MajorFunction
);
184 commonData
= (PCOMMON_DEVICE_DATA
) DeviceObject
->DeviceExtension
;
186 Irp
->IoStatus
.Information
= 0;
188 if (!commonData
->IsFDO
)
190 switch (irpStack
->Parameters
.DeviceIoControl
.IoControlCode
)
192 case IOCTL_ACPI_EVAL_METHOD
:
193 status
= Bus_PDO_EvalMethod((PPDO_DEVICE_DATA
)commonData
,
197 /* TODO: Implement other IOCTLs */
200 DPRINT1("Unsupported IOCTL: %x\n", irpStack
->Parameters
.DeviceIoControl
.IoControlCode
);
205 DPRINT1("IOCTL sent to the ACPI FDO! Kill the caller!\n");
207 if (status
!= STATUS_PENDING
)
209 Irp
->IoStatus
.Status
= status
;
210 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
219 PDRIVER_OBJECT DriverObject
,
220 PUNICODE_STRING RegistryPath
223 DPRINT("Driver Entry \n");
226 // Set entry points into the driver
228 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ACPIDispatchDeviceControl
;
229 DriverObject
->MajorFunction
[IRP_MJ_PNP
] = Bus_PnP
;
230 DriverObject
->MajorFunction
[IRP_MJ_POWER
] = Bus_Power
;
232 DriverObject
->DriverExtension
->AddDevice
= Bus_AddDevice
;
234 return STATUS_SUCCESS
;