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 ACPIDispatchCreateClose(
172 IN PDEVICE_OBJECT DeviceObject
,
175 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
176 Irp
->IoStatus
.Information
= 0;
178 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
180 return STATUS_SUCCESS
;
185 ACPIDispatchDeviceControl(
186 IN PDEVICE_OBJECT DeviceObject
,
189 PIO_STACK_LOCATION irpStack
;
190 NTSTATUS status
= STATUS_NOT_SUPPORTED
;
191 PCOMMON_DEVICE_DATA commonData
;
195 irpStack
= IoGetCurrentIrpStackLocation (Irp
);
196 ASSERT (IRP_MJ_DEVICE_CONTROL
== irpStack
->MajorFunction
);
198 commonData
= (PCOMMON_DEVICE_DATA
) DeviceObject
->DeviceExtension
;
200 Irp
->IoStatus
.Information
= 0;
202 if (!commonData
->IsFDO
)
204 switch (irpStack
->Parameters
.DeviceIoControl
.IoControlCode
)
206 case IOCTL_ACPI_EVAL_METHOD
:
207 status
= Bus_PDO_EvalMethod((PPDO_DEVICE_DATA
)commonData
,
211 /* TODO: Implement other IOCTLs */
214 DPRINT1("Unsupported IOCTL: %x\n", irpStack
->Parameters
.DeviceIoControl
.IoControlCode
);
219 DPRINT1("IOCTL sent to the ACPI FDO! Kill the caller!\n");
221 if (status
!= STATUS_PENDING
)
223 Irp
->IoStatus
.Status
= status
;
224 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
233 PDRIVER_OBJECT DriverObject
,
234 PUNICODE_STRING RegistryPath
237 DPRINT("Driver Entry \n");
240 // Set entry points into the driver
242 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ACPIDispatchDeviceControl
;
243 DriverObject
->MajorFunction
[IRP_MJ_PNP
] = Bus_PnP
;
244 DriverObject
->MajorFunction
[IRP_MJ_POWER
] = Bus_Power
;
245 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ACPIDispatchCreateClose
;
246 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ACPIDispatchCreateClose
;
248 DriverObject
->DriverExtension
->AddDevice
= Bus_AddDevice
;
250 return STATUS_SUCCESS
;