Index: /trunk/src/VBox/Devices/Audio/DevIchAc97.cpp
===================================================================
--- /trunk/src/VBox/Devices/Audio/DevIchAc97.cpp	(revision 65013)
+++ /trunk/src/VBox/Devices/Audio/DevIchAc97.cpp	(revision 65014)
@@ -836,4 +836,8 @@
 {
     RT_NOREF(pThis);
+    AssertPtrReturn(pDstStream,  VERR_INVALID_POINTER);
+    AssertPtrReturn(pSrcMixSink, VERR_INVALID_POINTER);
+    AssertReturn(cbToWrite,      VERR_INVALID_PARAMETER);
+    /* pcbWritten is optional. */
 
     PRTCIRCBUF pCircBuf = pDstStream->State.pCircBuf;
@@ -883,4 +887,8 @@
 {
     RT_NOREF(pThis);
+    AssertPtrReturn(pSrcStream,  VERR_INVALID_POINTER);
+    AssertPtrReturn(pDstMixSink, VERR_INVALID_POINTER);
+    AssertReturn(cbToRead,       VERR_INVALID_PARAMETER);
+    /* pcbRead is optional. */
 
     PRTCIRCBUF pCircBuf = pSrcStream->State.pCircBuf;
@@ -1115,4 +1123,7 @@
     PAC97STREAMSTATEAIO pAIO = &pStream->State.AIO;
 
+    if (!ASMAtomicReadBool(&pAIO->fStarted))
+        return;
+
     int rc2 = RTCritSectEnter(&pAIO->CritSect);
     AssertRC(rc2);
@@ -1127,4 +1138,7 @@
 {
     PAC97STREAMSTATEAIO pAIO = &pStream->State.AIO;
+
+    if (!ASMAtomicReadBool(&pAIO->fStarted))
+        return;
 
     int rc2 = RTCritSectLeave(&pAIO->CritSect);
@@ -1176,4 +1190,8 @@
             STAM_PROFILE_START(&pThis->StatOut, a);
 
+            /*
+             * Read from DMA.
+             */
+
             void *pvDst;
             size_t cbDst;
@@ -1190,5 +1208,11 @@
             RTCircBufReleaseWriteBlock(pCircBuf, cbProcessed);
 
-            if (cbProcessed)
+            /*
+             * Process backends.
+             */
+
+            /* Do we have data left to write to the backends? */
+            uint32_t cbUsed = (uint32_t)RTCircBufUsed(pCircBuf);
+            if (cbUsed)
             {
 #ifdef VBOX_WITH_AUDIO_AC97_ASYNC_IO
@@ -1196,4 +1220,5 @@
                 ichac97StreamAsyncIONotify(pThis, pStream);
 #else
+                /* Read audio data from the AC'97 stream and write to the backends. */
                 rc2 = ichac97StreamRead(pThis, pStream, pMixSink, cbProcessed, NULL /* pcbRead */);
                 AssertRC(rc2);
@@ -1201,6 +1226,8 @@
             }
 
+            /* All DMA transfers done for now? */
             if (   !cbProcessed
 #ifndef VBOX_WITH_AUDIO_AC97_ASYNC_IO
+            /* All data read *and* processed for now? */
                 && RTCircBufUsed(pCircBuf) == 0
 #endif
@@ -1210,4 +1237,8 @@
             }
 
+#ifndef VBOX_WITH_AUDIO_AC97_ASYNC_IO
+            rc2 = AudioMixerSinkUpdate(pMixSink);
+            AssertRC(rc2);
+#endif
             STAM_PROFILE_STOP(&pThis->StatOut, a);
         }
@@ -1217,8 +1248,27 @@
             STAM_PROFILE_START(&pThis->StatIn, a);
 
+            /*
+             * Process backends.
+             */
+
+#ifdef VBOX_WITH_AUDIO_AC97_ASYNC_IO
+            /* Let the asynchronous thread know that there is some new data to process. */
+            ichac97StreamAsyncIONotify(pThis, pStream);
+#else
+            rc2 = AudioMixerSinkUpdate(pMixSink);
+            AssertRC(rc2);
+
+            /* Write read data from the backend to the AC'97 stream. */
+            rc2 = ichac97StreamWrite(pThis, pStream, pMixSink, 256 /** @todo Fix this! */, NULL /* pcbWritten */);
+            AssertRC(rc2);
+#endif
+            /*
+             * Write to DMA.
+             */
+
             void *pvSrc;
             size_t cbSrc;
 
-            RTCircBufAcquireReadBlock(pCircBuf, 256 /** @todo */, &pvSrc, &cbSrc);
+            RTCircBufAcquireReadBlock(pCircBuf, 256 /** @todo Fix this! */, &pvSrc, &cbSrc);
 
             if (cbSrc)
@@ -1231,24 +1281,7 @@
             RTCircBufReleaseReadBlock(pCircBuf, cbProcessed);
 
-            if (cbProcessed)
-            {
-#ifdef VBOX_WITH_AUDIO_AC97_ASYNC_IO
-                /* Let the asynchronous thread know that there is some new data to process. */
-                ichac97StreamAsyncIONotify(pThis, pStream);
-#else
-                rc2 = ichac97StreamWrite(pThis, pStream, pMixSink, cbProcessed, NULL /* pcbWritten */);
-                AssertRC(rc2);
-#endif
-            }
-
-            /** @todo: Check this! */
-            if (   !cbProcessed
-#ifndef VBOX_WITH_AUDIO_AC97_ASYNC_IO
-                && RTCircBufUsed(pCircBuf) == 0
-#endif
-               )
-            {
+            /* All DMA transfers done for now? */
+            if (!cbProcessed)
                 fDone = true;
-            }
 
             STAM_PROFILE_STOP(&pThis->StatIn, a);
@@ -1257,8 +1290,4 @@
             AssertFailed();
 
-#ifndef VBOX_WITH_AUDIO_AC97_ASYNC_IO
-        rc2 = AudioMixerSinkUpdate(pMixSink);
-        AssertRC(rc2);
-#endif
         if (fDone)
             break;
