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_BUS_ENUMERATED_DEVICE
;
244 Device
->Pdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
246 //Device->Pdo->Flags |= DO_POWER_PAGABLE;
248 PdoDeviceExtension
= (PPDO_DEVICE_EXTENSION
)Device
->Pdo
->DeviceExtension
;
250 RtlZeroMemory(PdoDeviceExtension
, sizeof(PDO_DEVICE_EXTENSION
));
252 PdoDeviceExtension
->Common
.IsFDO
= FALSE
;
254 PdoDeviceExtension
->Common
.DeviceObject
= Device
->Pdo
;
256 PdoDeviceExtension
->Common
.DevicePowerState
= PowerDeviceD0
;
258 PdoDeviceExtension
->Fdo
= DeviceObject
;
260 PdoDeviceExtension
->PciDevice
= Device
;
262 /* Add Device ID string */
263 Status
= PciCreateDeviceIDString(&PdoDeviceExtension
->DeviceID
, Device
);
264 if (!NT_SUCCESS(Status
))
266 ErrorStatus
= Status
;
267 ErrorOccurred
= TRUE
;
271 DPRINT("DeviceID: %S\n", PdoDeviceExtension
->DeviceID
.Buffer
);
273 /* Add Instance ID string */
274 Status
= PciCreateInstanceIDString(&PdoDeviceExtension
->InstanceID
, Device
);
275 if (!NT_SUCCESS(Status
))
277 ErrorStatus
= Status
;
278 ErrorOccurred
= TRUE
;
282 /* Add Hardware IDs string */
283 Status
= PciCreateHardwareIDsString(&PdoDeviceExtension
->HardwareIDs
, Device
);
284 if (!NT_SUCCESS(Status
))
286 ErrorStatus
= Status
;
287 ErrorOccurred
= TRUE
;
291 /* Add Compatible IDs string */
292 Status
= PciCreateCompatibleIDsString(&PdoDeviceExtension
->CompatibleIDs
, Device
);
293 if (!NT_SUCCESS(Status
))
295 ErrorStatus
= Status
;
296 ErrorOccurred
= TRUE
;
300 /* Add device description string */
301 Status
= PciCreateDeviceDescriptionString(&PdoDeviceExtension
->DeviceDescription
, Device
);
302 if (!NT_SUCCESS(Status
))
304 ErrorStatus
= Status
;
305 ErrorOccurred
= TRUE
;
309 /* Add device location string */
310 Status
= PciCreateDeviceLocationString(&PdoDeviceExtension
->DeviceLocation
, Device
);
311 if (!NT_SUCCESS(Status
))
313 ErrorStatus
= Status
;
314 ErrorOccurred
= TRUE
;
319 if (!Device
->RemovePending
) {
320 /* Reference the physical device object. The PnP manager
321 will dereference it again when it is no longer needed */
322 ObReferenceObject(Device
->Pdo
);
324 Relations
->Objects
[i
] = Device
->Pdo
;
329 CurrentEntry
= CurrentEntry
->Flink
;
333 /* FIXME: Cleanup all new PDOs created in this call. Please give me SEH!!! ;-) */
334 /* FIXME: Should IoAttachDeviceToDeviceStack() be undone? */
335 if (PdoDeviceExtension
) {
336 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceID
);
337 RtlFreeUnicodeString(&PdoDeviceExtension
->InstanceID
);
338 RtlFreeUnicodeString(&PdoDeviceExtension
->HardwareIDs
);
339 RtlFreeUnicodeString(&PdoDeviceExtension
->CompatibleIDs
);
340 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceDescription
);
341 RtlFreeUnicodeString(&PdoDeviceExtension
->DeviceLocation
);
344 ExFreePool(Relations
);
348 Irp
->IoStatus
.Information
= (ULONG_PTR
)Relations
;
358 IN PDEVICE_OBJECT DeviceObject
,
361 PFDO_DEVICE_EXTENSION DeviceExtension
;
362 PCM_RESOURCE_LIST AllocatedResources
;
363 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
364 ULONG FoundBusNumber
= FALSE
;
369 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
371 AllocatedResources
= IoGetCurrentIrpStackLocation(Irp
)->Parameters
.StartDevice
.AllocatedResources
;
372 if (!AllocatedResources
)
374 DPRINT("No allocated resources sent to driver\n");
375 return STATUS_INSUFFICIENT_RESOURCES
;
377 if (AllocatedResources
->Count
< 1)
379 DPRINT("Not enough allocated resources sent to driver\n");
380 return STATUS_INSUFFICIENT_RESOURCES
;
382 if (AllocatedResources
->List
[0].PartialResourceList
.Version
!= 1
383 || AllocatedResources
->List
[0].PartialResourceList
.Revision
!= 1)
384 return STATUS_REVISION_MISMATCH
;
386 ASSERT(DeviceExtension
->State
== dsStopped
);
388 /* By default, use the bus number in the resource list header */
389 DeviceExtension
->BusNumber
= AllocatedResources
->List
[0].BusNumber
;
391 for (i
= 0; i
< AllocatedResources
->List
[0].PartialResourceList
.Count
; i
++)
393 ResourceDescriptor
= &AllocatedResources
->List
[0].PartialResourceList
.PartialDescriptors
[i
];
394 switch (ResourceDescriptor
->Type
)
396 case CmResourceTypeBusNumber
:
398 if (FoundBusNumber
|| ResourceDescriptor
->u
.BusNumber
.Length
!= 1)
399 return STATUS_INVALID_PARAMETER
;
400 /* Use this one instead */
401 ASSERT(AllocatedResources
->List
[0].BusNumber
== ResourceDescriptor
->u
.BusNumber
.Start
);
402 DeviceExtension
->BusNumber
= ResourceDescriptor
->u
.BusNumber
.Start
;
403 DPRINT("Found bus number resource: %lu\n", DeviceExtension
->BusNumber
);
404 FoundBusNumber
= TRUE
;
408 DPRINT("Unknown resource descriptor type 0x%x\n", ResourceDescriptor
->Type
);
412 InitializeListHead(&DeviceExtension
->DeviceListHead
);
413 KeInitializeSpinLock(&DeviceExtension
->DeviceListLock
);
414 DeviceExtension
->DeviceListCount
= 0;
415 DeviceExtension
->State
= dsStarted
;
417 ExInterlockedInsertTailList(
418 &DriverExtension
->BusListHead
,
419 &DeviceExtension
->ListEntry
,
420 &DriverExtension
->BusListLock
);
422 Irp
->IoStatus
.Information
= 0;
424 return STATUS_SUCCESS
;
430 IN PDEVICE_OBJECT DeviceObject
,
432 PIO_STACK_LOCATION IrpSp
)
434 PFDO_DEVICE_EXTENSION DeviceExtension
;
437 UNREFERENCED_PARAMETER(Irp
);
441 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
443 if (IrpSp
->Parameters
.Power
.Type
== DevicePowerState
) {
444 /* FIXME: Set device power state for the device */
445 Status
= STATUS_UNSUCCESSFUL
;
447 Status
= STATUS_UNSUCCESSFUL
;
454 /*** PUBLIC ******************************************************************/
458 PDEVICE_OBJECT DeviceObject
,
461 * FUNCTION: Handle Plug and Play IRPs for the PCI device object
463 * DeviceObject = Pointer to functional device object of the PCI driver
464 * Irp = Pointer to IRP that should be handled
469 PFDO_DEVICE_EXTENSION DeviceExtension
;
470 PIO_STACK_LOCATION IrpSp
;
475 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
477 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
478 switch (IrpSp
->MinorFunction
) {
480 case IRP_MN_CANCEL_REMOVE_DEVICE
:
481 Status
= STATUS_NOT_IMPLEMENTED
;
484 case IRP_MN_CANCEL_STOP_DEVICE
:
485 Status
= STATUS_NOT_IMPLEMENTED
;
488 case IRP_MN_DEVICE_USAGE_NOTIFICATION
:
489 Status
= STATUS_NOT_IMPLEMENTED
;
492 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
493 Status
= STATUS_NOT_IMPLEMENTED
;
496 case IRP_MN_QUERY_DEVICE_RELATIONS
:
497 Status
= FdoQueryBusRelations(DeviceObject
, Irp
, IrpSp
);
500 case IRP_MN_QUERY_PNP_DEVICE_STATE
:
501 Status
= STATUS_NOT_IMPLEMENTED
;
504 case IRP_MN_QUERY_REMOVE_DEVICE
:
505 Status
= STATUS_NOT_IMPLEMENTED
;
508 case IRP_MN_QUERY_STOP_DEVICE
:
509 Status
= STATUS_NOT_IMPLEMENTED
;
512 case IRP_MN_REMOVE_DEVICE
:
513 Status
= STATUS_NOT_IMPLEMENTED
;
516 case IRP_MN_START_DEVICE
:
517 DPRINT("IRP_MN_START_DEVICE received\n");
518 Status
= FdoStartDevice(DeviceObject
, Irp
);
520 case IRP_MN_STOP_DEVICE
:
521 /* Currently not supported */
522 Status
= STATUS_UNSUCCESSFUL
;
525 case IRP_MN_SURPRISE_REMOVAL
:
526 Status
= STATUS_NOT_IMPLEMENTED
;
530 DPRINT1("Unknown IOCTL 0x%lx\n", IrpSp
->MinorFunction
);
533 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS
:
535 * Do NOT complete the IRP as it will be processed by the lower
536 * device object, which will complete the IRP
538 IoSkipCurrentIrpStackLocation(Irp
);
539 Status
= IoCallDriver(DeviceExtension
->Ldo
, Irp
);
545 if (Status
!= STATUS_PENDING
) {
546 if (Status
!= STATUS_NOT_IMPLEMENTED
)
547 Irp
->IoStatus
.Status
= Status
;
548 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
551 DPRINT("Leaving. Status 0x%X\n", Status
);
559 PDEVICE_OBJECT DeviceObject
,
562 * FUNCTION: Handle power management IRPs for the PCI device object
564 * DeviceObject = Pointer to functional device object of the PCI driver
565 * Irp = Pointer to IRP that should be handled
570 PIO_STACK_LOCATION IrpSp
;
575 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
577 switch (IrpSp
->MinorFunction
) {
578 case IRP_MN_SET_POWER
:
579 Status
= FdoSetPower(DeviceObject
, Irp
, IrpSp
);
583 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->MinorFunction
);
584 Status
= STATUS_NOT_IMPLEMENTED
;
588 if (Status
!= STATUS_PENDING
) {
589 Irp
->IoStatus
.Status
= Status
;
590 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
593 DPRINT("Leaving. Status 0x%X\n", Status
);