1 /* $Id: pci.c,v 1.7 2004/06/09 14:22:53 ekohl Exp $
3 * PROJECT: ReactOS PCI Bus driver
5 * PURPOSE: Driver entry
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * 10-09-2001 CSH Created
11 #include <ddk/ntddk.h>
22 // Make the initialization routines discardable, so that they
25 #pragma alloc_text(init, DriverEntry)
27 #endif /* ALLOC_PRAGMA */
29 /*** PUBLIC ******************************************************************/
32 /*** PRIVATE *****************************************************************/
36 PciDispatchDeviceControl(
37 IN PDEVICE_OBJECT DeviceObject
,
40 PIO_STACK_LOCATION IrpSp
;
43 DPRINT("Called. IRP is at (0x%X)\n", Irp
);
45 Irp
->IoStatus
.Information
= 0;
47 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
48 switch (IrpSp
->Parameters
.DeviceIoControl
.IoControlCode
) {
50 DPRINT("Unknown IOCTL 0x%X\n", IrpSp
->Parameters
.DeviceIoControl
.IoControlCode
);
51 Status
= STATUS_NOT_IMPLEMENTED
;
55 if (Status
!= STATUS_PENDING
) {
56 Irp
->IoStatus
.Status
= Status
;
58 DPRINT("Completing IRP at 0x%X\n", Irp
);
60 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
63 DPRINT("Leaving. Status 0x%X\n", Status
);
72 IN PDEVICE_OBJECT DeviceObject
,
75 * FUNCTION: Handle Plug and Play IRPs
77 * DeviceObject = Pointer to PDO or FDO
78 * Irp = Pointer to IRP that should be handled
83 PCOMMON_DEVICE_EXTENSION DeviceExtension
;
86 DeviceExtension
= (PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
88 DPRINT("IsFDO %d\n", DeviceExtension
->IsFDO
);
90 if (DeviceExtension
->IsFDO
) {
91 Status
= FdoPnpControl(DeviceObject
, Irp
);
93 Status
= PdoPnpControl(DeviceObject
, Irp
);
103 IN PDEVICE_OBJECT DeviceObject
,
106 * FUNCTION: Handle power management IRPs
108 * DeviceObject = Pointer to PDO or FDO
109 * Irp = Pointer to IRP that should be handled
114 PCOMMON_DEVICE_EXTENSION DeviceExtension
;
117 DeviceExtension
= (PCOMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
119 if (DeviceExtension
->IsFDO
) {
120 Status
= FdoPowerControl(DeviceObject
, Irp
);
122 Status
= PdoPowerControl(DeviceObject
, Irp
);
132 IN PDRIVER_OBJECT DriverObject
,
133 IN PDEVICE_OBJECT PhysicalDeviceObject
)
135 PFDO_DEVICE_EXTENSION DeviceExtension
;
141 Status
= IoCreateDevice(DriverObject
, sizeof(FDO_DEVICE_EXTENSION
),
142 NULL
, FILE_DEVICE_BUS_EXTENDER
, FILE_DEVICE_SECURE_OPEN
, TRUE
, &Fdo
);
143 if (!NT_SUCCESS(Status
)) {
144 DPRINT("IoCreateDevice() failed with status 0x%X\n", Status
);
148 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)Fdo
->DeviceExtension
;
150 RtlZeroMemory(DeviceExtension
, sizeof(FDO_DEVICE_EXTENSION
));
152 DeviceExtension
->Common
.IsFDO
= TRUE
;
154 DeviceExtension
->Ldo
=
155 IoAttachDeviceToDeviceStack(Fdo
, PhysicalDeviceObject
);
157 DeviceExtension
->State
= dsStopped
;
159 Fdo
->Flags
&= ~DO_DEVICE_INITIALIZING
;
161 //Fdo->Flags |= DO_POWER_PAGABLE;
163 DPRINT("Done AddDevice\n");
165 return STATUS_SUCCESS
;
172 IN PDRIVER_OBJECT DriverObject
,
173 IN PUNICODE_STRING RegistryPath
)
175 DbgPrint("Peripheral Component Interconnect Bus Driver\n");
177 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = PciDispatchDeviceControl
;
178 DriverObject
->MajorFunction
[IRP_MJ_PNP
] = PciPnpControl
;
179 DriverObject
->MajorFunction
[IRP_MJ_POWER
] = PciPowerControl
;
180 DriverObject
->DriverExtension
->AddDevice
= PciAddDevice
;
182 return STATUS_SUCCESS
;
187 PciCreateUnicodeString(
188 PUNICODE_STRING Destination
,
196 RtlInitUnicodeString(Destination
, NULL
);
200 Length
= (wcslen(Source
) + 1) * sizeof(WCHAR
);
202 Destination
->Buffer
= ExAllocatePool(PoolType
, Length
);
204 if (Destination
->Buffer
== NULL
)
209 RtlCopyMemory(Destination
->Buffer
, Source
, Length
);
211 Destination
->MaximumLength
= Length
;
213 Destination
->Length
= Length
- sizeof(WCHAR
);
220 PciDuplicateUnicodeString(
221 PUNICODE_STRING Destination
,
222 PUNICODE_STRING Source
,
227 RtlInitUnicodeString(Destination
, NULL
);
228 return STATUS_SUCCESS
;
231 Destination
->Buffer
= ExAllocatePool(PoolType
, Source
->MaximumLength
);
232 if (Destination
->Buffer
== NULL
)
234 return STATUS_INSUFFICIENT_RESOURCES
;
237 Destination
->MaximumLength
= Source
->MaximumLength
;
238 Destination
->Length
= Source
->Length
;
239 RtlCopyMemory(Destination
->Buffer
, Source
->Buffer
, Source
->MaximumLength
);
241 return STATUS_SUCCESS
;
246 PciCreateDeviceIDString(PUNICODE_STRING DeviceID
,
252 L
"PCI\\VEN_%04X&DEV_%04X&SUBSYS_%08X&REV_%02X",
253 Device
->PciConfig
.VendorID
,
254 Device
->PciConfig
.DeviceID
,
255 (Device
->PciConfig
.u
.type0
.SubSystemID
<< 16) +
256 Device
->PciConfig
.u
.type0
.SubVendorID
,
257 Device
->PciConfig
.RevisionID
);
259 if (!PciCreateUnicodeString(DeviceID
, Buffer
, PagedPool
))
269 PciCreateInstanceIDString(PUNICODE_STRING DeviceID
,
278 Device
->SlotNumber
.SlotNumber
.u
.AsULONG
);
281 return PciCreateUnicodeString(DeviceID
, L
"0000", PagedPool
);
286 PciCreateHardwareIDsString(PUNICODE_STRING HardwareIDs
,
294 Index
+= swprintf(&Buffer
[Index
],
295 L
"PCI\\VEN_%04X&DEV_%04X&SUBSYS_%08X&REV_%02X",
296 Device
->PciConfig
.VendorID
,
297 Device
->PciConfig
.DeviceID
,
298 (Device
->PciConfig
.u
.type0
.SubSystemID
<< 16) +
299 Device
->PciConfig
.u
.type0
.SubVendorID
,
300 Device
->PciConfig
.RevisionID
);
303 Index
+= swprintf(&Buffer
[Index
],
304 L
"PCI\\VEN_%04X&DEV_%04X&SUBSYS_%08X",
305 Device
->PciConfig
.VendorID
,
306 Device
->PciConfig
.DeviceID
,
307 (Device
->PciConfig
.u
.type0
.SubSystemID
<< 16) +
308 Device
->PciConfig
.u
.type0
.SubVendorID
);
311 Buffer
[Index
] = UNICODE_NULL
;
313 Length
= (Index
+ 1) * sizeof(WCHAR
);
314 HardwareIDs
->Buffer
= ExAllocatePool(PagedPool
, Length
);
320 HardwareIDs
->Length
= Length
- sizeof(WCHAR
);
321 HardwareIDs
->MaximumLength
= Length
;
322 RtlCopyMemory(HardwareIDs
->Buffer
, Buffer
, Length
);
329 PciCreateCompatibleIDsString(PUNICODE_STRING HardwareIDs
,
337 Index
+= swprintf(&Buffer
[Index
],
338 L
"PCI\\VEN_%04X&DEV_%04X&REV_%02X&CC_%02X%02X",
339 Device
->PciConfig
.VendorID
,
340 Device
->PciConfig
.DeviceID
,
341 Device
->PciConfig
.RevisionID
,
342 Device
->PciConfig
.BaseClass
,
343 Device
->PciConfig
.SubClass
);
346 Index
+= swprintf(&Buffer
[Index
],
347 L
"PCI\\VEN_%04X&DEV_%04X&CC_%02X%02X%02X",
348 Device
->PciConfig
.VendorID
,
349 Device
->PciConfig
.DeviceID
,
350 Device
->PciConfig
.BaseClass
,
351 Device
->PciConfig
.SubClass
,
352 Device
->PciConfig
.ProgIf
);
355 Index
+= swprintf(&Buffer
[Index
],
356 L
"PCI\\VEN_%04X&DEV_%04X&CC_%02X%02X",
357 Device
->PciConfig
.VendorID
,
358 Device
->PciConfig
.DeviceID
,
359 Device
->PciConfig
.BaseClass
,
360 Device
->PciConfig
.SubClass
);
363 Index
+= swprintf(&Buffer
[Index
],
364 L
"PCI\\VEN_%04X&CC_%02X%02X%02X",
365 Device
->PciConfig
.VendorID
,
366 Device
->PciConfig
.BaseClass
,
367 Device
->PciConfig
.SubClass
,
368 Device
->PciConfig
.ProgIf
);
371 Index
+= swprintf(&Buffer
[Index
],
372 L
"PCI\\VEN_%04X&CC_%02X%02X",
373 Device
->PciConfig
.VendorID
,
374 Device
->PciConfig
.BaseClass
,
375 Device
->PciConfig
.SubClass
);
378 Index
+= swprintf(&Buffer
[Index
],
380 Device
->PciConfig
.VendorID
);
383 Index
+= swprintf(&Buffer
[Index
],
384 L
"PCI\\CC_%02X%02X%02X",
385 Device
->PciConfig
.BaseClass
,
386 Device
->PciConfig
.SubClass
,
387 Device
->PciConfig
.ProgIf
);
390 Index
+= swprintf(&Buffer
[Index
],
392 Device
->PciConfig
.BaseClass
,
393 Device
->PciConfig
.SubClass
);
396 Buffer
[Index
] = UNICODE_NULL
;
398 Length
= (Index
+ 1) * sizeof(WCHAR
);
399 HardwareIDs
->Buffer
= ExAllocatePool(PagedPool
, Length
);
405 HardwareIDs
->Length
= Length
- sizeof(WCHAR
);
406 HardwareIDs
->MaximumLength
= Length
;
407 RtlCopyMemory(HardwareIDs
->Buffer
, Buffer
, Length
);