1 /* $Id: fdo.c,v 1.8 2004/06/09 14:22:53 ekohl Exp $
3 * PROJECT: ReactOS PCI bus driver
5 * PURPOSE: PCI device object dispatch routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * 10-09-2001 CSH Created
11 #include <ddk/ntddk.h>
19 /*** PRIVATE *****************************************************************/
24 PFDO_DEVICE_EXTENSION DeviceExtension
,
25 PCI_SLOT_NUMBER SlotNumber
,
26 PPCI_COMMON_CONFIG PciConfig
)
28 PLIST_ENTRY CurrentEntry
;
29 PPCI_DEVICE CurrentDevice
;
33 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
34 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
35 CurrentDevice
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
37 /* If both vendor ID and device ID match, it is the same device */
38 if ((PciConfig
->VendorID
== CurrentDevice
->PciConfig
.VendorID
) &&
39 (PciConfig
->DeviceID
== CurrentDevice
->PciConfig
.DeviceID
) &&
40 (SlotNumber
.u
.AsULONG
== CurrentDevice
->SlotNumber
.u
.AsULONG
)) {
41 *Device
= CurrentDevice
;
43 return STATUS_SUCCESS
;
46 CurrentEntry
= CurrentEntry
->Flink
;
51 return STATUS_UNSUCCESSFUL
;
57 PDEVICE_OBJECT DeviceObject
)
59 PFDO_DEVICE_EXTENSION DeviceExtension
;
60 PCI_COMMON_CONFIG PciConfig
;
61 PLIST_ENTRY CurrentEntry
;
63 PCI_SLOT_NUMBER SlotNumber
;
72 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
74 /* Mark all devices to be removed. If we don't discover them again during
75 enumeration, assume that they have been surprise removed */
76 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
77 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
78 Device
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
79 Device
->RemovePending
= TRUE
;
80 CurrentEntry
= CurrentEntry
->Flink
;
83 DeviceExtension
->DeviceListCount
= 0;
85 /* Enumerate devices on the PCI bus */
86 for (BusNumber
= 0; BusNumber
< 8; BusNumber
++)
88 SlotNumber
.u
.AsULONG
= 0;
89 for (DeviceNumber
= 0; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
91 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
92 for (FunctionNumber
= 0; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
94 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
96 Size
= HalGetBusData(PCIConfiguration
,
100 sizeof(PCI_COMMON_CONFIG
));
101 DPRINT("Size %lu\n", Size
);
102 if (Size
< sizeof(PCI_COMMON_CONFIG
))
104 if (FunctionNumber
== 0)
114 DPRINT("Bus %1lu Device %2lu Func %1lu VenID 0x%04hx DevID 0x%04hx\n",
121 Status
= FdoLocateChildDevice(&Device
, DeviceExtension
, SlotNumber
, &PciConfig
);
122 if (!NT_SUCCESS(Status
))
124 Device
= (PPCI_DEVICE
)ExAllocatePool(PagedPool
, sizeof(PCI_DEVICE
));
127 /* FIXME: Cleanup resources for already discovered devices */
128 return STATUS_INSUFFICIENT_RESOURCES
;
131 RtlZeroMemory(Device
,
134 Device
->BusNumber
= BusNumber
;
136 RtlCopyMemory(&Device
->SlotNumber
,
138 sizeof(PCI_SLOT_NUMBER
));
140 RtlCopyMemory(&Device
->PciConfig
,
142 sizeof(PCI_COMMON_CONFIG
));
144 ExInterlockedInsertTailList(
145 &DeviceExtension
->DeviceListHead
,
147 &DeviceExtension
->DeviceListLock
);
150 /* Don't remove this device */
151 Device
->RemovePending
= FALSE
;
153 DeviceExtension
->DeviceListCount
++;
160 return STATUS_SUCCESS
;
165 FdoQueryBusRelations(
166 IN PDEVICE_OBJECT DeviceObject
,
168 PIO_STACK_LOCATION IrpSp
)
170 PPDO_DEVICE_EXTENSION PdoDeviceExtension
;
171 PFDO_DEVICE_EXTENSION DeviceExtension
;
172 PDEVICE_RELATIONS Relations
;
173 PLIST_ENTRY CurrentEntry
;
176 BOOLEAN ErrorOccurred
;
177 NTSTATUS ErrorStatus
;
183 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
185 Status
= STATUS_SUCCESS
;
187 ErrorOccurred
= FALSE
;
189 FdoEnumerateDevices(DeviceObject
);
191 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
193 if (Irp
->IoStatus
.Information
) {
194 /* FIXME: Another bus driver has already created a DEVICE_RELATIONS
195 structure so we must merge this structure with our own */
198 Size
= sizeof(DEVICE_RELATIONS
) + sizeof(Relations
->Objects
) *
199 (DeviceExtension
->DeviceListCount
- 1);
200 Relations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
, Size
);
202 return STATUS_INSUFFICIENT_RESOURCES
;
204 Relations
->Count
= DeviceExtension
->DeviceListCount
;
207 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
208 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
209 Device
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
211 PdoDeviceExtension
= NULL
;
214 /* Create a physical device object for the
215 device as it does not already have one */
216 Status
= IoCreateDevice(
217 DeviceObject
->DriverObject
,
218 sizeof(PDO_DEVICE_EXTENSION
),
220 FILE_DEVICE_CONTROLLER
,
224 if (!NT_SUCCESS(Status
)) {
225 DPRINT("IoCreateDevice() failed with status 0x%X\n", Status
);
226 ErrorStatus
= Status
;
227 ErrorOccurred
= TRUE
;
231 Device
->Pdo
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
233 Device
->Pdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
235 //Device->Pdo->Flags |= DO_POWER_PAGABLE;
237 PdoDeviceExtension
= (PPDO_DEVICE_EXTENSION
)Device
->Pdo
->DeviceExtension
;
239 RtlZeroMemory(PdoDeviceExtension
, sizeof(PDO_DEVICE_EXTENSION
));
241 PdoDeviceExtension
->Common
.IsFDO
= FALSE
;
243 PdoDeviceExtension
->Common
.DeviceObject
= Device
->Pdo
;
245 PdoDeviceExtension
->Common
.DevicePowerState
= PowerDeviceD0
;
247 PdoDeviceExtension
->Fdo
= DeviceObject
;
249 PdoDeviceExtension
->BusNumber
= Device
->BusNumber
;
252 &PdoDeviceExtension
->SlotNumber
,
254 sizeof(PCI_SLOT_NUMBER
));
256 /* Add Device ID string */
257 if (!PciCreateDeviceIDString(&PdoDeviceExtension
->DeviceID
,
260 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
261 ErrorOccurred
= TRUE
;
265 DPRINT("DeviceID: %S\n", PdoDeviceExtension
->DeviceID
.Buffer
);
267 /* Add Instance ID string */
268 if (!PciCreateInstanceIDString(&PdoDeviceExtension
->InstanceID
,
271 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
272 ErrorOccurred
= TRUE
;
276 /* Add Hardware IDs string */
277 if (!PciCreateHardwareIDsString(&PdoDeviceExtension
->HardwareIDs
,
280 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
281 ErrorOccurred
= TRUE
;
285 /* Add Compatible IDs string */
286 if (!PciCreateCompatibleIDsString(&PdoDeviceExtension
->CompatibleIDs
,
289 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
290 ErrorOccurred
= TRUE
;
295 if (!Device
->RemovePending
) {
296 /* Reference the physical device object. The PnP manager
297 will dereference it again when it is no longer needed */
298 ObReferenceObject(Device
->Pdo
);
300 Relations
->Objects
[i
] = Device
->Pdo
;
305 CurrentEntry
= CurrentEntry
->Flink
;
309 /* FIXME: Cleanup all new PDOs created in this call. Please give me SEH!!! ;-) */
310 /* FIXME: Should IoAttachDeviceToDeviceStack() be undone? */
311 if (PdoDeviceExtension
) {
312 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceID
);
313 ExFreePool(PdoDeviceExtension
);
316 ExFreePool(Relations
);
320 Irp
->IoStatus
.Information
= (ULONG_PTR
)Relations
;
330 IN PDEVICE_OBJECT DeviceObject
,
333 PFDO_DEVICE_EXTENSION DeviceExtension
;
337 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
339 assert(DeviceExtension
->State
== dsStopped
);
341 InitializeListHead(&DeviceExtension
->DeviceListHead
);
342 KeInitializeSpinLock(&DeviceExtension
->DeviceListLock
);
343 DeviceExtension
->DeviceListCount
= 0;
344 DeviceExtension
->State
= dsStarted
;
346 //Irp->IoStatus.Information = 0;
348 return STATUS_SUCCESS
;
354 IN PDEVICE_OBJECT DeviceObject
,
356 PIO_STACK_LOCATION IrpSp
)
358 PFDO_DEVICE_EXTENSION DeviceExtension
;
363 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
365 if (IrpSp
->Parameters
.Power
.Type
== DevicePowerState
) {
366 /* FIXME: Set device power state for the device */
367 Status
= STATUS_UNSUCCESSFUL
;
369 Status
= STATUS_UNSUCCESSFUL
;
376 /*** PUBLIC ******************************************************************/
380 PDEVICE_OBJECT DeviceObject
,
383 * FUNCTION: Handle Plug and Play IRPs for the PCI device object
385 * DeviceObject = Pointer to functional device object of the PCI driver
386 * Irp = Pointer to IRP that should be handled
391 PFDO_DEVICE_EXTENSION DeviceExtension
;
392 PIO_STACK_LOCATION IrpSp
;
397 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
399 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
400 switch (IrpSp
->MinorFunction
) {
402 case IRP_MN_CANCEL_REMOVE_DEVICE
:
403 Status
= STATUS_NOT_IMPLEMENTED
;
406 case IRP_MN_CANCEL_STOP_DEVICE
:
407 Status
= STATUS_NOT_IMPLEMENTED
;
410 case IRP_MN_DEVICE_USAGE_NOTIFICATION
:
411 Status
= STATUS_NOT_IMPLEMENTED
;
414 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
415 Status
= STATUS_NOT_IMPLEMENTED
;
418 case IRP_MN_QUERY_DEVICE_RELATIONS
:
419 Status
= FdoQueryBusRelations(DeviceObject
, Irp
, IrpSp
);
422 case IRP_MN_QUERY_PNP_DEVICE_STATE
:
423 Status
= STATUS_NOT_IMPLEMENTED
;
426 case IRP_MN_QUERY_REMOVE_DEVICE
:
427 Status
= STATUS_NOT_IMPLEMENTED
;
430 case IRP_MN_QUERY_STOP_DEVICE
:
431 Status
= STATUS_NOT_IMPLEMENTED
;
434 case IRP_MN_REMOVE_DEVICE
:
435 Status
= STATUS_NOT_IMPLEMENTED
;
438 case IRP_MN_START_DEVICE
:
439 DPRINT("IRP_MN_START_DEVICE received\n");
440 Status
= FdoStartDevice(DeviceObject
, Irp
);
442 case IRP_MN_STOP_DEVICE
:
443 /* Currently not supported */
444 Status
= STATUS_UNSUCCESSFUL
;
447 case IRP_MN_SURPRISE_REMOVAL
:
448 Status
= STATUS_NOT_IMPLEMENTED
;
452 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
455 * Do NOT complete the IRP as it will be processed by the lower
456 * device object, which will complete the IRP
458 IoSkipCurrentIrpStackLocation(Irp
);
459 Status
= IoCallDriver(DeviceExtension
->Ldo
, Irp
);
465 if (Status
!= STATUS_PENDING
) {
466 if (Status
!= STATUS_NOT_IMPLEMENTED
)
467 Irp
->IoStatus
.Status
= Status
;
468 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
471 DPRINT("Leaving. Status 0x%X\n", Status
);
479 PDEVICE_OBJECT DeviceObject
,
482 * FUNCTION: Handle power management IRPs for the PCI device object
484 * DeviceObject = Pointer to functional device object of the PCI driver
485 * Irp = Pointer to IRP that should be handled
490 PIO_STACK_LOCATION IrpSp
;
495 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
497 switch (IrpSp
->MinorFunction
) {
498 case IRP_MN_SET_POWER
:
499 Status
= FdoSetPower(DeviceObject
, Irp
, IrpSp
);
503 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
504 Status
= STATUS_NOT_IMPLEMENTED
;
508 if (Status
!= STATUS_PENDING
) {
509 Irp
->IoStatus
.Status
= Status
;
510 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
513 DPRINT("Leaving. Status 0x%X\n", Status
);