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 *****************************************************************/
19 static IO_COMPLETION_ROUTINE ForwardIrpAndWaitCompletion
;
22 ForwardIrpAndWaitCompletion(
23 IN PDEVICE_OBJECT DeviceObject
,
27 UNREFERENCED_PARAMETER(DeviceObject
);
28 if (Irp
->PendingReturned
)
29 KeSetEvent((PKEVENT
)Context
, IO_NO_INCREMENT
, FALSE
);
30 return STATUS_MORE_PROCESSING_REQUIRED
;
35 IN PDEVICE_OBJECT DeviceObject
,
40 PDEVICE_OBJECT LowerDevice
= ((PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->Ldo
;
43 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
44 IoCopyCurrentIrpStackLocationToNext(Irp
);
46 IoSetCompletionRoutine(Irp
, ForwardIrpAndWaitCompletion
, &Event
, TRUE
, TRUE
, TRUE
);
48 Status
= IoCallDriver(LowerDevice
, Irp
);
49 if (Status
== STATUS_PENDING
)
51 Status
= KeWaitForSingleObject(&Event
, Suspended
, KernelMode
, FALSE
, NULL
);
52 if (NT_SUCCESS(Status
))
53 Status
= Irp
->IoStatus
.Status
;
62 PFDO_DEVICE_EXTENSION DeviceExtension
,
63 PCI_SLOT_NUMBER SlotNumber
,
64 PPCI_COMMON_CONFIG PciConfig
)
66 PLIST_ENTRY CurrentEntry
;
67 PPCI_DEVICE CurrentDevice
;
71 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
72 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
73 CurrentDevice
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
75 /* If both vendor ID and device ID match, it is the same device */
76 if ((PciConfig
->VendorID
== CurrentDevice
->PciConfig
.VendorID
) &&
77 (PciConfig
->DeviceID
== CurrentDevice
->PciConfig
.DeviceID
) &&
78 (SlotNumber
.u
.AsULONG
== CurrentDevice
->SlotNumber
.u
.AsULONG
)) {
79 *Device
= CurrentDevice
;
81 return STATUS_SUCCESS
;
84 CurrentEntry
= CurrentEntry
->Flink
;
89 return STATUS_UNSUCCESSFUL
;
95 PDEVICE_OBJECT DeviceObject
)
97 PFDO_DEVICE_EXTENSION DeviceExtension
;
98 PCI_COMMON_CONFIG PciConfig
;
100 PCI_SLOT_NUMBER SlotNumber
;
102 ULONG FunctionNumber
;
108 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
110 DeviceExtension
->DeviceListCount
= 0;
112 /* Enumerate devices on the PCI bus */
113 SlotNumber
.u
.AsULONG
= 0;
114 for (DeviceNumber
= 0; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
116 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
117 for (FunctionNumber
= 0; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
119 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
121 DPRINT("Bus %1lu Device %2lu Func %1lu\n",
122 DeviceExtension
->BusNumber
,
126 RtlZeroMemory(&PciConfig
,
127 sizeof(PCI_COMMON_CONFIG
));
129 Size
= HalGetBusData(PCIConfiguration
,
130 DeviceExtension
->BusNumber
,
131 SlotNumber
.u
.AsULONG
,
133 PCI_COMMON_HDR_LENGTH
);
134 DPRINT("Size %lu\n", Size
);
135 if (Size
< PCI_COMMON_HDR_LENGTH
)
137 if (FunctionNumber
== 0)
147 DPRINT("Bus %1lu Device %2lu Func %1lu VenID 0x%04hx DevID 0x%04hx\n",
148 DeviceExtension
->BusNumber
,
154 Status
= FdoLocateChildDevice(&Device
, DeviceExtension
, SlotNumber
, &PciConfig
);
155 if (!NT_SUCCESS(Status
))
157 Device
= (PPCI_DEVICE
)ExAllocatePoolWithTag(NonPagedPool
, sizeof(PCI_DEVICE
),TAG_PCI
);
160 /* FIXME: Cleanup resources for already discovered devices */
161 return STATUS_INSUFFICIENT_RESOURCES
;
164 RtlZeroMemory(Device
,
167 Device
->BusNumber
= DeviceExtension
->BusNumber
;
169 RtlCopyMemory(&Device
->SlotNumber
,
171 sizeof(PCI_SLOT_NUMBER
));
173 RtlCopyMemory(&Device
->PciConfig
,
175 sizeof(PCI_COMMON_CONFIG
));
177 ExInterlockedInsertTailList(
178 &DeviceExtension
->DeviceListHead
,
180 &DeviceExtension
->DeviceListLock
);
183 DeviceExtension
->DeviceListCount
++;
185 /* Skip to next device if the current one is not a multifunction device */
186 if ((FunctionNumber
== 0) &&
187 ((PciConfig
.HeaderType
& 0x80) == 0))
196 return STATUS_SUCCESS
;
201 FdoQueryBusRelations(
202 IN PDEVICE_OBJECT DeviceObject
,
204 PIO_STACK_LOCATION IrpSp
)
206 PPDO_DEVICE_EXTENSION PdoDeviceExtension
= NULL
;
207 PFDO_DEVICE_EXTENSION DeviceExtension
;
208 PDEVICE_RELATIONS Relations
;
209 PLIST_ENTRY CurrentEntry
;
212 BOOLEAN ErrorOccurred
;
213 NTSTATUS ErrorStatus
;
217 UNREFERENCED_PARAMETER(IrpSp
);
221 ErrorStatus
= STATUS_INSUFFICIENT_RESOURCES
;
223 Status
= STATUS_SUCCESS
;
225 ErrorOccurred
= FALSE
;
227 FdoEnumerateDevices(DeviceObject
);
229 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
231 if (Irp
->IoStatus
.Information
) {
232 /* FIXME: Another bus driver has already created a DEVICE_RELATIONS
233 structure so we must merge this structure with our own */
236 Size
= sizeof(DEVICE_RELATIONS
) + sizeof(Relations
->Objects
) *
237 (DeviceExtension
->DeviceListCount
- 1);
238 Relations
= (PDEVICE_RELATIONS
)ExAllocatePool(PagedPool
, Size
);
240 return STATUS_INSUFFICIENT_RESOURCES
;
242 Relations
->Count
= DeviceExtension
->DeviceListCount
;
245 CurrentEntry
= DeviceExtension
->DeviceListHead
.Flink
;
246 while (CurrentEntry
!= &DeviceExtension
->DeviceListHead
) {
247 Device
= CONTAINING_RECORD(CurrentEntry
, PCI_DEVICE
, ListEntry
);
249 PdoDeviceExtension
= NULL
;
252 /* Create a physical device object for the
253 device as it does not already have one */
254 Status
= IoCreateDevice(
255 DeviceObject
->DriverObject
,
256 sizeof(PDO_DEVICE_EXTENSION
),
258 FILE_DEVICE_CONTROLLER
,
259 FILE_AUTOGENERATED_DEVICE_NAME
,
262 if (!NT_SUCCESS(Status
)) {
263 DPRINT("IoCreateDevice() failed with status 0x%X\n", Status
);
264 ErrorStatus
= Status
;
265 ErrorOccurred
= TRUE
;
269 Device
->Pdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
271 //Device->Pdo->Flags |= DO_POWER_PAGABLE;
273 PdoDeviceExtension
= (PPDO_DEVICE_EXTENSION
)Device
->Pdo
->DeviceExtension
;
275 RtlZeroMemory(PdoDeviceExtension
, sizeof(PDO_DEVICE_EXTENSION
));
277 PdoDeviceExtension
->Common
.IsFDO
= FALSE
;
279 PdoDeviceExtension
->Common
.DeviceObject
= Device
->Pdo
;
281 PdoDeviceExtension
->Common
.DevicePowerState
= PowerDeviceD0
;
283 PdoDeviceExtension
->Fdo
= DeviceObject
;
285 PdoDeviceExtension
->PciDevice
= Device
;
287 /* Add Device ID string */
288 Status
= PciCreateDeviceIDString(&PdoDeviceExtension
->DeviceID
, Device
);
289 if (!NT_SUCCESS(Status
))
291 ErrorStatus
= Status
;
292 ErrorOccurred
= TRUE
;
296 DPRINT("DeviceID: %S\n", PdoDeviceExtension
->DeviceID
.Buffer
);
298 /* Add Instance ID string */
299 Status
= PciCreateInstanceIDString(&PdoDeviceExtension
->InstanceID
, Device
);
300 if (!NT_SUCCESS(Status
))
302 ErrorStatus
= Status
;
303 ErrorOccurred
= TRUE
;
307 /* Add Hardware IDs string */
308 Status
= PciCreateHardwareIDsString(&PdoDeviceExtension
->HardwareIDs
, Device
);
309 if (!NT_SUCCESS(Status
))
311 ErrorStatus
= Status
;
312 ErrorOccurred
= TRUE
;
316 /* Add Compatible IDs string */
317 Status
= PciCreateCompatibleIDsString(&PdoDeviceExtension
->CompatibleIDs
, Device
);
318 if (!NT_SUCCESS(Status
))
320 ErrorStatus
= Status
;
321 ErrorOccurred
= TRUE
;
325 /* Add device description string */
326 Status
= PciCreateDeviceDescriptionString(&PdoDeviceExtension
->DeviceDescription
, Device
);
327 if (!NT_SUCCESS(Status
))
329 ErrorStatus
= Status
;
330 ErrorOccurred
= TRUE
;
334 /* Add device location string */
335 Status
= PciCreateDeviceLocationString(&PdoDeviceExtension
->DeviceLocation
, Device
);
336 if (!NT_SUCCESS(Status
))
338 ErrorStatus
= Status
;
339 ErrorOccurred
= TRUE
;
344 /* Reference the physical device object. The PnP manager
345 will dereference it again when it is no longer needed */
346 ObReferenceObject(Device
->Pdo
);
348 Relations
->Objects
[i
] = Device
->Pdo
;
352 CurrentEntry
= CurrentEntry
->Flink
;
356 /* FIXME: Cleanup all new PDOs created in this call. Please give me SEH!!! ;-) */
357 /* FIXME: Should IoAttachDeviceToDeviceStack() be undone? */
358 if (PdoDeviceExtension
) {
359 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceID
);
360 RtlFreeUnicodeString(&PdoDeviceExtension
->InstanceID
);
361 RtlFreeUnicodeString(&PdoDeviceExtension
->HardwareIDs
);
362 RtlFreeUnicodeString(&PdoDeviceExtension
->CompatibleIDs
);
363 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceDescription
);
364 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceLocation
);
367 ExFreePool(Relations
);
371 Irp
->IoStatus
.Information
= (ULONG_PTR
)Relations
;
381 IN PDEVICE_OBJECT DeviceObject
,
384 PFDO_DEVICE_EXTENSION DeviceExtension
;
385 PCM_RESOURCE_LIST AllocatedResources
;
386 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
387 ULONG FoundBusNumber
= FALSE
;
392 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
394 AllocatedResources
= IoGetCurrentIrpStackLocation(Irp
)->Parameters
.StartDevice
.AllocatedResources
;
395 if (!AllocatedResources
)
397 DPRINT("No allocated resources sent to driver\n");
398 return STATUS_INSUFFICIENT_RESOURCES
;
400 if (AllocatedResources
->Count
< 1)
402 DPRINT("Not enough allocated resources sent to driver\n");
403 return STATUS_INSUFFICIENT_RESOURCES
;
405 if (AllocatedResources
->List
[0].PartialResourceList
.Version
!= 1
406 || AllocatedResources
->List
[0].PartialResourceList
.Revision
!= 1)
407 return STATUS_REVISION_MISMATCH
;
409 ASSERT(DeviceExtension
->State
== dsStopped
);
411 /* By default, use the bus number in the resource list header */
412 DeviceExtension
->BusNumber
= AllocatedResources
->List
[0].BusNumber
;
414 for (i
= 0; i
< AllocatedResources
->List
[0].PartialResourceList
.Count
; i
++)
416 ResourceDescriptor
= &AllocatedResources
->List
[0].PartialResourceList
.PartialDescriptors
[i
];
417 switch (ResourceDescriptor
->Type
)
419 case CmResourceTypeBusNumber
:
421 if (FoundBusNumber
|| ResourceDescriptor
->u
.BusNumber
.Length
!= 1)
422 return STATUS_INVALID_PARAMETER
;
423 /* Use this one instead */
424 ASSERT(AllocatedResources
->List
[0].BusNumber
== ResourceDescriptor
->u
.BusNumber
.Start
);
425 DeviceExtension
->BusNumber
= ResourceDescriptor
->u
.BusNumber
.Start
;
426 DPRINT("Found bus number resource: %lu\n", DeviceExtension
->BusNumber
);
427 FoundBusNumber
= TRUE
;
431 DPRINT("Unknown resource descriptor type 0x%x\n", ResourceDescriptor
->Type
);
435 InitializeListHead(&DeviceExtension
->DeviceListHead
);
436 KeInitializeSpinLock(&DeviceExtension
->DeviceListLock
);
437 DeviceExtension
->DeviceListCount
= 0;
438 DeviceExtension
->State
= dsStarted
;
440 ExInterlockedInsertTailList(
441 &DriverExtension
->BusListHead
,
442 &DeviceExtension
->ListEntry
,
443 &DriverExtension
->BusListLock
);
445 Irp
->IoStatus
.Information
= 0;
447 return STATUS_SUCCESS
;
453 IN PDEVICE_OBJECT DeviceObject
,
455 PIO_STACK_LOCATION IrpSp
)
459 UNREFERENCED_PARAMETER(Irp
);
463 if (IrpSp
->Parameters
.Power
.Type
== DevicePowerState
) {
464 /* FIXME: Set device power state for the device */
465 Status
= STATUS_UNSUCCESSFUL
;
467 Status
= STATUS_UNSUCCESSFUL
;
474 /*** PUBLIC ******************************************************************/
478 PDEVICE_OBJECT DeviceObject
,
481 * FUNCTION: Handle Plug and Play IRPs for the PCI device object
483 * DeviceObject = Pointer to functional device object of the PCI driver
484 * Irp = Pointer to IRP that should be handled
489 PFDO_DEVICE_EXTENSION DeviceExtension
;
490 PIO_STACK_LOCATION IrpSp
;
491 NTSTATUS Status
= Irp
->IoStatus
.Status
;
495 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
497 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
498 switch (IrpSp
->MinorFunction
) {
500 case IRP_MN_CANCEL_REMOVE_DEVICE
:
501 Status
= STATUS_NOT_IMPLEMENTED
;
504 case IRP_MN_CANCEL_STOP_DEVICE
:
505 Status
= STATUS_NOT_IMPLEMENTED
;
508 case IRP_MN_DEVICE_USAGE_NOTIFICATION
:
509 Status
= STATUS_NOT_IMPLEMENTED
;
512 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
513 Status
= STATUS_NOT_IMPLEMENTED
;
516 case IRP_MN_QUERY_DEVICE_RELATIONS
:
517 if (IrpSp
->Parameters
.QueryDeviceRelations
.Type
!= BusRelations
)
520 Status
= FdoQueryBusRelations(DeviceObject
, Irp
, IrpSp
);
521 Irp
->IoStatus
.Status
= Status
;
522 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
525 case IRP_MN_QUERY_PNP_DEVICE_STATE
:
526 Status
= STATUS_NOT_IMPLEMENTED
;
529 case IRP_MN_QUERY_REMOVE_DEVICE
:
530 Status
= STATUS_NOT_IMPLEMENTED
;
533 case IRP_MN_START_DEVICE
:
534 DPRINT("IRP_MN_START_DEVICE received\n");
535 Status
= ForwardIrpAndWait(DeviceObject
, Irp
);
536 if (NT_SUCCESS(Status
))
537 Status
= FdoStartDevice(DeviceObject
, Irp
);
539 Irp
->IoStatus
.Status
= Status
;
540 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
543 case IRP_MN_QUERY_STOP_DEVICE
:
544 /* We don't support stopping yet */
545 Status
= STATUS_UNSUCCESSFUL
;
546 Irp
->IoStatus
.Status
= Status
;
547 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
550 case IRP_MN_STOP_DEVICE
:
551 /* We can't fail this one so we fail the QUERY_STOP request that precedes it */
554 case IRP_MN_SURPRISE_REMOVAL
:
555 Status
= STATUS_NOT_IMPLEMENTED
;
558 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
560 case IRP_MN_REMOVE_DEVICE
:
561 /* Detach the device object from the device stack */
562 IoDetachDevice(DeviceExtension
->Ldo
);
564 /* Delete the device object */
565 IoDeleteDevice(DeviceObject
);
568 Status
= STATUS_SUCCESS
;
571 DPRINT1("Unknown IOCTL 0x%lx\n", IrpSp
->MinorFunction
);
575 Irp
->IoStatus
.Status
= Status
;
576 IoSkipCurrentIrpStackLocation(Irp
);
577 Status
= IoCallDriver(DeviceExtension
->Ldo
, Irp
);
579 DPRINT("Leaving. Status 0x%X\n", Status
);
587 PDEVICE_OBJECT DeviceObject
,
590 * FUNCTION: Handle power management IRPs for the PCI device object
592 * DeviceObject = Pointer to functional device object of the PCI driver
593 * Irp = Pointer to IRP that should be handled
598 PIO_STACK_LOCATION IrpSp
;
603 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
605 switch (IrpSp
->MinorFunction
) {
606 case IRP_MN_SET_POWER
:
607 Status
= FdoSetPower(DeviceObject
, Irp
, IrpSp
);
611 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
612 Status
= STATUS_NOT_IMPLEMENTED
;
616 if (Status
!= STATUS_PENDING
) {
617 Irp
->IoStatus
.Status
= Status
;
618 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
621 DPRINT("Leaving. Status 0x%X\n", Status
);