2 * PROJECT: ReactOS PCI bus driver
4 * PURPOSE: PCI device object dispatch routines
5 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * 10-09-2001 CSH Created
17 /*** PRIVATE *****************************************************************/
22 PFDO_DEVICE_EXTENSION DeviceExtension
,
23 PCI_SLOT_NUMBER SlotNumber
,
24 PPCI_COMMON_CONFIG PciConfig
)
26 PLIST_ENTRY CurrentEntry
;
27 PPCI_DEVICE CurrentDevice
;
31 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
32 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
33 CurrentDevice
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
35 /* If both vendor ID and device ID match, it is the same device */
36 if ((PciConfig
->VendorID
== CurrentDevice
->PciConfig
.VendorID
) &&
37 (PciConfig
->DeviceID
== CurrentDevice
->PciConfig
.DeviceID
) &&
38 (SlotNumber
.u
.AsULONG
== CurrentDevice
->SlotNumber
.u
.AsULONG
)) {
39 *Device
= CurrentDevice
;
41 return STATUS_SUCCESS
;
44 CurrentEntry
= CurrentEntry
->Flink
;
49 return STATUS_UNSUCCESSFUL
;
55 PDEVICE_OBJECT DeviceObject
)
57 PFDO_DEVICE_EXTENSION DeviceExtension
;
58 PCI_COMMON_CONFIG PciConfig
;
59 PLIST_ENTRY CurrentEntry
;
61 PCI_SLOT_NUMBER SlotNumber
;
69 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
71 /* Mark all devices to be removed. If we don't discover them again during
72 enumeration, assume that they have been surprise removed */
73 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
74 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
75 Device
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
76 Device
->RemovePending
= TRUE
;
77 CurrentEntry
= CurrentEntry
->Flink
;
80 DeviceExtension
->DeviceListCount
= 0;
82 /* Enumerate devices on the PCI bus */
83 SlotNumber
.u
.AsULONG
= 0;
84 for (DeviceNumber
= 0; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
86 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
87 for (FunctionNumber
= 0; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
89 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
91 DPRINT("Bus %1lu Device %2lu Func %1lu\n",
92 DeviceExtension
->BusNumber
,
96 RtlZeroMemory(&PciConfig
,
97 sizeof(PCI_COMMON_CONFIG
));
99 Size
= HalGetBusData(PCIConfiguration
,
100 DeviceExtension
->BusNumber
,
101 SlotNumber
.u
.AsULONG
,
103 PCI_COMMON_HDR_LENGTH
);
104 DPRINT("Size %lu\n", Size
);
105 if (Size
< PCI_COMMON_HDR_LENGTH
)
107 if (FunctionNumber
== 0)
117 DPRINT("Bus %1lu Device %2lu Func %1lu VenID 0x%04hx DevID 0x%04hx\n",
118 DeviceExtension
->BusNumber
,
124 Status
= FdoLocateChildDevice(&Device
, DeviceExtension
, SlotNumber
, &PciConfig
);
125 if (!NT_SUCCESS(Status
))
127 Device
= (PPCI_DEVICE
)ExAllocatePoolWithTag(NonPagedPool
, sizeof(PCI_DEVICE
),TAG_PCI
);
130 /* FIXME: Cleanup resources for already discovered devices */
131 return STATUS_INSUFFICIENT_RESOURCES
;
134 RtlZeroMemory(Device
,
137 Device
->BusNumber
= DeviceExtension
->BusNumber
;
139 RtlCopyMemory(&Device
->SlotNumber
,
141 sizeof(PCI_SLOT_NUMBER
));
143 RtlCopyMemory(&Device
->PciConfig
,
145 sizeof(PCI_COMMON_CONFIG
));
147 ExInterlockedInsertTailList(
148 &DeviceExtension
->DeviceListHead
,
150 &DeviceExtension
->DeviceListLock
);
153 /* Don't remove this device */
154 Device
->RemovePending
= FALSE
;
156 DeviceExtension
->DeviceListCount
++;
158 /* Skip to next device if the current one is not a multifunction device */
159 if ((FunctionNumber
== 0) &&
160 ((PciConfig
.HeaderType
& 0x80) == 0))
169 return STATUS_SUCCESS
;
174 FdoQueryBusRelations(
175 IN PDEVICE_OBJECT DeviceObject
,
177 PIO_STACK_LOCATION IrpSp
)
179 PPDO_DEVICE_EXTENSION PdoDeviceExtension
= NULL
;
180 PFDO_DEVICE_EXTENSION DeviceExtension
;
181 PDEVICE_RELATIONS Relations
;
182 PLIST_ENTRY CurrentEntry
;
185 BOOLEAN ErrorOccurred
;
186 NTSTATUS ErrorStatus
;
190 UNREFERENCED_PARAMETER(IrpSp
);
194 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
196 Status
= STATUS_SUCCESS
;
198 ErrorOccurred
= FALSE
;
200 FdoEnumerateDevices(DeviceObject
);
202 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
204 if (Irp
->IoStatus
.Information
) {
205 /* FIXME: Another bus driver has already created a DEVICE_RELATIONS
206 structure so we must merge this structure with our own */
209 Size
= sizeof(DEVICE_RELATIONS
) + sizeof(Relations
->Objects
) *
210 (DeviceExtension
->DeviceListCount
- 1);
211 Relations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
, Size
);
213 return STATUS_INSUFFICIENT_RESOURCES
;
215 Relations
->Count
= DeviceExtension
->DeviceListCount
;
218 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
219 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
220 Device
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
222 PdoDeviceExtension
= NULL
;
225 /* Create a physical device object for the
226 device as it does not already have one */
227 Status
= IoCreateDevice(
228 DeviceObject
->DriverObject
,
229 sizeof(PDO_DEVICE_EXTENSION
),
231 FILE_DEVICE_CONTROLLER
,
232 FILE_AUTOGENERATED_DEVICE_NAME
,
235 if (!NT_SUCCESS(Status
)) {
236 DPRINT("IoCreateDevice() failed with status 0x%X\n", Status
);
237 ErrorStatus
= Status
;
238 ErrorOccurred
= TRUE
;
242 Device
->Pdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
244 //Device->Pdo->Flags |= DO_POWER_PAGABLE;
246 PdoDeviceExtension
= (PPDO_DEVICE_EXTENSION
)Device
->Pdo
->DeviceExtension
;
248 RtlZeroMemory(PdoDeviceExtension
, sizeof(PDO_DEVICE_EXTENSION
));
250 PdoDeviceExtension
->Common
.IsFDO
= FALSE
;
252 PdoDeviceExtension
->Common
.DeviceObject
= Device
->Pdo
;
254 PdoDeviceExtension
->Common
.DevicePowerState
= PowerDeviceD0
;
256 PdoDeviceExtension
->Fdo
= DeviceObject
;
258 PdoDeviceExtension
->PciDevice
= Device
;
260 /* Add Device ID string */
261 Status
= PciCreateDeviceIDString(&PdoDeviceExtension
->DeviceID
, Device
);
262 if (!NT_SUCCESS(Status
))
264 ErrorStatus
= Status
;
265 ErrorOccurred
= TRUE
;
269 DPRINT("DeviceID: %S\n", PdoDeviceExtension
->DeviceID
.Buffer
);
271 /* Add Instance ID string */
272 Status
= PciCreateInstanceIDString(&PdoDeviceExtension
->InstanceID
, Device
);
273 if (!NT_SUCCESS(Status
))
275 ErrorStatus
= Status
;
276 ErrorOccurred
= TRUE
;
280 /* Add Hardware IDs string */
281 Status
= PciCreateHardwareIDsString(&PdoDeviceExtension
->HardwareIDs
, Device
);
282 if (!NT_SUCCESS(Status
))
284 ErrorStatus
= Status
;
285 ErrorOccurred
= TRUE
;
289 /* Add Compatible IDs string */
290 Status
= PciCreateCompatibleIDsString(&PdoDeviceExtension
->CompatibleIDs
, Device
);
291 if (!NT_SUCCESS(Status
))
293 ErrorStatus
= Status
;
294 ErrorOccurred
= TRUE
;
298 /* Add device description string */
299 Status
= PciCreateDeviceDescriptionString(&PdoDeviceExtension
->DeviceDescription
, Device
);
300 if (!NT_SUCCESS(Status
))
302 ErrorStatus
= Status
;
303 ErrorOccurred
= TRUE
;
307 /* Add device location string */
308 Status
= PciCreateDeviceLocationString(&PdoDeviceExtension
->DeviceLocation
, Device
);
309 if (!NT_SUCCESS(Status
))
311 ErrorStatus
= Status
;
312 ErrorOccurred
= TRUE
;
317 if (!Device
->RemovePending
) {
318 /* Reference the physical device object. The PnP manager
319 will dereference it again when it is no longer needed */
320 ObReferenceObject(Device
->Pdo
);
322 Relations
->Objects
[i
] = Device
->Pdo
;
327 CurrentEntry
= CurrentEntry
->Flink
;
331 /* FIXME: Cleanup all new PDOs created in this call. Please give me SEH!!! ;-) */
332 /* FIXME: Should IoAttachDeviceToDeviceStack() be undone? */
333 if (PdoDeviceExtension
) {
334 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceID
);
335 RtlFreeUnicodeString(&PdoDeviceExtension
->InstanceID
);
336 RtlFreeUnicodeString(&PdoDeviceExtension
->HardwareIDs
);
337 RtlFreeUnicodeString(&PdoDeviceExtension
->CompatibleIDs
);
338 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceDescription
);
339 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceLocation
);
342 ExFreePool(Relations
);
346 Irp
->IoStatus
.Information
= (ULONG_PTR
)Relations
;
356 IN PDEVICE_OBJECT DeviceObject
,
359 PFDO_DEVICE_EXTENSION DeviceExtension
;
360 PCM_RESOURCE_LIST AllocatedResources
;
361 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
362 ULONG FoundBusNumber
= FALSE
;
367 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
369 AllocatedResources
= IoGetCurrentIrpStackLocation(Irp
)->Parameters
.StartDevice
.AllocatedResources
;
370 if (!AllocatedResources
)
372 DPRINT("No allocated resources sent to driver\n");
373 return STATUS_INSUFFICIENT_RESOURCES
;
375 if (AllocatedResources
->Count
< 1)
377 DPRINT("Not enough allocated resources sent to driver\n");
378 return STATUS_INSUFFICIENT_RESOURCES
;
380 if (AllocatedResources
->List
[0].PartialResourceList
.Version
!= 1
381 || AllocatedResources
->List
[0].PartialResourceList
.Revision
!= 1)
382 return STATUS_REVISION_MISMATCH
;
384 ASSERT(DeviceExtension
->State
== dsStopped
);
386 /* By default, use the bus number in the resource list header */
387 DeviceExtension
->BusNumber
= AllocatedResources
->List
[0].BusNumber
;
389 for (i
= 0; i
< AllocatedResources
->List
[0].PartialResourceList
.Count
; i
++)
391 ResourceDescriptor
= &AllocatedResources
->List
[0].PartialResourceList
.PartialDescriptors
[i
];
392 switch (ResourceDescriptor
->Type
)
394 case CmResourceTypeBusNumber
:
396 if (FoundBusNumber
|| ResourceDescriptor
->u
.BusNumber
.Length
!= 1)
397 return STATUS_INVALID_PARAMETER
;
398 /* Use this one instead */
399 ASSERT(AllocatedResources
->List
[0].BusNumber
== ResourceDescriptor
->u
.BusNumber
.Start
);
400 DeviceExtension
->BusNumber
= ResourceDescriptor
->u
.BusNumber
.Start
;
401 DPRINT("Found bus number resource: %lu\n", DeviceExtension
->BusNumber
);
402 FoundBusNumber
= TRUE
;
406 DPRINT("Unknown resource descriptor type 0x%x\n", ResourceDescriptor
->Type
);
410 InitializeListHead(&DeviceExtension
->DeviceListHead
);
411 KeInitializeSpinLock(&DeviceExtension
->DeviceListLock
);
412 DeviceExtension
->DeviceListCount
= 0;
413 DeviceExtension
->State
= dsStarted
;
415 ExInterlockedInsertTailList(
416 &DriverExtension
->BusListHead
,
417 &DeviceExtension
->ListEntry
,
418 &DriverExtension
->BusListLock
);
420 Irp
->IoStatus
.Information
= 0;
422 return STATUS_SUCCESS
;
428 IN PDEVICE_OBJECT DeviceObject
,
430 PIO_STACK_LOCATION IrpSp
)
432 PFDO_DEVICE_EXTENSION DeviceExtension
;
435 UNREFERENCED_PARAMETER(Irp
);
439 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
441 if (IrpSp
->Parameters
.Power
.Type
== DevicePowerState
) {
442 /* FIXME: Set device power state for the device */
443 Status
= STATUS_UNSUCCESSFUL
;
445 Status
= STATUS_UNSUCCESSFUL
;
452 /*** PUBLIC ******************************************************************/
456 PDEVICE_OBJECT DeviceObject
,
459 * FUNCTION: Handle Plug and Play IRPs for the PCI device object
461 * DeviceObject = Pointer to functional device object of the PCI driver
462 * Irp = Pointer to IRP that should be handled
467 PFDO_DEVICE_EXTENSION DeviceExtension
;
468 PIO_STACK_LOCATION IrpSp
;
473 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
475 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
476 switch (IrpSp
->MinorFunction
) {
478 case IRP_MN_CANCEL_REMOVE_DEVICE
:
479 Status
= STATUS_NOT_IMPLEMENTED
;
482 case IRP_MN_CANCEL_STOP_DEVICE
:
483 Status
= STATUS_NOT_IMPLEMENTED
;
486 case IRP_MN_DEVICE_USAGE_NOTIFICATION
:
487 Status
= STATUS_NOT_IMPLEMENTED
;
490 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
491 Status
= STATUS_NOT_IMPLEMENTED
;
494 case IRP_MN_QUERY_DEVICE_RELATIONS
:
495 Status
= FdoQueryBusRelations(DeviceObject
, Irp
, IrpSp
);
498 case IRP_MN_QUERY_PNP_DEVICE_STATE
:
499 Status
= STATUS_NOT_IMPLEMENTED
;
502 case IRP_MN_QUERY_REMOVE_DEVICE
:
503 Status
= STATUS_NOT_IMPLEMENTED
;
506 case IRP_MN_QUERY_STOP_DEVICE
:
507 Status
= STATUS_NOT_IMPLEMENTED
;
510 case IRP_MN_REMOVE_DEVICE
:
511 Status
= STATUS_NOT_IMPLEMENTED
;
514 case IRP_MN_START_DEVICE
:
515 DPRINT("IRP_MN_START_DEVICE received\n");
516 Status
= FdoStartDevice(DeviceObject
, Irp
);
518 case IRP_MN_STOP_DEVICE
:
519 /* Currently not supported */
520 Status
= STATUS_UNSUCCESSFUL
;
523 case IRP_MN_SURPRISE_REMOVAL
:
524 Status
= STATUS_NOT_IMPLEMENTED
;
528 DPRINT1("Unknown IOCTL 0x%lx\n", IrpSp
->MinorFunction
);
531 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
533 * Do NOT complete the IRP as it will be processed by the lower
534 * device object, which will complete the IRP
536 IoSkipCurrentIrpStackLocation(Irp
);
537 Status
= IoCallDriver(DeviceExtension
->Ldo
, Irp
);
543 if (Status
!= STATUS_PENDING
) {
544 if (Status
!= STATUS_NOT_IMPLEMENTED
)
545 Irp
->IoStatus
.Status
= Status
;
546 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
549 DPRINT("Leaving. Status 0x%X\n", Status
);
557 PDEVICE_OBJECT DeviceObject
,
560 * FUNCTION: Handle power management IRPs for the PCI device object
562 * DeviceObject = Pointer to functional device object of the PCI driver
563 * Irp = Pointer to IRP that should be handled
568 PIO_STACK_LOCATION IrpSp
;
573 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
575 switch (IrpSp
->MinorFunction
) {
576 case IRP_MN_SET_POWER
:
577 Status
= FdoSetPower(DeviceObject
, Irp
, IrpSp
);
581 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
582 Status
= STATUS_NOT_IMPLEMENTED
;
586 if (Status
!= STATUS_PENDING
) {
587 Irp
->IoStatus
.Status
= Status
;
588 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
591 DPRINT("Leaving. Status 0x%X\n", Status
);