- Copy the map registers to the buffer only, if they are used (in IoFlushAdapterBuffers).  
- Do not use the byte offset into the page from a given buffer if the map registers are used, 
  because the caller didn't request for one additional register in the call to IoAllocateAdapterChannel 
  and it will not work for a 64k buffer.
Modified: trunk/reactos/hal/halx86/generic/dma.c
Modified: trunk/reactos/hal/halx86/include/haldma.h

Modified: trunk/reactos/hal/halx86/generic/dma.c
--- trunk/reactos/hal/halx86/generic/dma.c	2005-09-05 20:25:16 UTC (rev 17664)
+++ trunk/reactos/hal/halx86/generic/dma.c	2005-09-05 20:25:31 UTC (rev 17665)
@@ -646,12 +646,7 @@
    }
    else
    {
-      /*
-       * In the equation below the additional map register added by
-       * the "+1" accounts for the case when a transfer does not start
-       * at a page-aligned address.
-       */
-      MapRegisters = BYTES_TO_PAGES(MaximumLength) + 1;
+      MapRegisters = BYTES_TO_PAGES(MaximumLength);
       if (MapRegisters > 16)
          MapRegisters = 16;
    }
@@ -1466,7 +1461,6 @@
 {
    ULONG CurrentLength;
    ULONG_PTR CurrentAddress;
-   ULONG ByteOffset;
    PVOID VirtualAddress;
 
    VirtualAddress = MmGetSystemAddressForMdlSafe(Mdl, HighPagePriority);
@@ -1487,15 +1481,11 @@
 
    while (Length > 0)
    {
-      ByteOffset = BYTE_OFFSET(CurrentAddress);
-      CurrentLength = PAGE_SIZE - ByteOffset;
-      if (CurrentLength > Length)
-         CurrentLength = Length;
-
+      CurrentLength = min(PAGE_SIZE, Length);
       if (WriteToDevice)
       {
          RtlCopyMemory(
-            (PVOID)((ULONG_PTR)MapRegisterBase->VirtualAddress + ByteOffset),
+            MapRegisterBase->VirtualAddress,
             (PVOID)CurrentAddress,
             CurrentLength);
       }
@@ -1503,7 +1493,7 @@
       {
          RtlCopyMemory(
             (PVOID)CurrentAddress,
-            (PVOID)((ULONG_PTR)MapRegisterBase->VirtualAddress + ByteOffset),
+            MapRegisterBase->VirtualAddress,
             CurrentLength);
       }
 
@@ -1584,7 +1574,7 @@
    RealMapRegisterBase =
       (PMAP_REGISTER_ENTRY)((ULONG_PTR)MapRegisterBase & ~MAP_BASE_SW_SG);
 
-   if (!WriteToDevice)
+   if (!WriteToDevice && RealMapRegisterBase->UseMapRegisters)
    {
       if ((ULONG_PTR)MapRegisterBase & MAP_BASE_SW_SG)
       {
@@ -1654,7 +1644,6 @@
    ULONG ByteOffset;
    ULONG TransferOffset;
    ULONG TransferLength;
-   BOOLEAN UseMapRegisters;
    PMAP_REGISTER_ENTRY RealMapRegisterBase;
    PHYSICAL_ADDRESS PhysicalAddress;
    PHYSICAL_ADDRESS HighestAcceptableAddress;
@@ -1754,9 +1743,8 @@
    if ((ULONG_PTR)MapRegisterBase & MAP_BASE_SW_SG &&
        TransferLength < *Length)
    {
-      UseMapRegisters = TRUE;
+      RealMapRegisterBase->UseMapRegisters = TRUE;
       PhysicalAddress = RealMapRegisterBase->PhysicalAddress;
-      PhysicalAddress.QuadPart += ByteOffset;
       TransferLength = *Length;
       RealMapRegisterBase->Counter = ~0;
       Counter = 0;
@@ -1769,7 +1757,7 @@
        * the transfer progress.
        */
 
-      UseMapRegisters = FALSE;
+      RealMapRegisterBase->UseMapRegisters = FALSE;
       Counter = RealMapRegisterBase->Counter;
       RealMapRegisterBase->Counter += BYTES_TO_PAGES(ByteOffset + TransferLength);
 
@@ -1783,9 +1771,8 @@
       if (PhysicalAddress.QuadPart + TransferLength >
           HighestAcceptableAddress.QuadPart)
       {
-         UseMapRegisters = TRUE;
+         RealMapRegisterBase->UseMapRegisters = TRUE;
          PhysicalAddress = RealMapRegisterBase->PhysicalAddress;
-         PhysicalAddress.QuadPart += ByteOffset;
          if ((ULONG_PTR)MapRegisterBase & MAP_BASE_SW_SG)
          {
             RealMapRegisterBase->Counter = ~0;
@@ -1799,7 +1786,7 @@
     * register memory.
     */
 
-   if (UseMapRegisters && WriteToDevice)
+   if (RealMapRegisterBase->UseMapRegisters && WriteToDevice)
    {
       HalpCopyBufferMap(Mdl, RealMapRegisterBase + Counter,
                         CurrentVa, TransferLength, WriteToDevice);

Modified: trunk/reactos/hal/halx86/include/haldma.h
--- trunk/reactos/hal/halx86/include/haldma.h	2005-09-05 20:25:16 UTC (rev 17664)
+++ trunk/reactos/hal/halx86/include/haldma.h	2005-09-05 20:25:31 UTC (rev 17665)
@@ -315,6 +315,7 @@
    PVOID VirtualAddress;
    PHYSICAL_ADDRESS PhysicalAddress;
    ULONG Counter;
+   BOOLEAN UseMapRegisters;
 } MAP_REGISTER_ENTRY, *PMAP_REGISTER_ENTRY;
 
 struct _ADAPTER_OBJECT {