<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 28552ea9cad567f75aa3b5caf7111df4086c10b4 Mon Sep 17 00:00:00 2001
Message-Id: &lt;28552ea9cad567f75aa3b5caf7111df4086c10b4.1355923269.git.minovotn@redhat.com&gt;
In-Reply-To: &lt;d2057772c367ef69443b067dca18d1f4f42c6711.1355923269.git.minovotn@redhat.com&gt;
References: &lt;d2057772c367ef69443b067dca18d1f4f42c6711.1355923269.git.minovotn@redhat.com&gt;
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= &lt;marcandre.lureau@gmail.com&gt;
Date: Tue, 18 Dec 2012 11:25:47 +0100
Subject: [PATCH 8/9] hw/ac97: add support for volume control


Signed-off-by: Michal Novotny &lt;minovotn@redhat.com&gt;
---
 hw/ac97.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

diff --git a/hw/ac97.c b/hw/ac97.c
index 6552da3..6a8dcd7 100644
--- a/hw/ac97.c
+++ b/hw/ac97.c
@@ -430,6 +430,65 @@ static void reset_voices (AC97LinkState *s, uint8_t active[LAST_INDEX])
     AUD_set_active_in (s-&gt;voice_mc, active[MC_INDEX]);
 }
 
+static void get_volume (uint16_t vol, uint16_t mask, int inverse,
+                        int *mute, uint8_t *lvol, uint8_t *rvol)
+{
+    *mute = (vol &gt;&gt; MUTE_SHIFT) &amp; 1;
+    *rvol = (255 * (vol &amp; mask)) / mask;
+    *lvol = (255 * ((vol &gt;&gt; 8) &amp; mask)) / mask;
+
+    if (inverse) {
+        *rvol = 255 - *rvol;
+        *lvol = 255 - *lvol;
+    }
+}
+
+static void update_combined_volume_out (AC97LinkState *s)
+{
+    uint8_t lvol, rvol, plvol, prvol;
+    int mute, pmute;
+
+    get_volume (mixer_load (s, AC97_Master_Volume_Mute), 0x3f, 1,
+                &amp;mute, &amp;lvol, &amp;rvol);
+    /* FIXME: should be 1f according to spec */
+    get_volume (mixer_load (s, AC97_PCM_Out_Volume_Mute), 0x3f, 1,
+                &amp;pmute, &amp;plvol, &amp;prvol);
+
+    mute = mute | pmute;
+    lvol = (lvol * plvol) / 255;
+    rvol = (rvol * prvol) / 255;
+
+    AUD_set_volume_out (s-&gt;voice_po, mute, lvol, rvol);
+}
+
+static void update_volume_in (AC97LinkState *s)
+{
+    uint8_t lvol, rvol;
+    int mute;
+
+    get_volume (mixer_load (s, AC97_Record_Gain_Mute), 0x0f, 0,
+                &amp;mute, &amp;lvol, &amp;rvol);
+
+    AUD_set_volume_in (s-&gt;voice_pi, mute, lvol, rvol);
+}
+
+static void set_volume (AC97LinkState *s, int index, uint32_t val)
+{
+    mixer_store (s, index, val);
+    if (index == AC97_Master_Volume_Mute || index == AC97_PCM_Out_Volume_Mute) {
+        update_combined_volume_out (s);
+    } else if (index == AC97_Record_Gain_Mute) {
+        update_volume_in (s);
+    }
+}
+
+static void record_select (AC97LinkState *s, uint32_t val)
+{
+    uint8_t rs = val &amp; REC_MASK;
+    uint8_t ls = (val &gt;&gt; 8) &amp; REC_MASK;
+    mixer_store (s, AC97_Record_Select, rs | (ls &lt;&lt; 8));
+}
+
 static void mixer_reset (AC97LinkState *s)
 {
     uint8_t active[LAST_INDEX];
@@ -464,6 +523,11 @@ static void mixer_reset (AC97LinkState *s)
     mixer_store (s, AC97_PCM_LR_ADC_Rate         , 0xbb80);
     mixer_store (s, AC97_MIC_ADC_Rate            , 0xbb80);
 
+    record_select (s, 0);
+    set_volume (s, AC97_Master_Volume_Mute, 0x8000);
+    set_volume (s, AC97_PCM_Out_Volume_Mute, 0x8808);
+    set_volume (s, AC97_Line_In_Volume_Mute, 0x8808);
+
     reset_voices (s, active);
 }
 
@@ -522,6 +586,15 @@ static void nam_writew (void *opaque, uint32_t addr, uint32_t val)
         val |= mixer_load (s, index) &amp; 0xf;
         mixer_store (s, index, val);
         break;
+    case AC97_PCM_Out_Volume_Mute:
+    case AC97_Master_Volume_Mute:
+    case AC97_Record_Gain_Mute:
+    case AC97_Line_In_Volume_Mute:
+        set_volume (s, index, val);
+        break;
+    case AC97_Record_Select:
+        record_select (s, val);
+        break;
     case AC97_Vendor_ID1:
     case AC97_Vendor_ID2:
         dolog ("Attempt to write vendor ID to %#x\n", val);
@@ -1078,6 +1151,14 @@ static int ac97_post_load (void *opaque, int version_id)
     uint8_t active[LAST_INDEX];
     AC97LinkState *s = opaque;
 
+    record_select (s, mixer_load (s, AC97_Record_Select));
+    set_volume (s, AC97_Master_Volume_Mute,
+                mixer_load (s, AC97_Master_Volume_Mute));
+    set_volume (s, AC97_PCM_Out_Volume_Mute,
+                mixer_load (s, AC97_PCM_Out_Volume_Mute));
+    set_volume (s, AC97_Line_In_Volume_Mute,
+                mixer_load (s, AC97_Line_In_Volume_Mute));
+
     active[PI_INDEX] = !!(s-&gt;bm_regs[PI_INDEX].cr &amp; CR_RPBM);
     active[PO_INDEX] = !!(s-&gt;bm_regs[PO_INDEX].cr &amp; CR_RPBM);
     active[MC_INDEX] = !!(s-&gt;bm_regs[MC_INDEX].cr &amp; CR_RPBM);
-- 
1.7.11.7

</pre></body></html>