sync with trunk r46493
[reactos.git] / drivers / video / videoprt / resource.c
index 4341496..b17edb7 100644 (file)
@@ -4,19 +4,18 @@
  * Copyright (C) 2002 - 2005 ReactOS Team
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; see the file COPYING.LIB.
- * If not, write to the Free Software Foundation,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
@@ -164,7 +163,7 @@ IntVideoPortMapMemory(
       if (Status)
          *Status = NO_ERROR;
 
-      return (PVOID)TranslatedAddress.u.LowPart;
+      return (PVOID)(ULONG_PTR)TranslatedAddress.u.LowPart;
    }
 
    /* user space */
@@ -272,7 +271,7 @@ IntVideoPortUnmapMemory(
    Status = ZwUnmapViewOfSection(NtCurrentProcess(), MappedAddress);
    if (!NT_SUCCESS(Status))
    {
-      WARN_(VIDEOPRT, "Warning: Mapping for address 0x%x not found!\n", (ULONG)MappedAddress);
+      WARN_(VIDEOPRT, "Warning: Mapping for address 0x%p not found!\n", MappedAddress);
    }
 }
 
@@ -332,7 +331,7 @@ VideoPortMapBankedMemory(
 {
    TRACE_(VIDEOPRT, "VideoPortMapBankedMemory\n");
    UNIMPLEMENTED;
-   return ERROR_CALL_NOT_IMPLEMENTED;
+   return ERROR_INVALID_FUNCTION;
 }
 
 
@@ -401,6 +400,7 @@ VideoPortGetAccessRanges(
    IN PULONG Slot)
 {
    PCI_SLOT_NUMBER PciSlotNumber;
+   ULONG DeviceNumber;
    ULONG FunctionNumber;
    PCI_COMMON_CONFIG Config;
    PCM_RESOURCE_LIST AllocatedResources;
@@ -413,6 +413,7 @@ VideoPortGetAccessRanges(
    USHORT DeviceIdToFind;
    ULONG SlotIdToFind;
    ULONG ReturnedLength;
+   BOOLEAN DeviceAndVendorFound = FALSE;
 
    TRACE_(VIDEOPRT, "VideoPortGetAccessRanges\n");
 
@@ -437,7 +438,7 @@ VideoPortGetAccessRanges(
 
             if (ReturnedLength != sizeof(PCI_COMMON_CONFIG))
             {
-               return ERROR_NO_SYSTEM_RESOURCES;
+               return ERROR_NOT_ENOUGH_MEMORY;
             }
          }
          else
@@ -453,36 +454,40 @@ VideoPortGetAccessRanges(
             /*
              * Search for the device id and vendor id on this bus.
              */
-
-            for (FunctionNumber = 0; FunctionNumber < 8; FunctionNumber++)
+            for (DeviceNumber = 0; DeviceNumber < PCI_MAX_DEVICES; DeviceNumber++)
             {
-               INFO_(VIDEOPRT, "- Function number: %d\n", FunctionNumber);
-               PciSlotNumber.u.bits.FunctionNumber = FunctionNumber;
-               ReturnedLength = HalGetBusData(
-                  PCIConfiguration,
-                  DeviceExtension->SystemIoBusNumber,
-                  PciSlotNumber.u.AsULONG,
-                  &Config,
-                  sizeof(PCI_COMMON_CONFIG));
-               INFO_(VIDEOPRT, "- Length of data: %x\n", ReturnedLength);
-               if (ReturnedLength == sizeof(PCI_COMMON_CONFIG))
+               PciSlotNumber.u.bits.DeviceNumber = DeviceNumber;
+               for (FunctionNumber = 0; FunctionNumber < 8; FunctionNumber++)
                {
-                  INFO_(VIDEOPRT, "- Slot 0x%02x (Device %d Function %d) VendorId 0x%04x "
-                         "DeviceId 0x%04x\n",
-                         PciSlotNumber.u.AsULONG,
-                         PciSlotNumber.u.bits.DeviceNumber,
-                         PciSlotNumber.u.bits.FunctionNumber,
-                         Config.VendorID,
-                         Config.DeviceID);
-
-                  if ((VendorIdToFind == 0 || Config.VendorID == VendorIdToFind) &&
-                      (DeviceIdToFind == 0 || Config.DeviceID == DeviceIdToFind))
+                  INFO_(VIDEOPRT, "- Function number: %d\n", FunctionNumber);
+                  PciSlotNumber.u.bits.FunctionNumber = FunctionNumber;
+                  ReturnedLength = HalGetBusData(
+                     PCIConfiguration,
+                     DeviceExtension->SystemIoBusNumber,
+                     PciSlotNumber.u.AsULONG,
+                     &Config,
+                     sizeof(PCI_COMMON_CONFIG));
+                  INFO_(VIDEOPRT, "- Length of data: %x\n", ReturnedLength);
+                  if (ReturnedLength == sizeof(PCI_COMMON_CONFIG))
                   {
-                     break;
+                     INFO_(VIDEOPRT, "- Slot 0x%02x (Device %d Function %d) VendorId 0x%04x "
+                            "DeviceId 0x%04x\n",
+                            PciSlotNumber.u.AsULONG,
+                            PciSlotNumber.u.bits.DeviceNumber,
+                            PciSlotNumber.u.bits.FunctionNumber,
+                            Config.VendorID,
+                            Config.DeviceID);
+
+                     if ((VendorIdToFind == 0 || Config.VendorID == VendorIdToFind) &&
+                         (DeviceIdToFind == 0 || Config.DeviceID == DeviceIdToFind))
+                     {
+                        DeviceAndVendorFound = TRUE;
+                        break;
+                     }
                   }
                }
+               if (DeviceAndVendorFound) break;
             }
-
             if (FunctionNumber == 8)
             {
                WARN_(VIDEOPRT, "Didn't find device.\n");
@@ -491,7 +496,10 @@ VideoPortGetAccessRanges(
          }
 
          Status = HalAssignSlotResources(
-            NULL, NULL, NULL, NULL,
+            &DeviceExtension->RegistryPath,
+            NULL,
+            DeviceExtension->DriverObject,
+            DeviceExtension->DriverObject->DeviceObject,
             DeviceExtension->AdapterInterfaceType,
             DeviceExtension->SystemIoBusNumber,
             PciSlotNumber.u.AsULONG,
@@ -499,23 +507,26 @@ VideoPortGetAccessRanges(
 
          if (!NT_SUCCESS(Status))
          {
+            WARN_(VIDEOPRT, "HalAssignSlotResources failed with status %x.\n",Status);
             return Status;
          }
          DeviceExtension->AllocatedResources = AllocatedResources;
       }
       if (AllocatedResources == NULL)
-         return ERROR_NO_SYSTEM_RESOURCES;
-
+         return ERROR_NOT_ENOUGH_MEMORY;
       AssignedCount = 0;
       for (FullList = AllocatedResources->List;
            FullList < AllocatedResources->List + AllocatedResources->Count;
            FullList++)
       {
-         ASSERT(FullList->InterfaceType == PCIBus &&
-                FullList->BusNumber == DeviceExtension->SystemIoBusNumber &&
-                1 == FullList->PartialResourceList.Version &&
-                1 == FullList->PartialResourceList.Revision);
-        for (Descriptor = FullList->PartialResourceList.PartialDescriptors;
+         INFO_(VIDEOPRT, "InterfaceType %u BusNumber List %u Device BusNumber %u Version %u Revision %u\n", 
+                FullList->InterfaceType, FullList->BusNumber, DeviceExtension->SystemIoBusNumber, FullList->PartialResourceList.Version, FullList->PartialResourceList.Revision);
+
+         ASSERT(FullList->InterfaceType == PCIBus);
+         ASSERT(FullList->BusNumber == DeviceExtension->SystemIoBusNumber);
+         ASSERT(1 == FullList->PartialResourceList.Version);
+         ASSERT(1 == FullList->PartialResourceList.Revision);
+         for (Descriptor = FullList->PartialResourceList.PartialDescriptors;
               Descriptor < FullList->PartialResourceList.PartialDescriptors + FullList->PartialResourceList.Count;
               Descriptor++)
          {
@@ -524,14 +535,14 @@ VideoPortGetAccessRanges(
                 AssignedCount >= NumAccessRanges)
             {
                WARN_(VIDEOPRT, "Too many access ranges found\n");
-               return ERROR_NO_SYSTEM_RESOURCES;
+               return ERROR_NOT_ENOUGH_MEMORY;
             }
             if (Descriptor->Type == CmResourceTypeMemory)
             {
                if (NumAccessRanges <= AssignedCount)
                {
                   WARN_(VIDEOPRT, "Too many access ranges found\n");
-                  return ERROR_NO_SYSTEM_RESOURCES;
+                  return ERROR_NOT_ENOUGH_MEMORY;
                }
                INFO_(VIDEOPRT, "Memory range starting at 0x%08x length 0x%08x\n",
                       Descriptor->u.Memory.Start.u.LowPart, Descriptor->u.Memory.Length);
@@ -667,7 +678,7 @@ VideoPortGetDeviceData(
 {
    TRACE_(VIDEOPRT, "VideoPortGetDeviceData\n");
    UNIMPLEMENTED;
-   return ERROR_CALL_NOT_IMPLEMENTED;
+   return ERROR_INVALID_FUNCTION;
 }
 
 /*
@@ -726,7 +737,7 @@ VideoPortReleaseBuffer(
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 
 PVOID NTAPI
@@ -736,8 +747,16 @@ VideoPortLockBuffer(
    IN ULONG Length,
    IN VP_LOCK_OPERATION Operation)
 {
-    UNIMPLEMENTED;
-    return NULL;
+    PMDL Mdl;
+
+    Mdl = IoAllocateMdl(BaseAddress, Length, FALSE, FALSE, NULL);
+    if (!Mdl)
+    {
+        return NULL;
+    }
+    /* FIXME use seh */
+    MmProbeAndLockPages(Mdl, KernelMode,Operation);
+    return Mdl;
 }
 
 /*
@@ -749,7 +768,11 @@ VideoPortUnlockBuffer(
    IN PVOID HwDeviceExtension,
    IN PVOID Mdl)
 {
-    UNIMPLEMENTED;
+    if (Mdl)
+    {
+        MmUnlockPages((PMDL)Mdl);
+        IoFreeMdl(Mdl);
+    }
 }
 
 /*