- Update to r53061
[reactos.git] / drivers / video / videoprt / dma.c
index a2db10a..bc344ae 100644 (file)
 
 typedef struct
 {
+    LIST_ENTRY Entry;
     PDMA_ADAPTER Adapter;
     ULONG MapRegisters;
+    PVOID HwDeviceExtension;
 
 }VIP_DMA_ADAPTER, *PVIP_DMA_ADAPTER;
 
@@ -46,7 +48,14 @@ VideoPortAllocateCommonBuffer(IN PVOID HwDeviceExtension,
 {
     PVIP_DMA_ADAPTER Adapter = (PVIP_DMA_ADAPTER)VpDmaAdapter;
 
+    /* check for valid arguments */
+    if (!Adapter || !Adapter->Adapter)
+    {
+        /* invalid parameter */
+        return NULL;
+    }
 
+    /* allocate common buffer */
     return Adapter->Adapter->DmaOperations->AllocateCommonBuffer(Adapter->Adapter, DesiredLength, LogicalAddress, CacheEnabled);
 }
 
@@ -64,6 +73,14 @@ VideoPortReleaseCommonBuffer(IN PVOID HwDeviceExtension,
 {
     PVIP_DMA_ADAPTER Adapter = (PVIP_DMA_ADAPTER)VpDmaAdapter;
 
+    /* check for valid arguments */
+    if (!Adapter || !Adapter->Adapter)
+    {
+        /* invalid parameter */
+        return;
+    }
+
+    /* release common buffer */
     Adapter->Adapter->DmaOperations->FreeCommonBuffer(Adapter->Adapter, Length, LogicalAddress, VirtualAddress, CacheEnabled);
 }
 
@@ -75,9 +92,22 @@ NTAPI
 VideoPortPutDmaAdapter(IN PVOID HwDeviceExtension,
                        IN PVP_DMA_ADAPTER VpDmaAdapter)
 {
+    PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension;
     PVIP_DMA_ADAPTER Adapter = (PVIP_DMA_ADAPTER)VpDmaAdapter;
 
+    /* get hw device extension */
+    DeviceExtension = VIDEO_PORT_GET_DEVICE_EXTENSION(HwDeviceExtension);
+
+    /* sanity check */
+    ASSERT(!IsListEmpty(&DeviceExtension->DmaAdapterList));
+
+    /* remove dma adapter from list */
+    RemoveEntryList(&Adapter->Entry);
+
+    /* release dma adapter */
     Adapter->Adapter->DmaOperations->PutDmaAdapter(Adapter->Adapter);
+
+    /* free memory */
     ExFreePool(Adapter);
 }
 
@@ -95,13 +125,21 @@ VideoPortGetDmaAdapter(IN PVOID HwDeviceExtension,
     PVIP_DMA_ADAPTER Adapter;
     PDMA_ADAPTER DmaAdapter;
 
+    /* allocate private adapter structure */
+    Adapter = ExAllocatePool(NonPagedPool, sizeof(VIP_DMA_ADAPTER));
+    if (!Adapter)
+    {
+        /* failed to allocate adapter structure */
+        return NULL;
+    }
+
     /* Zero the structure */
     RtlZeroMemory(&DeviceDescription,
                   sizeof(DEVICE_DESCRIPTION));
 
     /* Initialize the structure */
     DeviceDescription.Version = DEVICE_DESCRIPTION_VERSION;
-    DeviceDescription.Master = TRUE /* ?? */;
+    DeviceDescription.Master = TRUE;
     DeviceDescription.DmaWidth = Width8Bits;
     DeviceDescription.DmaSpeed = Compatible;
 
@@ -115,21 +153,28 @@ VideoPortGetDmaAdapter(IN PVOID HwDeviceExtension,
     DeviceDescription.BusNumber = DeviceExtension->SystemIoBusNumber;
     DeviceDescription.InterfaceType = DeviceExtension->AdapterInterfaceType;
 
-    Adapter = ExAllocatePool(NonPagedPool, sizeof(VIP_DMA_ADAPTER));
-    if (!Adapter)
-        return NULL;
-
-
+    /* acquire dma adapter */
     DmaAdapter = IoGetDmaAdapter(DeviceExtension->PhysicalDeviceObject, &DeviceDescription, &NumberOfMapRegisters);
     if (!DmaAdapter)
     {
+        /* failed to acquire dma */
         ExFreePool(Adapter);
         return NULL;
     }
 
+    /* store dma adapter */
     Adapter->Adapter = DmaAdapter;
+
+    /* store map register count */
     Adapter->MapRegisters = NumberOfMapRegisters;
 
+    /* store hw device extension */
+    Adapter->HwDeviceExtension = HwDeviceExtension;
+
+    /* store in dma adapter list */
+    InsertTailList(&DeviceExtension->DmaAdapterList, &Adapter->Entry);
+
+    /* return result */
     return (PVP_DMA_ADAPTER)Adapter;
 }
 
@@ -144,21 +189,25 @@ VideoPortFreeCommonBuffer(IN PVOID HwDeviceExtension,
                           IN PHYSICAL_ADDRESS LogicalAddress,
                           IN BOOLEAN CacheEnabled)
 {
-    DEVICE_DESCRIPTION DeviceDescription;
-    PVP_DMA_ADAPTER VpDmaAdapter;
+    PVIP_DMA_ADAPTER VpDmaAdapter;
+    PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension = VIDEO_PORT_GET_DEVICE_EXTENSION(HwDeviceExtension);
+
+    /* sanity check */
+    ASSERT(!IsListEmpty(&DeviceExtension->DmaAdapterList));
+
+    /* grab first dma adapter */
+    VpDmaAdapter = (PVIP_DMA_ADAPTER)CONTAINING_RECORD(DeviceExtension->DmaAdapterList.Flink, VIP_DMA_ADAPTER, Entry);
+
+    /* sanity checks */
+    ASSERT(VpDmaAdapter->HwDeviceExtension == HwDeviceExtension);
+    ASSERT(VpDmaAdapter->Adapter != NULL);
+    ASSERT(VpDmaAdapter->MapRegisters != 0);
 
-    /* FIXME: Broken code*/
-    VpDmaAdapter = VideoPortGetDmaAdapter(HwDeviceExtension,
-                                          (PVP_DEVICE_DESCRIPTION)&DeviceDescription);
-    HalFreeCommonBuffer((PADAPTER_OBJECT)VpDmaAdapter,
-                        Length,
-                        LogicalAddress,
-                        VirtualAddress,
-                        CacheEnabled);
+    VideoPortReleaseCommonBuffer(HwDeviceExtension, (PVP_DMA_ADAPTER)VpDmaAdapter, Length, LogicalAddress, VirtualAddress, CacheEnabled);
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 PVOID
 NTAPI
@@ -169,8 +218,44 @@ VideoPortGetCommonBuffer(IN PVOID HwDeviceExtension,
                          OUT PULONG pActualLength,
                          IN BOOLEAN CacheEnabled)
 {
-    UNIMPLEMENTED;
-       return NULL;
+    PVOID Result;
+    PVIP_DMA_ADAPTER VpDmaAdapter;
+    PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension = VIDEO_PORT_GET_DEVICE_EXTENSION(HwDeviceExtension);
+
+    /* maximum palette size */
+    if (DesiredLength > 262144) 
+    {
+        /* size exceeded */
+        return NULL;
+    }
+
+    /* sanity check */
+    ASSERT(!IsListEmpty(&DeviceExtension->DmaAdapterList));
+
+    /* grab first dma adapter */
+    VpDmaAdapter = (PVIP_DMA_ADAPTER)CONTAINING_RECORD(DeviceExtension->DmaAdapterList.Flink, VIP_DMA_ADAPTER, Entry);
+
+    /* sanity checks */
+    ASSERT(VpDmaAdapter->HwDeviceExtension == HwDeviceExtension);
+    ASSERT(VpDmaAdapter->Adapter != NULL);
+    ASSERT(VpDmaAdapter->MapRegisters != 0);
+
+
+    /* allocate common buffer */
+    Result = VideoPortAllocateCommonBuffer(HwDeviceExtension, (PVP_DMA_ADAPTER)VpDmaAdapter, DesiredLength, LogicalAddress, CacheEnabled, NULL);
+
+    if (Result)
+    {
+        /* store length */
+        *pActualLength = DesiredLength;
+    }
+    else
+    {
+        /* failed to allocate common buffer */
+        *pActualLength = 0;
+    }
+
+    return Result;
 }
 
 /*
@@ -185,7 +270,7 @@ VideoPortUnmapDmaMemory(
     PDMA  BoardMemoryHandle)
 {
     /* Deprecated */
-       return FALSE;
+    return FALSE;
 }
 
 /*
@@ -203,7 +288,7 @@ VideoPortMapDmaMemory(IN PVOID HwDeviceExtension,
                       IN OUT PVOID *VirtualAddress)
 {
     /* Deprecated */
-       return NULL;
+    return NULL;
 }
 
 /*
@@ -216,7 +301,7 @@ VideoPortSetDmaContext(IN PVOID HwDeviceExtension,
                        IN PVOID InstanceContext)
 {
     /* Deprecated */
-       return;
+    return;
 }
 
 /*
@@ -228,7 +313,7 @@ VideoPortSignalDmaComplete(IN PVOID HwDeviceExtension,
                            IN PDMA pDmaHandle)
 {
     /* Deprecated */
-       return FALSE;
+    return FALSE;
 }
 
 
@@ -327,7 +412,7 @@ VideoPortGetDmaContext(IN PVOID HwDeviceExtension,
                        IN PDMA pDma)
 {
     /* Deprecated */
-       return NULL;
+    return NULL;
 }
 
 /*
@@ -344,7 +429,7 @@ VideoPortDoDma(IN PVOID HwDeviceExtension,
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 PDMA
 NTAPI
@@ -353,8 +438,8 @@ VideoPortAssociateEventsWithDmaHandle(IN PVOID HwDeviceExtension,
                                       IN PVOID MappedUserEvent,
                                       IN PVOID DisplayDriverEvent)
 {
-    UNIMPLEMENTED;
-       return NULL;
+    /* Deprecated */
+    return NULL;
 }
 
 /*