Implement InterfacePciDevicePresent(Ex) of PCI_DEVICE_PRESENT_INTERFACE
[reactos.git] / reactos / drivers / bus / pci / pci.c
index 85970fb..34d0cbd 100644 (file)
@@ -1,5 +1,4 @@
-/* $Id: pci.c,v 1.9 2004/08/20 13:33:51 ekohl Exp $
- *
+/*
  * PROJECT:         ReactOS PCI Bus driver
  * FILE:            pci.c
  * PURPOSE:         Driver entry
@@ -9,6 +8,8 @@
  */
 
 #include <ddk/ntddk.h>
+#include <ddk/ntifs.h>
+#include <stdio.h>
 
 #include "pcidef.h"
 #include "pci.h"
@@ -19,7 +20,7 @@
 
 #ifdef  ALLOC_PRAGMA
 
-// Make the initialization routines discardable, so that they 
+// Make the initialization routines discardable, so that they
 // don't waste space
 
 #pragma  alloc_text(init, DriverEntry)
 
 /*** PUBLIC ******************************************************************/
 
+PPCI_DRIVER_EXTENSION DriverExtension = NULL;
 
 /*** PRIVATE *****************************************************************/
 
-NTSTATUS
+static NTSTATUS
 STDCALL
 PciDispatchDeviceControl(
-  IN PDEVICE_OBJECT DeviceObject, 
-  IN PIRP Irp) 
+  IN PDEVICE_OBJECT DeviceObject,
+  IN PIRP Irp)
 {
   PIO_STACK_LOCATION IrpSp;
   NTSTATUS Status;
@@ -66,7 +68,7 @@ PciDispatchDeviceControl(
 }
 
 
-NTSTATUS
+static NTSTATUS
 STDCALL
 PciPnpControl(
   IN PDEVICE_OBJECT DeviceObject,
@@ -97,7 +99,7 @@ PciPnpControl(
 }
 
 
-NTSTATUS
+static NTSTATUS
 STDCALL
 PciPowerControl(
   IN PDEVICE_OBJECT DeviceObject,
@@ -126,7 +128,7 @@ PciPowerControl(
 }
 
 
-NTSTATUS
+static NTSTATUS
 STDCALL
 PciAddDevice(
   IN PDRIVER_OBJECT DriverObject,
@@ -172,77 +174,32 @@ DriverEntry(
   IN PDRIVER_OBJECT DriverObject,
   IN PUNICODE_STRING RegistryPath)
 {
-  DbgPrint("Peripheral Component Interconnect Bus Driver\n");
+  NTSTATUS Status;
+
+  DPRINT("Peripheral Component Interconnect Bus Driver\n");
 
   DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = PciDispatchDeviceControl;
   DriverObject->MajorFunction[IRP_MJ_PNP] = PciPnpControl;
   DriverObject->MajorFunction[IRP_MJ_POWER] = PciPowerControl;
   DriverObject->DriverExtension->AddDevice = PciAddDevice;
 
-  return STATUS_SUCCESS;
-}
-
-
-BOOLEAN
-PciCreateUnicodeString(
-  PUNICODE_STRING Destination,
-  PWSTR Source,
-  POOL_TYPE PoolType)
-{
-  ULONG Length;
-
-  if (!Source)
-  {
-    RtlInitUnicodeString(Destination, NULL);
-    return TRUE;
-  }
-
-  Length = (wcslen(Source) + 1) * sizeof(WCHAR);
-
-  Destination->Buffer = ExAllocatePool(PoolType, Length);
-
-  if (Destination->Buffer == NULL)
-  {
-    return FALSE;
-  }
-
-  RtlCopyMemory(Destination->Buffer, Source, Length);
-
-  Destination->MaximumLength = Length;
-
-  Destination->Length = Length - sizeof(WCHAR);
-
-  return TRUE;
-}
-
-
-NTSTATUS
-PciDuplicateUnicodeString(
-  PUNICODE_STRING Destination,
-  PUNICODE_STRING Source,
-  POOL_TYPE PoolType)
-{
-  if (Source == NULL)
-  {
-    RtlInitUnicodeString(Destination, NULL);
-    return STATUS_SUCCESS;
-  }
-
-  Destination->Buffer = ExAllocatePool(PoolType, Source->MaximumLength);
-  if (Destination->Buffer == NULL)
-  {
-    return STATUS_INSUFFICIENT_RESOURCES;
-  }
+  Status = IoAllocateDriverObjectExtension(
+    DriverObject,
+    DriverObject,
+    sizeof(PCI_DRIVER_EXTENSION),
+    (PVOID*)&DriverExtension);
+  if (!NT_SUCCESS(Status))
+    return Status;
+  RtlZeroMemory(DriverExtension, sizeof(PCI_DRIVER_EXTENSION));
 
-  Destination->MaximumLength = Source->MaximumLength;
-  Destination->Length = Source->Length;
-  RtlCopyMemory(Destination->Buffer, Source->Buffer, Source->MaximumLength);
+  InitializeListHead(&DriverExtension->BusListHead);
+  KeInitializeSpinLock(&DriverExtension->BusListLock);
 
   return STATUS_SUCCESS;
 }
 
 
-BOOLEAN
+NTSTATUS
 PciCreateDeviceIDString(PUNICODE_STRING DeviceID,
                         PPCI_DEVICE Device)
 {
@@ -256,55 +213,36 @@ PciCreateDeviceIDString(PUNICODE_STRING DeviceID,
            Device->PciConfig.u.type0.SubVendorID,
            Device->PciConfig.RevisionID);
 
-  if (!PciCreateUnicodeString(DeviceID, Buffer, PagedPool))
-  {
-    return FALSE;
-  }
-
-  return TRUE;
+  return RtlCreateUnicodeString(DeviceID, Buffer) ? STATUS_SUCCESS : STATUS_INSUFFICIENT_RESOURCES;
 }
 
 
-BOOLEAN
+NTSTATUS
 PciCreateInstanceIDString(PUNICODE_STRING InstanceID,
                           PPCI_DEVICE Device)
 {
-#if 0
   WCHAR Buffer[32];
-  ULONG Length;
   ULONG Index;
 
-  Index = swprintf(Buffer,
-                   L"%lX&%02lX",
-                   Device->BusNumber,
-                   (Device->SlotNumber.u.bits.DeviceNumber << 3) +
-                   Device->SlotNumber.u.bits.FunctionNumber);
-  Index++;
-  Buffer[Index] = UNICODE_NULL;
-
-  Length = (Index + 1) * sizeof(WCHAR);
-  InstanceID->Buffer = ExAllocatePool(PagedPool, Length);
-  if (InstanceID->Buffer == NULL)
+  Index = 0;
+  if (((PPDO_DEVICE_EXTENSION)Device->Pdo->DeviceExtension)->PciDevice->BusNumber != 0)
   {
-    return FALSE;
+    /* FIXME: Copy InstanceID of parent PCI bus to Buffer */
+    // Index += swprintf(Buffer, ....);
   }
 
-  InstanceID->Length = Length - sizeof(WCHAR);
-  InstanceID->MaximumLength = Length;
-  RtlCopyMemory(InstanceID->Buffer, Buffer, Length);
+  swprintf(&Buffer[Index], L"%02X", Device->SlotNumber.u.AsULONG & 0xff);
 
-  return TRUE;
-#endif
-  return PciCreateUnicodeString(InstanceID, L"0000", PagedPool);
+  return RtlCreateUnicodeString(InstanceID, Buffer) ? STATUS_SUCCESS : STATUS_INSUFFICIENT_RESOURCES;
 }
 
 
-BOOLEAN
+NTSTATUS
 PciCreateHardwareIDsString(PUNICODE_STRING HardwareIDs,
                            PPCI_DEVICE Device)
 {
   WCHAR Buffer[256];
-  ULONG Length;
+  UNICODE_STRING BufferU;
   ULONG Index;
 
   Index = 0;
@@ -325,56 +263,52 @@ PciCreateHardwareIDsString(PUNICODE_STRING HardwareIDs,
            Device->PciConfig.u.type0.SubVendorID);
   Index++;
 
-  Buffer[Index] = UNICODE_NULL;
+  Index += swprintf(&Buffer[Index],
+           L"PCI\\VEN_%04X&DEV_%04X&CC_%02X%02X%02X",
+           Device->PciConfig.VendorID,
+           Device->PciConfig.DeviceID,
+           Device->PciConfig.BaseClass,
+           Device->PciConfig.SubClass,
+           Device->PciConfig.ProgIf);
+  Index++;
 
-  Length = (Index + 1) * sizeof(WCHAR);
-  HardwareIDs->Buffer = ExAllocatePool(PagedPool, Length);
-  if (HardwareIDs->Buffer == NULL)
-  {
-    return FALSE;
-  }
+  Index += swprintf(&Buffer[Index],
+           L"PCI\\VEN_%04X&DEV_%04X&CC_%02X%02X",
+           Device->PciConfig.VendorID,
+           Device->PciConfig.DeviceID,
+           Device->PciConfig.BaseClass,
+           Device->PciConfig.SubClass);
+  Index++;
 
-  HardwareIDs->Length = Length - sizeof(WCHAR);
-  HardwareIDs->MaximumLength = Length;
-  RtlCopyMemory(HardwareIDs->Buffer, Buffer, Length);
+  Buffer[Index] = UNICODE_NULL;
+  
+  BufferU.Length = BufferU.MaximumLength = Index * sizeof(WCHAR);
+  BufferU.Buffer = Buffer;
 
-  return TRUE;
+  return RtlDuplicateUnicodeString(0, &BufferU, HardwareIDs);
 }
 
 
-BOOLEAN
+NTSTATUS
 PciCreateCompatibleIDsString(PUNICODE_STRING CompatibleIDs,
                              PPCI_DEVICE Device)
 {
   WCHAR Buffer[256];
-  ULONG Length;
+  UNICODE_STRING BufferU;
   ULONG Index;
 
   Index = 0;
   Index += swprintf(&Buffer[Index],
-           L"PCI\\VEN_%04X&DEV_%04X&REV_%02X&CC_%02X%02X",
+           L"PCI\\VEN_%04X&DEV_%04X&REV_%02X",
            Device->PciConfig.VendorID,
            Device->PciConfig.DeviceID,
-           Device->PciConfig.RevisionID,
-           Device->PciConfig.BaseClass,
-           Device->PciConfig.SubClass);
-  Index++;
-
-  Index += swprintf(&Buffer[Index],
-           L"PCI\\VEN_%04X&DEV_%04X&CC_%02X%02X%02X",
-           Device->PciConfig.VendorID,
-           Device->PciConfig.DeviceID,
-           Device->PciConfig.BaseClass,
-           Device->PciConfig.SubClass,
-           Device->PciConfig.ProgIf);
+           Device->PciConfig.RevisionID);
   Index++;
 
   Index += swprintf(&Buffer[Index],
-           L"PCI\\VEN_%04X&DEV_%04X&CC_%02X%02X",
+           L"PCI\\VEN_%04X&DEV_%04X",
            Device->PciConfig.VendorID,
-           Device->PciConfig.DeviceID,
-           Device->PciConfig.BaseClass,
-           Device->PciConfig.SubClass);
+           Device->PciConfig.DeviceID);
   Index++;
 
   Index += swprintf(&Buffer[Index],
@@ -412,27 +346,18 @@ PciCreateCompatibleIDsString(PUNICODE_STRING CompatibleIDs,
 
   Buffer[Index] = UNICODE_NULL;
 
-  Length = (Index + 1) * sizeof(WCHAR);
-  CompatibleIDs->Buffer = ExAllocatePool(PagedPool, Length);
-  if (CompatibleIDs->Buffer == NULL)
-  {
-    return FALSE;
-  }
+  BufferU.Length = BufferU.MaximumLength = Index * sizeof(WCHAR);
+  BufferU.Buffer = Buffer;
 
-  CompatibleIDs->Length = Length - sizeof(WCHAR);
-  CompatibleIDs->MaximumLength = Length;
-  RtlCopyMemory(CompatibleIDs->Buffer, Buffer, Length);
-
-  return TRUE;
+  return RtlDuplicateUnicodeString(0, &BufferU, CompatibleIDs);
 }
 
 
-BOOLEAN
+NTSTATUS
 PciCreateDeviceDescriptionString(PUNICODE_STRING DeviceDescription,
                                  PPCI_DEVICE Device)
 {
-  PWSTR Description;
-  ULONG Length;
+  PCWSTR Description;
 
   switch (Device->PciConfig.BaseClass)
   {
@@ -688,51 +613,23 @@ PciCreateDeviceDescriptionString(PUNICODE_STRING DeviceDescription,
       break;
   }
 
-  Length = (wcslen(Description) + 1) * sizeof(WCHAR);
-  DeviceDescription->Buffer = ExAllocatePool(PagedPool, Length);
-  if (DeviceDescription->Buffer == NULL)
-  {
-    return FALSE;
-  }
-
-  DeviceDescription->Length = Length - sizeof(WCHAR);
-  DeviceDescription->MaximumLength = Length;
-  RtlCopyMemory(DeviceDescription->Buffer, Description, Length);
-
-  return TRUE;
+  return RtlCreateUnicodeString(DeviceDescription, Description) ? STATUS_SUCCESS : STATUS_INSUFFICIENT_RESOURCES;
 }
 
 
-BOOLEAN
+NTSTATUS
 PciCreateDeviceLocationString(PUNICODE_STRING DeviceLocation,
                               PPCI_DEVICE Device)
 {
   WCHAR Buffer[256];
-  ULONG Length;
-  ULONG Index;
-
-  Index = 0;
-  Index += swprintf(&Buffer[Index],
-                    L"PCI-Bus %lu, Device %u, Function %u",
-                    Device->BusNumber,
-                    Device->SlotNumber.u.bits.DeviceNumber,
-                    Device->SlotNumber.u.bits.FunctionNumber);
-  Index++;
 
-  Buffer[Index] = UNICODE_NULL;
-
-  Length = (Index + 1) * sizeof(WCHAR);
-  DeviceLocation->Buffer = ExAllocatePool(PagedPool, Length);
-  if (DeviceLocation->Buffer == NULL)
-  {
-    return FALSE;
-  }
-
-  DeviceLocation->Length = Length - sizeof(WCHAR);
-  DeviceLocation->MaximumLength = Length;
-  RtlCopyMemory(DeviceLocation->Buffer, Buffer, Length);
+  swprintf(Buffer,
+           L"PCI-Bus %lu, Device %u, Function %u",
+           Device->BusNumber,
+           Device->SlotNumber.u.bits.DeviceNumber,
+           Device->SlotNumber.u.bits.FunctionNumber);
 
-  return TRUE;
+  return RtlCreateUnicodeString(DeviceLocation, Buffer) ? STATUS_SUCCESS : STATUS_INSUFFICIENT_RESOURCES;
 }
 
 /* EOF */