1 /* $Id: fdo.c,v 1.7 2004/03/14 17:10:43 navaraf 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
;
178 WCHAR Buffer
[MAX_PATH
];
184 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
186 Status
= STATUS_SUCCESS
;
188 ErrorOccurred
= FALSE
;
190 FdoEnumerateDevices(DeviceObject
);
192 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
194 if (Irp
->IoStatus
.Information
) {
195 /* FIXME: Another bus driver has already created a DEVICE_RELATIONS
196 structure so we must merge this structure with our own */
199 Size
= sizeof(DEVICE_RELATIONS
) + sizeof(Relations
->Objects
) *
200 (DeviceExtension
->DeviceListCount
- 1);
201 Relations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
, Size
);
203 return STATUS_INSUFFICIENT_RESOURCES
;
205 Relations
->Count
= DeviceExtension
->DeviceListCount
;
208 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
209 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
210 Device
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
212 PdoDeviceExtension
= NULL
;
215 /* Create a physical device object for the
216 device as it does not already have one */
217 Status
= IoCreateDevice(
218 DeviceObject
->DriverObject
,
219 sizeof(PDO_DEVICE_EXTENSION
),
221 FILE_DEVICE_CONTROLLER
,
225 if (!NT_SUCCESS(Status
)) {
226 DPRINT("IoCreateDevice() failed with status 0x%X\n", Status
);
227 ErrorStatus
= Status
;
228 ErrorOccurred
= TRUE
;
232 Device
->Pdo
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
234 Device
->Pdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
236 //Device->Pdo->Flags |= DO_POWER_PAGABLE;
238 PdoDeviceExtension
= (PPDO_DEVICE_EXTENSION
)Device
->Pdo
->DeviceExtension
;
240 RtlZeroMemory(PdoDeviceExtension
, sizeof(PDO_DEVICE_EXTENSION
));
242 PdoDeviceExtension
->Common
.IsFDO
= FALSE
;
244 PdoDeviceExtension
->Common
.DeviceObject
= Device
->Pdo
;
246 PdoDeviceExtension
->Common
.DevicePowerState
= PowerDeviceD0
;
248 PdoDeviceExtension
->Fdo
= DeviceObject
;
250 PdoDeviceExtension
->BusNumber
= Device
->BusNumber
;
253 &PdoDeviceExtension
->SlotNumber
,
255 sizeof(PCI_SLOT_NUMBER
));
257 /* FIXME: Get device properties (Hardware IDs, etc.) */
261 L
"PCI\\VEN_%04X&DEV_%04X&SUBSYS_%08X&REV_%02X",
262 Device
->PciConfig
.VendorID
,
263 Device
->PciConfig
.DeviceID
,
264 (Device
->PciConfig
.u
.type0
.SubSystemID
<< 16) +
265 Device
->PciConfig
.u
.type0
.SubVendorID
,
266 Device
->PciConfig
.RevisionID
);
268 if (!PciCreateUnicodeString(
269 &PdoDeviceExtension
->DeviceID
,
272 ErrorOccurred
= TRUE
;
276 DPRINT("DeviceID: %S\n", PdoDeviceExtension
->DeviceID
.Buffer
);
279 if (!Device
->RemovePending
) {
280 /* Reference the physical device object. The PnP manager
281 will dereference it again when it is no longer needed */
282 ObReferenceObject(Device
->Pdo
);
284 Relations
->Objects
[i
] = Device
->Pdo
;
289 CurrentEntry
= CurrentEntry
->Flink
;
293 /* FIXME: Cleanup all new PDOs created in this call. Please give me SEH!!! ;-) */
294 /* FIXME: Should IoAttachDeviceToDeviceStack() be undone? */
295 if (PdoDeviceExtension
) {
296 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceID
);
297 ExFreePool(PdoDeviceExtension
);
300 ExFreePool(Relations
);
304 Irp
->IoStatus
.Information
= (ULONG_PTR
)Relations
;
314 IN PDEVICE_OBJECT DeviceObject
,
317 PFDO_DEVICE_EXTENSION DeviceExtension
;
321 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
323 assert(DeviceExtension
->State
== dsStopped
);
325 InitializeListHead(&DeviceExtension
->DeviceListHead
);
326 KeInitializeSpinLock(&DeviceExtension
->DeviceListLock
);
327 DeviceExtension
->DeviceListCount
= 0;
328 DeviceExtension
->State
= dsStarted
;
330 //Irp->IoStatus.Information = 0;
332 return STATUS_SUCCESS
;
338 IN PDEVICE_OBJECT DeviceObject
,
340 PIO_STACK_LOCATION IrpSp
)
342 PFDO_DEVICE_EXTENSION DeviceExtension
;
347 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
349 if (IrpSp
->Parameters
.Power
.Type
== DevicePowerState
) {
350 /* FIXME: Set device power state for the device */
351 Status
= STATUS_UNSUCCESSFUL
;
353 Status
= STATUS_UNSUCCESSFUL
;
360 /*** PUBLIC ******************************************************************/
364 PDEVICE_OBJECT DeviceObject
,
367 * FUNCTION: Handle Plug and Play IRPs for the PCI device object
369 * DeviceObject = Pointer to functional device object of the PCI driver
370 * Irp = Pointer to IRP that should be handled
375 PFDO_DEVICE_EXTENSION DeviceExtension
;
376 PIO_STACK_LOCATION IrpSp
;
381 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
383 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
384 switch (IrpSp
->MinorFunction
) {
386 case IRP_MN_CANCEL_REMOVE_DEVICE
:
387 Status
= STATUS_NOT_IMPLEMENTED
;
390 case IRP_MN_CANCEL_STOP_DEVICE
:
391 Status
= STATUS_NOT_IMPLEMENTED
;
394 case IRP_MN_DEVICE_USAGE_NOTIFICATION
:
395 Status
= STATUS_NOT_IMPLEMENTED
;
398 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
399 Status
= STATUS_NOT_IMPLEMENTED
;
402 case IRP_MN_QUERY_DEVICE_RELATIONS
:
403 Status
= FdoQueryBusRelations(DeviceObject
, Irp
, IrpSp
);
406 case IRP_MN_QUERY_PNP_DEVICE_STATE
:
407 Status
= STATUS_NOT_IMPLEMENTED
;
410 case IRP_MN_QUERY_REMOVE_DEVICE
:
411 Status
= STATUS_NOT_IMPLEMENTED
;
414 case IRP_MN_QUERY_STOP_DEVICE
:
415 Status
= STATUS_NOT_IMPLEMENTED
;
418 case IRP_MN_REMOVE_DEVICE
:
419 Status
= STATUS_NOT_IMPLEMENTED
;
422 case IRP_MN_START_DEVICE
:
423 DPRINT("IRP_MN_START_DEVICE received\n");
424 Status
= FdoStartDevice(DeviceObject
, Irp
);
426 case IRP_MN_STOP_DEVICE
:
427 /* Currently not supported */
428 Status
= STATUS_UNSUCCESSFUL
;
431 case IRP_MN_SURPRISE_REMOVAL
:
432 Status
= STATUS_NOT_IMPLEMENTED
;
436 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
439 * Do NOT complete the IRP as it will be processed by the lower
440 * device object, which will complete the IRP
442 IoSkipCurrentIrpStackLocation(Irp
);
443 Status
= IoCallDriver(DeviceExtension
->Ldo
, Irp
);
449 if (Status
!= STATUS_PENDING
) {
450 if (Status
!= STATUS_NOT_IMPLEMENTED
)
451 Irp
->IoStatus
.Status
= Status
;
452 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
455 DPRINT("Leaving. Status 0x%X\n", Status
);
463 PDEVICE_OBJECT DeviceObject
,
466 * FUNCTION: Handle power management IRPs for the PCI device object
468 * DeviceObject = Pointer to functional device object of the PCI driver
469 * Irp = Pointer to IRP that should be handled
474 PIO_STACK_LOCATION IrpSp
;
479 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
481 switch (IrpSp
->MinorFunction
) {
482 case IRP_MN_SET_POWER
:
483 Status
= FdoSetPower(DeviceObject
, Irp
, IrpSp
);
487 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
488 Status
= STATUS_NOT_IMPLEMENTED
;
492 if (Status
!= STATUS_PENDING
) {
493 Irp
->IoStatus
.Status
= Status
;
494 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
497 DPRINT("Leaving. Status 0x%X\n", Status
);