Index: trunk/src/VBox/Devices/Audio/DevIchAc97.cpp
===================================================================
--- trunk/src/VBox/Devices/Audio/DevIchAc97.cpp	(revision 89722)
+++ trunk/src/VBox/Devices/Audio/DevIchAc97.cpp	(revision 89723)
@@ -848,11 +848,12 @@
  * @param   pStreamCC           The AC'97 stream to retrieve size for (ring-3).
  */
-static uint32_t ichac97R3StreamGetUsed(PAC97STREAMR3 pStreamCC)
-{
-    if (!pStreamCC->State.pCircBuf)
-        return 0;
-
-    return (uint32_t)RTCircBufUsed(pStreamCC->State.pCircBuf);
-}
+DECLINLINE(uint32_t) ichac97R3StreamGetUsed(PAC97STREAMR3 pStreamCC)
+{
+    PRTCIRCBUF const pCircBuf = pStreamCC->State.pCircBuf;
+    if (pCircBuf)
+        return (uint32_t)RTCircBufUsed(pCircBuf);
+    return 0;
+}
+
 
 /**
@@ -862,11 +863,12 @@
  * @param   pStreamCC           AC'97 stream to retrieve size for (ring-3).
  */
-static uint32_t ichac97R3StreamGetFree(PAC97STREAMR3 pStreamCC)
-{
-    if (!pStreamCC->State.pCircBuf)
-        return 0;
-
-    return (uint32_t)RTCircBufFree(pStreamCC->State.pCircBuf);
-}
+DECLINLINE(uint32_t) ichac97R3StreamGetFree(PAC97STREAMR3 pStreamCC)
+{
+    PRTCIRCBUF const pCircBuf = pStreamCC->State.pCircBuf;
+    if (pCircBuf)
+        return (uint32_t)RTCircBufFree(pCircBuf);
+    return 0;
+}
+
 
 # if 0 /* Unused */
@@ -1015,19 +1017,16 @@
                                    PAC97STREAMR3 pStreamCC, uint32_t cbToProcessMax, bool fWriteSilence, bool fInput)
 {
-    if (!cbToProcessMax)
+    if (RT_LIKELY(cbToProcessMax > 0))
+        Assert(PDMAudioPropsIsSizeAligned(&pStreamCC->State.Cfg.Props, cbToProcessMax));
+    else
         return VINF_SUCCESS;
 
-#ifdef VBOX_STRICT
-    const unsigned cbFrame = PDMAudioPropsBytesPerFrame(&pStreamCC->State.Cfg.Props);
-#endif
-
-    /* Make sure to only process an integer number of audio frames. */
-    Assert(cbToProcessMax % cbFrame == 0);
-
     ichac97R3StreamLock(pStreamCC);
 
     PAC97BMREGS pRegs = &pStream->Regs;
 
-    if (pRegs->sr & AC97_SR_DCH) /* Controller halted? */
+    if (!(pRegs->sr & AC97_SR_DCH)) /* Controller halted? */
+    { /* not halted - likely */ }
+    else
     {
         if (pRegs->cr & AC97_CR_RPBM) /* Bus master operation starts. */
@@ -1049,6 +1048,10 @@
 
     /* BCIS flag still set? Skip iteration. */
-    if (pRegs->sr & AC97_SR_BCIS)
-    {
+/** @todo combine with the above test. */
+    if (!(pRegs->sr & AC97_SR_BCIS))
+    { /* likely */ }
+    else
+    {
+        /** @todo counter   */
         Log3Func(("[SD%RU8] BCIS set\n", pStream->u8SD));
 
@@ -1619,5 +1622,5 @@
  * @param   pStreamCC           The AC'97 stream to lock (ring-3).
  */
-static void ichac97R3StreamLock(PAC97STREAMR3 pStreamCC)
+DECLINLINE(void) ichac97R3StreamLock(PAC97STREAMR3 pStreamCC)
 {
     int rc2 = RTCritSectEnter(&pStreamCC->State.CritSect);
@@ -1631,5 +1634,5 @@
  * @param   pStreamCC           The AC'97 stream to unlock (ring-3).
  */
-static void ichac97R3StreamUnlock(PAC97STREAMR3 pStreamCC)
+DECLINLINE(void) ichac97R3StreamUnlock(PAC97STREAMR3 pStreamCC)
 {
     int rc2 = RTCritSectLeave(&pStreamCC->State.CritSect);
@@ -1758,21 +1761,15 @@
 static PAC97DRIVERSTREAM ichac97R3MixerGetDrvStream(PAC97DRIVER pDrv, PDMAUDIODIR enmDir, PDMAUDIOPATH enmPath)
 {
-    PAC97DRIVERSTREAM pDrvStream = NULL;
-
     if (enmDir == PDMAUDIODIR_IN)
     {
         LogFunc(("enmRecSource=%d\n", enmPath));
-
         switch (enmPath)
         {
             case PDMAUDIOPATH_IN_LINE:
-                pDrvStream = &pDrv->LineIn;
-                break;
+                return &pDrv->LineIn;
             case PDMAUDIOPATH_IN_MIC:
-                pDrvStream = &pDrv->MicIn;
-                break;
+                return &pDrv->MicIn;
             default:
-                AssertFailed();
-                break;
+                AssertFailedBreak();
         }
     }
@@ -1780,13 +1777,10 @@
     {
         LogFunc(("enmPlaybackDst=%d\n", enmPath));
-
         switch (enmPath)
         {
             case PDMAUDIOPATH_OUT_FRONT:
-                pDrvStream = &pDrv->Out;
-                break;
+                return &pDrv->Out;
             default:
-                AssertFailed();
-                break;
+                AssertFailedBreak();
         }
     }
@@ -1794,5 +1788,5 @@
         AssertFailed();
 
-    return pDrvStream;
+    return NULL;
 }
 
@@ -1833,10 +1827,9 @@
             rc = AudioMixerSinkAddStream(pMixSink, pMixStrm);
             LogFlowFunc(("LUN#%RU8: Added stream \"%s\" to sink, rc=%Rrc\n", pDrv->uLUN, pStreamCfg->szName, rc));
-            if (RT_FAILURE(rc))
+            if (RT_SUCCESS(rc))
+                pDrvStream->pMixStrm = pMixStrm;
+            else
                 AudioMixerStreamDestroy(pMixStrm, pDevIns, true /*fImmediate*/);
         }
-
-        if (RT_SUCCESS(rc))
-            pDrvStream->pMixStrm = pMixStrm;
     }
     else
@@ -1862,21 +1855,25 @@
     AssertPtrReturn(pMixSink, VERR_INVALID_POINTER);
 
-    if (!AudioHlpStreamCfgIsValid(pCfg))
-        return VERR_INVALID_PARAMETER;
-
-    int rc = AudioMixerSinkSetFormat(pMixSink, &pCfg->Props);
-    if (RT_FAILURE(rc))
-        return rc;
-
-    PAC97DRIVER pDrv;
-    RTListForEach(&pThisCC->lstDrv, pDrv, AC97DRIVER, Node)
-    {
-        int rc2 = ichac97R3MixerAddDrvStream(pDevIns, pMixSink, pCfg, pDrv);
-        if (RT_FAILURE(rc2))
-            LogFunc(("Attaching stream failed with %Rrc\n", rc2));
-
-        /* Do not pass failure to rc here, as there might be drivers which aren't
-         * configured / ready yet. */
-    }
+    int rc;
+    if (AudioHlpStreamCfgIsValid(pCfg))
+    {
+        rc = AudioMixerSinkSetFormat(pMixSink, &pCfg->Props);
+        if (RT_SUCCESS(rc))
+        {
+
+            PAC97DRIVER pDrv;
+            RTListForEach(&pThisCC->lstDrv, pDrv, AC97DRIVER, Node)
+            {
+                int rc2 = ichac97R3MixerAddDrvStream(pDevIns, pMixSink, pCfg, pDrv);
+                if (RT_FAILURE(rc2))
+                    LogFunc(("Attaching stream failed with %Rrc\n", rc2));
+
+                /* Do not pass failure to rc here, as there might be drivers which aren't
+                   configured / ready yet. */
+            }
+        }
+    }
+    else
+        rc = VERR_INVALID_PARAMETER;
 
     LogFlowFuncLeaveRC(rc);
@@ -1886,4 +1883,6 @@
 /**
  * Adds a specific AC'97 driver to the driver chain.
+ *
+ * Only called from ichac97R3Attach().
  *
  * @returns VBox status code.
@@ -1922,4 +1921,6 @@
  * Removes a specific AC'97 driver from the driver chain and destroys its
  * associated streams.
+ *
+ * Only called from ichac97R3Detach().
  *
  * @param   pDevIns     The device instance.
@@ -2277,16 +2278,19 @@
 }
 
+
 /**
  * Closes an AC'97 stream.
  *
- * @returns VBox status code.
+ * Empty stub at present, nothing to do here as we reuse streams and only really
+ * re-open them if parameters changed (seldom).
+ *
  * @param   pStream             The AC'97 stream to close (shared).
  */
-static int ichac97R3StreamClose(PAC97STREAM pStream)
+static void ichac97R3StreamClose(PAC97STREAM pStream)
 {
     RT_NOREF(pStream);
     LogFlowFunc(("[SD%RU8]\n", pStream->u8SD));
-    return VINF_SUCCESS;
-}
+}
+
 
 /**
@@ -2311,9 +2315,6 @@
     Assert(pStreamCC - &pThisCC->aStreams[0] == pStream->u8SD);
 
-    int rc = ichac97R3StreamClose(pStream);
-    if (RT_SUCCESS(rc))
-        rc = ichac97R3StreamOpen(pDevIns, pThis, pThisCC, pStream, pStreamCC, fForce);
-
-    return rc;
+    ichac97R3StreamClose(pStream);
+    return ichac97R3StreamOpen(pDevIns, pThis, pThisCC, pStream, pStreamCC, fForce);
 }
 
@@ -2383,8 +2384,6 @@
     else
     {
-        rc = ichac97R3StreamClose(pStream);
-        if (RT_SUCCESS(rc))
-            rc = AudioMixerSinkDrainAndStop(pSink,
-                                            pStreamCC->State.pCircBuf ? (uint32_t)RTCircBufUsed(pStreamCC->State.pCircBuf) : 0);
+        rc = AudioMixerSinkDrainAndStop(pSink, pStreamCC->State.pCircBuf ? (uint32_t)RTCircBufUsed(pStreamCC->State.pCircBuf) : 0);
+        ichac97R3StreamClose(pStream);
     }
 
@@ -2929,8 +2928,7 @@
 {
     AssertMsgReturnVoid(uMixerIdx + 2U <= sizeof(pThis->mixer_data),
-                         ("Index %RU8 out of bounds (%zu)\n", uMixerIdx, sizeof(pThis->mixer_data)));
-
-    LogRel2(("AC97: Setting mixer index #%RU8 to %RU16 (%RU8 %RU8)\n",
-             uMixerIdx, uVal, RT_HI_U8(uVal), RT_LO_U8(uVal)));
+                        ("Index %RU8 out of bounds (%zu)\n", uMixerIdx, sizeof(pThis->mixer_data)));
+
+    LogRel2(("AC97: Setting mixer index #%RU8 to %RU16 (%RU8 %RU8)\n", uMixerIdx, uVal, RT_HI_U8(uVal), RT_LO_U8(uVal)));
 
     pThis->mixer_data[uMixerIdx + 0] = RT_LO_U8(uVal);
@@ -2981,5 +2979,7 @@
      * master volume controls.
      */
-    if (index == AC97_Master_Volume_Mute || index == AC97_Headphone_Volume_Mute || index == AC97_Master_Volume_Mono_Mute)
+    if (   index == AC97_Master_Volume_Mute
+        || index == AC97_Headphone_Volume_Mute
+        || index == AC97_Master_Volume_Mono_Mute)
     {
         if (uVal & RT_BIT(5))  /* D5 bit set? */
@@ -3063,5 +3063,5 @@
  * Sets the gain of a specific AC'97 recording control.
  *
- * NB: gain support is currently not implemented in PDM audio.
+ * @note    Gain support is currently not implemented in PDM audio.
  *
  * @returns VBox status code.
@@ -3078,5 +3078,5 @@
      * zero being 0dB gain and 15 being +22.5dB gain.
      */
-    const bool  fCtlMuted     = (uVal >> AC97_BARS_VOL_MUTE_SHIFT) & 1;
+    bool const  fCtlMuted     = (uVal >> AC97_BARS_VOL_MUTE_SHIFT) & 1;
     uint8_t     uCtlGainLeft  = (uVal >> 8) & AC97_BARS_GAIN_MASK;
     uint8_t     uCtlGainRight = uVal & AC97_BARS_GAIN_MASK;
@@ -3147,4 +3147,5 @@
 }
 
+
 /**
  * Converts an AC'97 recording source index to a PDM audio recording source.
@@ -3171,4 +3172,5 @@
 }
 
+
 /**
  * Converts a PDM audio recording source to an AC'97 recording source index.
@@ -3316,31 +3318,24 @@
     {
         case 1:
-        {
             LogRel2(("AC97: Warning: Unimplemented read (1 byte) offPort=%#x\n", offPort));
             pThis->cas = 0;
             *pu32 = UINT32_MAX;
             break;
-        }
 
         case 2:
-        {
             pThis->cas = 0;
             *pu32 = ichac97MixerGet(pThis, offPort);
             break;
-        }
 
         case 4:
-        {
             LogRel2(("AC97: Warning: Unimplemented read (4 bytes) offPort=%#x\n", offPort));
             pThis->cas = 0;
             *pu32 = UINT32_MAX;
             break;
-        }
 
         default:
-        {
             AssertFailed();
             rc = VERR_IOM_IOPORT_UNUSED;
-        }
+            break;
     }
 
@@ -3367,9 +3362,7 @@
     {
         case 1:
-        {
             LogRel2(("AC97: Warning: Unimplemented NAMWrite (1 byte) offPort=%#x <- %#x\n", offPort, u32));
             pThis->cas = 0;
             break;
-        }
 
         case 2:
@@ -3563,9 +3556,7 @@
 
         case 4:
-        {
             LogRel2(("AC97: Warning: Unimplemented 4 byte NAMWrite: offPort=%#x <- %#x\n", offPort, u32));
             pThis->cas = 0;
             break;
-        }
 
         default:
@@ -3609,4 +3600,5 @@
 }
 
+
 /**
  * @callback_method_impl{FNSSMDEVSAVEEXEC}
@@ -3643,4 +3635,5 @@
     return VINF_SUCCESS;
 }
+
 
 /**
@@ -3669,4 +3662,5 @@
 }
 
+
 /**
  * @callback_method_impl{FNSSMDEVLOADEXEC}
@@ -3693,6 +3687,6 @@
     for (unsigned i = 0; i < AC97_MAX_STREAMS; i++)
     {
-        int rc2 = ichac97R3LoadStream(pDevIns, pSSM, &pThis->aStreams[i]);
-        AssertRCReturn(rc2, rc2);
+        int rc = ichac97R3LoadStream(pDevIns, pSSM, &pThis->aStreams[i]);
+        AssertRCReturn(rc, rc);
     }
 
@@ -3721,6 +3715,6 @@
      */
     uint8_t afActiveStrms[AC97SOUNDSOURCE_MAX];
-    int rc2 = pHlp->pfnSSMGetMem(pSSM, afActiveStrms, sizeof(afActiveStrms));
-    AssertRCReturn(rc2, rc2);
+    int rc = pHlp->pfnSSMGetMem(pSSM, afActiveStrms, sizeof(afActiveStrms));
+    AssertRCReturn(rc, rc);
 
     for (unsigned i = 0; i < AC97_MAX_STREAMS; i++)
@@ -3730,8 +3724,8 @@
         const PAC97STREAMR3 pStreamCC = &pThisCC->aStreams[i];
 
-        rc2 = ichac97R3StreamEnable(pDevIns, pThis, pThisCC, pStream, pStreamCC, fEnable);
-        AssertRC(rc2);
+        rc = ichac97R3StreamEnable(pDevIns, pThis, pThisCC, pStream, pStreamCC, fEnable);
+        AssertRC(rc);
         if (   fEnable
-            && RT_SUCCESS(rc2))
+            && RT_SUCCESS(rc))
         {
             /* Re-arm the timer for this stream. */
@@ -4086,4 +4080,5 @@
 }
 
+
 /**
  * @interface_method_impl{PDMDEVREGR3,pfnAttach}
@@ -4112,21 +4107,4 @@
 }
 
-/**
- * Worker for ichac97R3Detach that does all but freeing the pDrv structure.
- *
- * This is called to let the device detach from a driver for a specified LUN
- * at runtime.
- *
- * @param   pDevIns     The device instance.
- * @param   pThisCC     The ring-3 AC'97 device state.
- * @param   pDrv        Driver to detach from device.
- */
-static void ichac97R3DetachInternal(PPDMDEVINS pDevIns, PAC97STATER3 pThisCC, PAC97DRIVER pDrv)
-{
-    /* Remove the driver from our list and destory it's associated streams.
-       This also will un-set the driver as a recording source (if associated). */
-    ichac97R3MixerRemoveDrv(pDevIns, pThisCC, pDrv);
-    LogFunc(("Detached LUN#%u\n", pDrv->uLUN));
-}
 
 /**
@@ -4148,8 +4126,13 @@
         if (pDrv->uLUN == iLUN)
         {
-            ichac97R3DetachInternal(pDevIns, pThisCC, pDrv);
+            /* Remove the driver from our list and destory it's associated streams.
+               This also will un-set the driver as a recording source (if associated). */
+            ichac97R3MixerRemoveDrv(pDevIns, pThisCC, pDrv);
+            LogFunc(("Detached LUN#%u\n", pDrv->uLUN));
+
+            DEVAC97_UNLOCK(pDevIns, pThis);
+
             RTStrFree(pDrv->pszDesc);
             RTMemFree(pDrv);
-            DEVAC97_UNLOCK(pDevIns, pThis);
             return;
         }
@@ -4191,4 +4174,5 @@
     return VINF_SUCCESS;
 }
+
 
 /**
