Merge upstream QEMU 10.0.50 into the Android source tree.

This change integrates many changes from the upstream QEMU sources.
Its main purpose is to enable correct ARMv6 and ARMv7 support to the
Android emulator. Due to the nature of the upstream code base, this
unfortunately also required changes to many other parts of the source.

Note that to ensure easier integrations in the future, some source files
and directories that have heavy Android-specific customization have been
renamed with an -android suffix. The original files are still there for
easier integration tracking, but *never* compiled. For example:

  net.c        net-android.c
  qemu-char.c  qemu-char-android.c
  slirp/       slirp-android/
  etc...

Tested on linux-x86, darwin-x86 and windows host machines.
diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c
index 1cc4d6e..1ae42b6 100644
--- a/audio/alsaaudio.c
+++ b/audio/alsaaudio.c
@@ -23,6 +23,7 @@
  * THE SOFTWARE.
  */
 #include <alsa/asoundlib.h>
+#include "qemu-common.h"
 #include "audio.h"
 
 #define AUDIO_CAP "alsa"
@@ -92,10 +93,10 @@
 
 /* these are inlined functions in the original headers */
 #define FF_snd_pcm_hw_params_alloca(ptr) \
-    do { assert(ptr); *ptr = (snd_pcm_hw_params_t *) alloca(FF(snd_pcm_hw_params_sizeof)()); memset(*ptr, 0, FF(snd_pcm_hw_params_sizeof)()); } while (0)
+    do { *ptr = (snd_pcm_hw_params_t *) alloca(FF(snd_pcm_hw_params_sizeof)()); memset(*ptr, 0, FF(snd_pcm_hw_params_sizeof)()); } while (0)
 
 #define FF_snd_pcm_sw_params_alloca(ptr) \
-    do { assert(ptr); *ptr = (snd_pcm_sw_params_t *) alloca(FF(snd_pcm_sw_params_sizeof)()); memset(*ptr, 0, FF(snd_pcm_sw_params_sizeof)()); } while (0)
+    do { *ptr = (snd_pcm_sw_params_t *) alloca(FF(snd_pcm_sw_params_sizeof)()); memset(*ptr, 0, FF(snd_pcm_sw_params_sizeof)()); } while (0)
 
 static void*  alsa_lib;
 
@@ -600,7 +601,7 @@
     int rpos, live, decr;
     int samples;
     uint8_t *dst;
-    st_sample_t *src;
+    struct st_sample *src;
     snd_pcm_sframes_t avail;
 
     live = audio_pcm_hw_get_live_out (hw);
@@ -685,13 +686,13 @@
     }
 }
 
-static int alsa_init_out (HWVoiceOut *hw, audsettings_t *as)
+static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
     struct alsa_params_req req;
     struct alsa_params_obt obt;
     snd_pcm_t *handle;
-    audsettings_t obt_as;
+    struct audsettings obt_as;
     int  result = -1;
 
     /* shut alsa debug spew */
@@ -776,13 +777,13 @@
     return -1;
 }
 
-static int alsa_init_in (HWVoiceIn *hw, audsettings_t *as)
+static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
 {
     ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
     struct alsa_params_req req;
     struct alsa_params_obt obt;
     snd_pcm_t *handle;
-    audsettings_t obt_as;
+    struct audsettings obt_as;
     int result = -1;
 
     /* shut alsa debug spew */
@@ -887,7 +888,7 @@
 
     for (i = 0; i < 2; ++i) {
         void *src;
-        st_sample_t *dst;
+        struct st_sample *dst;
         snd_pcm_sframes_t nread;
         snd_pcm_uframes_t len;
 
diff --git a/audio/audio.c b/audio/audio.c
index 5a77dac..fbc8431 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -24,7 +24,7 @@
  */
 #include "hw/hw.h"
 #include "audio.h"
-#include "console.h"
+#include "monitor.h"
 #include "qemu-timer.h"
 #include "sysemu.h"
 
@@ -146,14 +146,14 @@
     int enabled;
     int nb_voices;
     int greedy;
-    audsettings_t settings;
+    struct audsettings settings;
 };
 
 static struct {
     struct fixed_settings fixed_out;
     struct fixed_settings fixed_in;
     union {
-        int hz;
+        int hertz;
         int64_t ticks;
     } period;
     int plive;
@@ -183,14 +183,14 @@
         }
     },
 
-    { 0 },                      /* period */
+    { 250 },                    /* period */
     0,                          /* plive */
     0                           /* log_to_monitor */
 };
 
-AudioState glob_audio_state;
+static AudioState glob_audio_state;
 
-volume_t nominal_volume = {
+struct mixeng_volume nominal_volume = {
     0,
 #ifdef FLOAT_MIXENG
     1.0,
@@ -296,8 +296,8 @@
 static char *audio_alloc_prefix (const char *s)
 {
     const char qemu_prefix[] = "QEMU_";
-    size_t len;
-    char *r;
+    size_t len, i;
+    char *r, *u;
 
     if (!s) {
         return NULL;
@@ -306,17 +306,15 @@
     len = strlen (s);
     r = qemu_malloc (len + sizeof (qemu_prefix));
 
-    if (r) {
-        size_t i;
-        char *u = r + sizeof (qemu_prefix) - 1;
+    u = r + sizeof (qemu_prefix) - 1;
 
-        strcpy (r, qemu_prefix);
-        strcat (r, s);
+    pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
+    pstrcat (r, len + sizeof (qemu_prefix), s);
 
-        for (i = 0; i < len; ++i) {
-            u[i] = toupper (u[i]);
-        }
+    for (i = 0; i < len; ++i) {
+        u[i] = qemu_toupper(u[i]);
     }
+
     return r;
 }
 
@@ -433,10 +431,10 @@
 {
     if (conf.log_to_monitor) {
         if (cap) {
-            term_printf ("%s: ", cap);
+            monitor_printf(cur_mon, "%s: ", cap);
         }
 
-        term_vprintf (fmt, ap);
+        monitor_vprintf(cur_mon, fmt, ap);
     }
     else {
         if (!VERBOSE_CHECK(audio))
@@ -536,7 +534,7 @@
 {
     char *optname;
     const char qemu_prefix[] = "QEMU_";
-    size_t preflen;
+    size_t preflen, optlen;
 
     if (audio_bug (AUDIO_FUNC, !prefix)) {
         dolog ("prefix = NULL\n");
@@ -564,21 +562,17 @@
         /* len of opt->name + len of prefix + size of qemu_prefix
          * (includes trailing zero) + zero + underscore (on behalf of
          * sizeof) */
-        optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
-        if (!optname) {
-            dolog ("Could not allocate memory for option name `%s'\n",
-                   opt->name);
-            continue;
-        }
+        optlen = len + preflen + sizeof (qemu_prefix) + 1;
+        optname = qemu_malloc (optlen);
 
-        strcpy (optname, qemu_prefix);
+        pstrcpy (optname, optlen, qemu_prefix);
 
         /* copy while upper-casing, including trailing zero */
         for (i = 0; i <= preflen; ++i) {
-            optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
+            optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
         }
-        strcat (optname, "_");
-        strcat (optname, opt->name);
+        pstrcat (optname, optlen, "_");
+        pstrcat (optname, optlen, opt->name);
 
         def = 1;
         switch (opt->tag) {
@@ -618,7 +612,7 @@
     }
 }
 
-static void audio_print_settings (audsettings_t *as)
+static void audio_print_settings (struct audsettings *as)
 {
     dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
 
@@ -635,6 +629,12 @@
     case AUD_FMT_U16:
         AUD_log (NULL, "U16");
         break;
+    case AUD_FMT_S32:
+        AUD_log (NULL, "S32");
+        break;
+    case AUD_FMT_U32:
+        AUD_log (NULL, "U32");
+        break;
     default:
         AUD_log (NULL, "invalid(%d)", as->fmt);
         break;
@@ -655,7 +655,7 @@
     AUD_log (NULL, "\n");
 }
 
-static int audio_validate_settings (audsettings_t *as)
+static int audio_validate_settings (struct audsettings *as)
 {
     int invalid;
 
@@ -679,7 +679,7 @@
     return invalid ? -1 : 0;
 }
 
-static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
+static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
 {
     int bits = 8, sign = 0;
 
@@ -694,6 +694,7 @@
     case AUD_FMT_U16:
         bits = 16;
         break;
+
     case AUD_FMT_S32:
         sign = 1;
     case AUD_FMT_U32:
@@ -707,7 +708,7 @@
         && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
 }
 
-void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
+void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
 {
     int bits = 8, sign = 0, shift = 0;
 
@@ -802,8 +803,8 @@
 /*
  * Capture
  */
-static void noop_conv (st_sample_t *dst, const void *src,
-                       int samples, volume_t *vol)
+static void noop_conv (struct st_sample *dst, const void *src,
+                       int samples, struct mixeng_volume *vol)
 {
     (void) src;
     (void) dst;
@@ -812,11 +813,11 @@
 }
 
 static CaptureVoiceOut *audio_pcm_capture_find_specific (
-    AudioState *s,
-    audsettings_t *as
+    struct audsettings *as
     )
 {
     CaptureVoiceOut *cap;
+    AudioState *s = &glob_audio_state;
 
     for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
         if (audio_pcm_info_eq (&cap->hw.info, as)) {
@@ -891,8 +892,9 @@
     }
 }
 
-static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
+static int audio_attach_capture (HWVoiceOut *hw)
 {
+    AudioState *s = &glob_audio_state;
     CaptureVoiceOut *cap;
 
     audio_detach_capture (hw);
@@ -989,7 +991,7 @@
 {
     HWVoiceIn *hw = sw->hw;
     int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
-    st_sample_t *src, *dst = sw->buf;
+    struct st_sample *src, *dst = sw->buf;
 
     rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
 
@@ -1236,6 +1238,7 @@
 
     hw = sw->hw;
     if (sw->active != on) {
+        AudioState *s = &glob_audio_state;
         SWVoiceOut *temp_sw;
         SWVoiceCap *sc;
 
@@ -1243,9 +1246,11 @@
             hw->pending_disable = 0;
             if (!hw->enabled) {
                 hw->enabled = 1;
+                if (s->vm_running) {
                 BEGIN_NOSIGALRM
                     hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
                 END_NOSIGALRM
+                }
             }
         }
         else {
@@ -1281,14 +1286,17 @@
 
     hw = sw->hw;
     if (sw->active != on) {
+        AudioState *s = &glob_audio_state;
         SWVoiceIn *temp_sw;
 
         if (on) {
             if (!hw->enabled) {
                 hw->enabled = 1;
+                if (s->vm_running) {
                 BEGIN_NOSIGALRM
                     hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
                 END_NOSIGALRM
+                }
             }
             sw->total_hw_samples_acquired = hw->total_samples_captured;
         }
@@ -1404,7 +1412,7 @@
     HWVoiceOut *hw = NULL;
     SWVoiceOut *sw;
 
-    while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
+    while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
         int played;
         int live, free, nb_live, cleanup_required, prev_rpos;
 
@@ -1503,7 +1511,7 @@
 #ifdef DEBUG_PLIVE
                     dolog ("Finishing with old voice\n");
 #endif
-                    audio_close_out (s, sw);
+                    audio_close_out (sw);
                 }
                 sw = sw1;
             }
@@ -1515,7 +1523,7 @@
 {
     HWVoiceIn *hw = NULL;
 
-    while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
+    while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
         SWVoiceIn *sw;
         int captured, min;
 
@@ -1556,7 +1564,7 @@
         while (live) {
             int left = hw->samples - rpos;
             int to_capture = audio_MIN (live, left);
-            st_sample_t *src;
+            struct st_sample *src;
             struct capture_callback *cb;
 
             src = hw->mix_buf + rpos;
@@ -1591,7 +1599,7 @@
 
 static void audio_timer (void *opaque)
 {
-    AudioState* s = opaque;
+    AudioState *s = opaque;
 #if 0
 #define  MAX_DIFFS  1000
     int64_t         now  = qemu_get_clock(vm_clock);
@@ -1662,7 +1670,7 @@
      "Number of voices for ADC", NULL, 0},
 
     /* Misc */
-    {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
+    {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hertz,
      "Timer period in HZ (0 - use lowest possible)", NULL, 0},
 
     {"PLIVE", AUD_OPT_BOOL, &conf.plive,
@@ -1698,7 +1706,7 @@
     size_t i;
 
     audio_process_options ("AUDIO", audio_options);
-    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
+    for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
         struct audio_driver *d = drvtab[i];
         if (d->options) {
             audio_process_options (d->name, d->options);
@@ -1711,7 +1719,7 @@
 
     printf ("Available drivers:\n");
 
-    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
+    for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
         struct audio_driver *d = drvtab[i];
 
         printf ("Name: %s\n", d->name);
@@ -1773,8 +1781,8 @@
     END_NOSIGALRM
 
     if (opaque != NULL) {
-        audio_init_nb_voices_out (s, drv);
-        audio_init_nb_voices_in (s, drv);
+        audio_init_nb_voices_out (drv);
+        audio_init_nb_voices_in (drv);
         if (out) {
             s->drv_out         = drv;
             s->drv_out_opaque  = opaque;
@@ -1790,19 +1798,21 @@
     }
 }
 
-static void audio_vm_change_state_handler (void *opaque, int running)
+static void audio_vm_change_state_handler (void *opaque, int running,
+                                           int reason)
 {
     AudioState *s = opaque;
     HWVoiceOut *hwo = NULL;
     HWVoiceIn *hwi = NULL;
     int op = running ? VOICE_ENABLE : VOICE_DISABLE;
 
+    s->vm_running = running;
     BEGIN_NOSIGALRM
-        while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
+        while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
             hwo->pcm_ops->ctl_out (hwo, op);
         }
 
-        while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
+        while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
             hwi->pcm_ops->ctl_in (hwi, op);
         }
     END_NOSIGALRM
@@ -1821,7 +1831,7 @@
     initialized = 0;
 
     BEGIN_NOSIGALRM
-    while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
+    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
         SWVoiceCap *sc;
 
         hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
@@ -1837,7 +1847,7 @@
         }
     }
 
-    while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
+    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
         hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
         hwi->pcm_ops->fini_in (hwi);
     }
@@ -1869,21 +1879,6 @@
     return 0;
 }
 
-void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
-{
-    card->audio = s;
-    card->name = qemu_strdup (name);
-    memset (&card->entries, 0, sizeof (card->entries));
-    LIST_INSERT_HEAD (&s->card_head, card, entries);
-}
-
-void AUD_remove_card (QEMUSoundCard *card)
-{
-    LIST_REMOVE (card, entries);
-    card->audio = NULL;
-    qemu_free (card->name);
-}
-
 static int
 find_audio_driver( AudioState*  s, int  out )
 {
@@ -1946,10 +1941,14 @@
 }
 
 
-AudioState *AUD_init (void)
+static void audio_init (void)
 {
     AudioState *s = &glob_audio_state;
 
+    if (s->drv_out && s->drv_in) {
+        return;
+    }
+
     LIST_INIT (&s->hw_head_out);
     LIST_INIT (&s->hw_head_in);
     LIST_INIT (&s->cap_head);
@@ -1958,7 +1957,7 @@
     s->ts = qemu_new_timer (vm_clock, audio_timer, s);
     if (!s->ts) {
         dolog ("Could not create audio timer\n");
-        return NULL;
+        return;
     }
 
     audio_process_options ("AUDIO", audio_options);
@@ -1978,32 +1977,29 @@
         s->nb_hw_voices_in = 0;
     }
 
-    if ( find_audio_driver (s, 0) == 0 &&
-         find_audio_driver (s, 1) == 0 )
-    {
-        VMChangeStateEntry *e;
-
-        if (conf.period.hz <= 0) {
-            if (conf.period.hz < 0) {
-                dolog ("warning: Timer period is negative - %d "
-                       "treating as zero\n",
-                       conf.period.hz);
-            }
-            conf.period.ticks = 1;
-        }
-        else {
-            conf.period.ticks = ticks_per_sec / conf.period.hz;
-        }
-
-        e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
-        if (!e) {
-            dolog ("warning: Could not register change state handler\n"
-                   "(Audio can continue looping even after stopping the VM)\n");
-        }
-    }
-    else {
+    if ( find_audio_driver (s, 0) != 0 ||
+         find_audio_driver (s, 1) != 0 ) {
         qemu_del_timer (s->ts);
-        return NULL;
+        return;
+    }
+
+    VMChangeStateEntry *e;
+
+    if (conf.period.hertz <= 0) {
+        if (conf.period.hertz < 0) {
+            dolog ("warning: Timer period is negative - %d "
+                   "treating as zero\n",
+                   conf.period.hertz);
+        }
+        conf.period.ticks = 1;
+    } else {
+        conf.period.ticks = ticks_per_sec / conf.period.hertz;
+    }
+
+    e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
+    if (!e) {
+        dolog ("warning: Could not register change state handler\n"
+               "(Audio can continue looping even after stopping the VM)\n");
     }
 
     initialized = 1;
@@ -2011,7 +2007,20 @@
     LIST_INIT (&s->card_head);
     register_savevm ("audio", 0, 1, audio_save, audio_load, s);
     qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
-    return s;
+}
+
+void AUD_register_card (const char *name, QEMUSoundCard *card)
+{
+    audio_init ();
+    card->name = qemu_strdup (name);
+    memset (&card->entries, 0, sizeof (card->entries));
+    LIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
+}
+
+void AUD_remove_card (QEMUSoundCard *card)
+{
+    LIST_REMOVE (card, entries);
+    qemu_free (card->name);
 }
 
 // this was added to work around a deadlock in SDL when quitting
@@ -2021,20 +2030,15 @@
 }
 
 CaptureVoiceOut *AUD_add_capture (
-    AudioState *s,
-    audsettings_t *as,
+    struct audsettings *as,
     struct audio_capture_ops *ops,
     void *cb_opaque
     )
 {
+    AudioState *s = &glob_audio_state;
     CaptureVoiceOut *cap;
     struct capture_callback *cb;
 
-    if (!s) {
-        /* XXX suppress */
-        s = &glob_audio_state;
-    }
-
     if (audio_validate_settings (as)) {
         dolog ("Invalid settings were passed when trying to add capture\n");
         audio_print_settings (as);
@@ -2050,7 +2054,7 @@
     cb->ops = *ops;
     cb->opaque = cb_opaque;
 
-    cap = audio_pcm_capture_find_specific (s, as);
+    cap = audio_pcm_capture_find_specific (as);
     if (cap) {
         LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
         return cap;
@@ -2073,7 +2077,7 @@
         /* XXX find a more elegant way */
         hw->samples = 4096 * 4;
         hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
-                                    sizeof (st_sample_t));
+                                    sizeof (struct st_sample));
         if (!hw->mix_buf) {
             dolog ("Could not allocate capture mix buffer (%d samples)\n",
                    hw->samples);
@@ -2100,8 +2104,8 @@
         LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
 
         hw = NULL;
-        while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
-            audio_attach_capture (s, hw);
+        while ((hw = audio_pcm_hw_find_any_out (hw))) {
+            audio_attach_capture (hw);
         }
         return cap;
 
diff --git a/audio/audio.h b/audio/audio.h
index 2c347bf..208a811 100644
--- a/audio/audio.h
+++ b/audio/audio.h
@@ -45,12 +45,12 @@
 #define AUDIO_HOST_ENDIANNESS 0
 #endif
 
-typedef struct {
+struct audsettings {
     int freq;
     int nchannels;
     audfmt_e fmt;
     int endianness;
-} audsettings_t;
+};
 
 typedef enum {
     AUD_CNOTIFY_ENABLE,
@@ -79,7 +79,6 @@
 typedef struct SWVoiceIn SWVoiceIn;
 
 typedef struct QEMUSoundCard {
-    AudioState *audio;
     char *name;
     LIST_ENTRY (QEMUSoundCard) entries;
 } QEMUSoundCard;
@@ -95,15 +94,11 @@
 #endif
     ;
 
-extern AudioState glob_audio_state;
-
-AudioState *AUD_init (void);
 void AUD_help (void);
-void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card);
+void AUD_register_card (const char *name, QEMUSoundCard *card);
 void AUD_remove_card (QEMUSoundCard *card);
 CaptureVoiceOut *AUD_add_capture (
-    AudioState *s,
-    audsettings_t *as,
+    struct audsettings *as,
     struct audio_capture_ops *ops,
     void *opaque
     );
@@ -115,7 +110,7 @@
     const char *name,
     void *callback_opaque,
     audio_callback_fn_t callback_fn,
-    audsettings_t *settings
+    struct audsettings *settings
     );
 
 void AUD_close_out (QEMUSoundCard *card, SWVoiceOut *sw);
@@ -136,7 +131,7 @@
     const char *name,
     void *callback_opaque,
     audio_callback_fn_t callback_fn,
-    audsettings_t *settings
+    struct audsettings *settings
     );
 
 void AUD_close_in (QEMUSoundCard *card, SWVoiceIn *sw);
@@ -173,6 +168,9 @@
 #define audio_MAX(a, b) ((a)<(b)?(b):(a))
 #endif
 
+int wav_start_capture (CaptureState *s, const char *path, int freq,
+                       int bits, int nchannels);
+
 extern int
 audio_get_backend_count( int  is_input );
 
diff --git a/audio/audio_int.h b/audio/audio_int.h
index 6677ec1..54b0f5a 100644
--- a/audio/audio_int.h
+++ b/audio/audio_int.h
@@ -78,7 +78,7 @@
     int rpos;
     uint64_t ts_helper;
 
-    st_sample_t *mix_buf;
+    struct st_sample *mix_buf;
 
     int samples;
     LIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
@@ -97,7 +97,7 @@
     int total_samples_captured;
     uint64_t ts_helper;
 
-    st_sample_t *conv_buf;
+    struct st_sample *conv_buf;
 
     int samples;
     LIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
@@ -106,32 +106,34 @@
 } HWVoiceIn;
 
 struct SWVoiceOut {
+    QEMUSoundCard *card;
     struct audio_pcm_info info;
     t_sample *conv;
     int64_t ratio;
-    st_sample_t *buf;
+    struct st_sample *buf;
     void *rate;
     int total_hw_samples_mixed;
     int active;
     int empty;
     HWVoiceOut *hw;
     char *name;
-    volume_t vol;
+    struct mixeng_volume vol;
     struct audio_callback callback;
     LIST_ENTRY (SWVoiceOut) entries;
 };
 
 struct SWVoiceIn {
+    QEMUSoundCard *card;
     int active;
     struct audio_pcm_info info;
     int64_t ratio;
     void *rate;
     int total_hw_samples_acquired;
-    st_sample_t *buf;
+    struct st_sample *buf;
     f_sample *clip;
     HWVoiceIn *hw;
     char *name;
-    volume_t vol;
+    struct mixeng_volume vol;
     struct audio_callback callback;
     LIST_ENTRY (SWVoiceIn) entries;
 };
@@ -151,13 +153,13 @@
 };
 
 struct audio_pcm_ops {
-    int  (*init_out)(HWVoiceOut *hw, audsettings_t *as);
+    int  (*init_out)(HWVoiceOut *hw, struct audsettings *as);
     void (*fini_out)(HWVoiceOut *hw);
     int  (*run_out) (HWVoiceOut *hw);
     int  (*write)   (SWVoiceOut *sw, void *buf, int size);
     int  (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
 
-    int  (*init_in) (HWVoiceIn *hw, audsettings_t *as);
+    int  (*init_in) (HWVoiceIn *hw, struct audsettings *as);
     void (*fini_in) (HWVoiceIn *hw);
     int  (*run_in)  (HWVoiceIn *hw);
     int  (*read)    (SWVoiceIn *sw, void *buf, int size);
@@ -196,6 +198,7 @@
     LIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
     int nb_hw_voices_out;
     int nb_hw_voices_in;
+    int vm_running;
 };
 
 extern struct audio_driver no_audio_driver;
@@ -204,14 +207,14 @@
 extern struct audio_driver win_audio_driver;
 extern struct audio_driver wav_audio_driver;
 extern struct audio_driver fmod_audio_driver;
-extern struct audio_driver esd_audio_driver;
 extern struct audio_driver alsa_audio_driver;
 extern struct audio_driver coreaudio_audio_driver;
 extern struct audio_driver dsound_audio_driver;
 extern struct audio_driver esd_audio_driver;
-extern volume_t nominal_volume;
+extern struct audio_driver pa_audio_driver;
+extern struct mixeng_volume nominal_volume;
 
-void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as);
+void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
 
 int  audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int len);
diff --git a/audio/audio_template.h b/audio/audio_template.h
index 6f8e166..d6c1037 100644
--- a/audio/audio_template.h
+++ b/audio/audio_template.h
@@ -36,11 +36,9 @@
 #define HWBUF hw->conv_buf
 #endif
 
-static void glue (audio_init_nb_voices_, TYPE) (
-    AudioState *s,
-    struct audio_driver *drv
-    )
+static void glue (audio_init_nb_voices_, TYPE) (struct audio_driver *drv)
 {
+    AudioState *s = &glob_audio_state;
     int max_voices = glue (drv->max_voices_, TYPE);
     int voice_size = glue (drv->voice_size_, TYPE);
 
@@ -82,7 +80,7 @@
 
 static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
 {
-    HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
+    HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (struct st_sample));
     if (!HWBUF) {
         dolog ("Could not allocate " NAME " buffer (%d samples)\n",
                hw->samples);
@@ -116,7 +114,7 @@
     samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
 #endif
 
-    sw->buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t));
+    sw->buf = audio_calloc (AUDIO_FUNC, samples, sizeof (struct st_sample));
     if (!sw->buf) {
         dolog ("Could not allocate buffer for `%s' (%d samples)\n",
                SW_NAME (sw), samples);
@@ -140,7 +138,7 @@
     SW *sw,
     HW *hw,
     const char *name,
-    audsettings_t *as
+    struct audsettings *as
     )
 {
     int err;
@@ -194,8 +192,9 @@
     LIST_REMOVE (sw, entries);
 }
 
-static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp)
+static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
 {
+    AudioState *s = &glob_audio_state;
     HW *hw = *hwp;
 
     if (!hw->sw_head.lh_first) {
@@ -213,14 +212,15 @@
     }
 }
 
-static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw)
+static HW *glue (audio_pcm_hw_find_any_, TYPE) (HW *hw)
 {
-    return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first;
+    AudioState *s = &glob_audio_state;
+    return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
 }
 
-static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw)
+static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (HW *hw)
 {
-    while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
+    while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (hw))) {
         if (hw->enabled) {
             return hw;
         }
@@ -229,12 +229,11 @@
 }
 
 static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
-    AudioState *s,
     HW *hw,
-    audsettings_t *as
+    struct audsettings *as
     )
 {
-    while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
+    while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (hw))) {
         if (audio_pcm_info_eq (&hw->info, as)) {
             return hw;
         }
@@ -242,9 +241,10 @@
     return NULL;
 }
 
-static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s, audsettings_t *as)
+static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
 {
     HW *hw;
+    AudioState *s = &glob_audio_state;
     struct audio_driver *drv = glue(s->drv_, TYPE);
     int  err;
 
@@ -302,7 +302,7 @@
     LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
     glue (s->nb_hw_voices_, TYPE) -= 1;
 #ifdef DAC
-    audio_attach_capture (s, hw);
+    audio_attach_capture (hw);
 #endif
     return hw;
 
@@ -315,39 +315,38 @@
     return NULL;
 }
 
-static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s, audsettings_t *as)
+static HW *glue (audio_pcm_hw_add_, TYPE) (struct audsettings *as)
 {
     HW *hw;
 
     if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
-        hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
+        hw = glue (audio_pcm_hw_add_new_, TYPE) (as);
         if (hw) {
             return hw;
         }
     }
 
-    hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as);
+    hw = glue (audio_pcm_hw_find_specific_, TYPE) (NULL, as);
     if (hw) {
         return hw;
     }
 
-    hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
+    hw = glue (audio_pcm_hw_add_new_, TYPE) (as);
     if (hw) {
         return hw;
     }
 
-    return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL);
+    return glue (audio_pcm_hw_find_any_, TYPE) (NULL);
 }
 
 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
-    AudioState *s,
     const char *sw_name,
-    audsettings_t *as
+    struct audsettings *as
     )
 {
     SW *sw;
     HW *hw;
-    audsettings_t hw_as;
+    struct audsettings hw_as;
 
     if (glue (conf.fixed_, TYPE).enabled) {
         hw_as = glue (conf.fixed_, TYPE).settings;
@@ -363,7 +362,7 @@
         goto err1;
     }
 
-    hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as);
+    hw = glue (audio_pcm_hw_add_, TYPE) (&hw_as);
     if (!hw) {
         goto err2;
     }
@@ -378,31 +377,30 @@
 
 err3:
     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
-    glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
+    glue (audio_pcm_hw_gc_, TYPE) (&hw);
 err2:
     qemu_free (sw);
 err1:
     return NULL;
 }
 
-static void glue (audio_close_, TYPE) (AudioState *s, SW *sw)
+static void glue (audio_close_, TYPE) (SW *sw)
 {
     glue (audio_pcm_sw_fini_, TYPE) (sw);
     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
-    glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw);
+    glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
     qemu_free (sw);
 }
 
 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
 {
     if (sw) {
-        if (audio_bug (AUDIO_FUNC, !card || !card->audio)) {
-            dolog ("card=%p card->audio=%p\n",
-                   card, card ? card->audio : NULL);
+        if (audio_bug (AUDIO_FUNC, !card)) {
+            dolog ("card=%p\n", card);
             return;
         }
 
-        glue (audio_close_, TYPE) (card->audio, sw);
+        glue (audio_close_, TYPE) (sw);
     }
 }
 
@@ -412,10 +410,10 @@
     const char *name,
     void *callback_opaque ,
     audio_callback_fn_t callback_fn,
-    audsettings_t *as
+    struct audsettings *as
     )
 {
-    AudioState *s;
+    AudioState *s = &glob_audio_state;
 #ifdef DAC
     int live = 0;
     SW *old_sw = NULL;
@@ -424,15 +422,12 @@
     ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
             name, as->freq, as->nchannels, as->fmt);
 
-    if (audio_bug (AUDIO_FUNC,
-                   !card || !card->audio || !name || !callback_fn || !as)) {
-        dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
-               card, card ? card->audio : NULL, name, callback_fn, as);
+    if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
+        dolog ("card=%p name=%p callback_fn=%p as=%p\n",
+               card, name, callback_fn, as);
         goto fail;
     }
 
-    s = card->audio;
-
     if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
         audio_print_settings (as);
         goto fail;
@@ -490,7 +485,7 @@
         }
     }
     else {
-        sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
+        sw = glue (audio_pcm_create_voice_pair_, TYPE) (name, as);
         if (!sw) {
             dolog ("Failed to create voice `%s'\n", name);
             return NULL;
@@ -498,6 +493,7 @@
     }
 
     if (sw) {
+        sw->card = card;
         sw->vol = nominal_volume;
         sw->callback.fn = callback_fn;
         sw->callback.opaque = callback_opaque;
diff --git a/audio/coreaudio.c b/audio/coreaudio.c
index f23ebee..8abe0c4 100644
--- a/audio/coreaudio.c
+++ b/audio/coreaudio.c
@@ -283,7 +283,7 @@
 
 static int
 coreaudio_voice_init (coreaudioVoice*    core,
-                      audsettings_t*     as,
+                      struct audsettings*  as,
                       int                frameSize,
                       AudioDeviceIOProc  ioproc,
                       void*              hw,
@@ -510,7 +510,7 @@
     HWVoiceOut *hw = hwptr;
     coreaudioVoice *core = CORE_OUT(hw);
     int rpos, live;
-    st_sample_t *src;
+    struct st_sample *src;
 #ifndef FLOAT_MIXENG
 #ifdef RECIPROCAL
     const float scale = 1.f / UINT_MAX;
@@ -568,7 +568,7 @@
 }
 
 static int
-coreaudio_init_out (HWVoiceOut *hw, audsettings_t *as)
+coreaudio_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     coreaudioVoice*  core = CORE_OUT(hw);
     int              err;
@@ -654,7 +654,7 @@
     HWVoiceIn *hw = hwptr;
     coreaudioVoice *core = CORE_IN(hw);
     int wpos, avail;
-    st_sample_t *dst;
+    struct st_sample *dst;
 #ifndef FLOAT_MIXENG
 #ifdef RECIPROCAL
     const float scale = 1.f / UINT_MAX;
@@ -718,7 +718,7 @@
 }
 
 static int
-coreaudio_init_in (HWVoiceIn *hw, audsettings_t *as)
+coreaudio_init_in (HWVoiceIn *hw, struct audsettings *as)
 {
     coreaudioVoice*  core = CORE_IN(hw);
     int              err;
diff --git a/audio/esdaudio.c b/audio/esdaudio.c
index a06176e..1d72125 100644
--- a/audio/esdaudio.c
+++ b/audio/esdaudio.c
@@ -22,8 +22,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-
 #include <esd.h>
+#include "qemu-common.h"
 #include "audio.h"
 #include <signal.h>
 
@@ -122,7 +122,7 @@
     while (liveSamples > 0) {
         int  chunkSamples = audio_MIN (liveSamples, hw->samples - rpos);
         int  chunkBytes   = chunkSamples << hw->info.shift;
-        st_sample_t *src = hw->mix_buf + rpos;
+        struct st_sample *src = hw->mix_buf + rpos;
 
         hw->clip (esd->pcm_buf, src, chunkSamples);
 
@@ -142,7 +142,7 @@
 
         writeSamples = nwrite >> hw->info.shift;
         writeBytes   = writeSamples << hw->info.shift;
-        if (writeSamples != writeBytes) {
+        if (writeBytes != nwrite) {
             dolog ("warning: Misaligned write %d (requested %d), "
                     "alignment %d\n",
                     nwrite, writeBytes, hw->info.align + 1);
@@ -160,10 +160,10 @@
     return audio_pcm_sw_write (sw, buf, len);
 }
 
-static int qesd_init_out (HWVoiceOut *hw, audsettings_t *as)
+static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     ESDVoiceOut *esd = (ESDVoiceOut *) hw;
-    audsettings_t obt_as = *as;
+    struct audsettings obt_as = *as;
     int esdfmt = ESD_STREAM | ESD_PLAY;
     int result = -1;
 
@@ -318,10 +318,10 @@
     return audio_pcm_sw_read (sw, buf, len);
 }
 
-static int qesd_init_in (HWVoiceIn *hw, audsettings_t *as)
+static int qesd_init_in (HWVoiceIn *hw, struct audsettings *as)
 {
     ESDVoiceIn *esd = (ESDVoiceIn *) hw;
-    audsettings_t obt_as = *as;
+    struct audsettings obt_as = *as;
     int esdfmt = ESD_STREAM | ESD_RECORD;
     int result = -1;
 
@@ -342,6 +342,7 @@
         esdfmt |= ESD_BITS16;
         obt_as.fmt = AUD_FMT_S16;
         break;
+
     case AUD_FMT_S32:
     case AUD_FMT_U32:
         dolog ("Will use 16 instead of 32 bit samples\n");
@@ -487,7 +488,7 @@
     {NULL, 0, NULL, NULL, NULL, 0}
 };
 
-struct audio_pcm_ops qesd_pcm_ops = {
+static struct audio_pcm_ops qesd_pcm_ops = {
     qesd_init_out,
     qesd_fini_out,
     qesd_run_out,
diff --git a/audio/fmodaudio.c b/audio/fmodaudio.c
index e230e8b..0becd3b 100644
--- a/audio/fmodaudio.c
+++ b/audio/fmodaudio.c
@@ -23,6 +23,7 @@
  */
 #include <fmod.h>
 #include <fmod_errors.h>
+#include "qemu-common.h"
 #include "audio.h"
 
 #define AUDIO_CAP "fmod"
@@ -141,8 +142,8 @@
     int src_len1 = dst_len;
     int src_len2 = 0;
     int pos = hw->rpos + dst_len;
-    st_sample_t *src1 = hw->mix_buf + hw->rpos;
-    st_sample_t *src2 = NULL;
+    struct st_sample *src1 = hw->mix_buf + hw->rpos;
+    struct st_sample *src2 = NULL;
 
     if (pos > hw->samples) {
         src_len1 = hw->samples - hw->rpos;
@@ -354,11 +355,11 @@
     }
 }
 
-static int fmod_init_out (HWVoiceOut *hw, audsettings_t *as)
+static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     int bits16, mode, channel;
     FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
-    audsettings_t obt_as = *as;
+    struct audsettings obt_as = *as;
 
     mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
     fmd->fmod_sample = FSOUND_Sample_Alloc (
@@ -416,11 +417,11 @@
     return 0;
 }
 
-static int fmod_init_in (HWVoiceIn *hw, audsettings_t *as)
+static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
 {
     int bits16, mode;
     FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
-    audsettings_t obt_as = *as;
+    struct audsettings obt_as = *as;
 
     if (conf.broken_adc) {
         return -1;
@@ -563,7 +564,7 @@
 
     if (drv) {
         int found = 0;
-        for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
+        for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
             if (!strcmp (drv, drvtab[i].name)) {
                 output_type = drvtab[i].type;
                 found = 1;
@@ -573,7 +574,7 @@
         if (!found) {
             dolog ("Unknown FMOD driver `%s'\n", drv);
             dolog ("Valid drivers:\n");
-            for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
+            for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
                 dolog ("  %s\n", drvtab[i].name);
             }
         }
diff --git a/audio/mixeng.c b/audio/mixeng.c
index 34fc6df..8ce942e 100644
--- a/audio/mixeng.c
+++ b/audio/mixeng.c
@@ -22,13 +22,12 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+#include "qemu-common.h"
 #include "audio.h"
 
 #define AUDIO_CAP "mixeng"
 #include "audio_int.h"
 
-#define NOVOL
-
 /* 8 bit */
 #define ENDIAN_CONVERSION natural
 #define ENDIAN_CONVERT(v) (v)
@@ -291,7 +290,7 @@
     uint64_t opos;
     uint64_t opos_inc;
     uint32_t ipos;              /* position in the input stream (integer) */
-    st_sample_t ilast;          /* last sample in the input stream */
+    struct st_sample ilast;          /* last sample in the input stream */
 };
 
 /*
@@ -330,7 +329,7 @@
     qemu_free (opaque);
 }
 
-void mixeng_clear (st_sample_t *buf, int len)
+void mixeng_clear (struct st_sample *buf, int len)
 {
-    memset (buf, 0, len * sizeof (st_sample_t));
+    memset (buf, 0, len * sizeof (struct st_sample));
 }
diff --git a/audio/mixeng.h b/audio/mixeng.h
index 95b68df..4af1dd9 100644
--- a/audio/mixeng.h
+++ b/audio/mixeng.h
@@ -25,27 +25,27 @@
 #define QEMU_MIXENG_H
 
 #ifdef FLOAT_MIXENG
-typedef float real_t;
-typedef struct { int mute; real_t r; real_t l; } volume_t;
-typedef struct { real_t l; real_t r; } st_sample_t;
+typedef float mixeng_real;
+struct mixeng_volume { int mute; mixeng_real r; mixeng_real l; };
+struct st_sample { mixeng_real l; mixeng_real r; };
 #else
-typedef struct { int mute; int64_t r; int64_t l; } volume_t;
-typedef struct { int64_t l; int64_t r; } st_sample_t;
+struct mixeng_volume { int mute; int64_t r; int64_t l; };
+struct st_sample { int64_t l; int64_t r; };
 #endif
 
-typedef void (t_sample) (st_sample_t *dst, const void *src,
-                         int samples, volume_t *vol);
-typedef void (f_sample) (void *dst, const st_sample_t *src, int samples);
+typedef void (t_sample) (struct st_sample *dst, const void *src,
+                         int samples, struct mixeng_volume *vol);
+typedef void (f_sample) (void *dst, const struct st_sample *src, int samples);
 
 extern t_sample *mixeng_conv[2][2][2][3];
 extern f_sample *mixeng_clip[2][2][2][3];
 
 void *st_rate_start (int inrate, int outrate);
-void st_rate_flow (void *opaque, st_sample_t *ibuf, st_sample_t *obuf,
+void st_rate_flow (void *opaque, struct st_sample *ibuf, struct st_sample *obuf,
                    int *isamp, int *osamp);
-void st_rate_flow_mix (void *opaque, st_sample_t *ibuf, st_sample_t *obuf,
+void st_rate_flow_mix (void *opaque, struct st_sample *ibuf, struct st_sample *obuf,
                        int *isamp, int *osamp);
 void st_rate_stop (void *opaque);
-void mixeng_clear (st_sample_t *buf, int len);
+void mixeng_clear (struct st_sample *buf, int len);
 
 #endif  /* mixeng.h */
diff --git a/audio/mixeng_template.h b/audio/mixeng_template.h
index d726441..5617705 100644
--- a/audio/mixeng_template.h
+++ b/audio/mixeng_template.h
@@ -31,39 +31,39 @@
 #define HALF (IN_MAX >> 1)
 #endif
 
-#ifdef NOVOL
-#define VOL(a, b) a
-#else
+#ifdef CONFIG_MIXEMU
 #ifdef FLOAT_MIXENG
 #define VOL(a, b) ((a) * (b))
 #else
 #define VOL(a, b) ((a) * (b)) >> 32
 #endif
+#else
+#define VOL(a, b) a
 #endif
 
 #define ET glue (ENDIAN_CONVERSION, glue (_, IN_T))
 
 #ifdef FLOAT_MIXENG
-static real_t inline glue (conv_, ET) (IN_T v)
+static mixeng_real inline glue (conv_, ET) (IN_T v)
 {
     IN_T nv = ENDIAN_CONVERT (v);
 
 #ifdef RECIPROCAL
 #ifdef SIGNED
-    return nv * (1.f / (real_t) (IN_MAX - IN_MIN));
+    return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN));
 #else
-    return (nv - HALF) * (1.f / (real_t) IN_MAX);
+    return (nv - HALF) * (1.f / (mixeng_real) IN_MAX);
 #endif
 #else  /* !RECIPROCAL */
 #ifdef SIGNED
-    return nv / (real_t) (IN_MAX - IN_MIN);
+    return nv / (mixeng_real) (IN_MAX - IN_MIN);
 #else
-    return (nv - HALF) / (real_t) IN_MAX;
+    return (nv - HALF) / (mixeng_real) IN_MAX;
 #endif
 #endif
 }
 
-static IN_T inline glue (clip_, ET) (real_t v)
+static IN_T inline glue (clip_, ET) (mixeng_real v)
 {
     if (v >= 0.5) {
         return IN_MAX;
@@ -109,11 +109,11 @@
 #endif
 
 static void glue (glue (conv_, ET), _to_stereo)
-    (st_sample_t *dst, const void *src, int samples, volume_t *vol)
+    (struct st_sample *dst, const void *src, int samples, struct mixeng_volume *vol)
 {
-    st_sample_t *out = dst;
+    struct st_sample *out = dst;
     IN_T *in = (IN_T *) src;
-#ifndef NOVOL
+#ifdef CONFIG_MIXEMU
     if (vol->mute) {
         mixeng_clear (dst, samples);
         return;
@@ -129,11 +129,11 @@
 }
 
 static void glue (glue (conv_, ET), _to_mono)
-    (st_sample_t *dst, const void *src, int samples, volume_t *vol)
+    (struct st_sample *dst, const void *src, int samples, struct mixeng_volume *vol)
 {
-    st_sample_t *out = dst;
+    struct st_sample *out = dst;
     IN_T *in = (IN_T *) src;
-#ifndef NOVOL
+#ifdef CONFIG_MIXEMU
     if (vol->mute) {
         mixeng_clear (dst, samples);
         return;
@@ -150,9 +150,9 @@
 }
 
 static void glue (glue (clip_, ET), _from_stereo)
-    (void *dst, const st_sample_t *src, int samples)
+    (void *dst, const struct st_sample *src, int samples)
 {
-    const st_sample_t *in = src;
+    const struct st_sample *in = src;
     IN_T *out = (IN_T *) dst;
     while (samples--) {
         *out++ = glue (clip_, ET) (in->l);
@@ -162,9 +162,9 @@
 }
 
 static void glue (glue (clip_, ET), _from_mono)
-    (void *dst, const st_sample_t *src, int samples)
+    (void *dst, const struct st_sample *src, int samples)
 {
-    const st_sample_t *in = src;
+    const struct st_sample *in = src;
     IN_T *out = (IN_T *) dst;
     while (samples--) {
         *out++ = glue (clip_, ET) (in->l + in->r);
diff --git a/audio/noaudio.c b/audio/noaudio.c
index 8788a41..7451653 100644
--- a/audio/noaudio.c
+++ b/audio/noaudio.c
@@ -21,6 +21,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+#include "qemu-common.h"
+#include "audio.h"
 #include "qemu-timer.h"
 
 #define AUDIO_CAP "noaudio"
@@ -66,7 +68,7 @@
     return audio_pcm_sw_write (sw, buf, len);
 }
 
-static int no_init_out (HWVoiceOut *hw, audsettings_t *as)
+static int no_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     audio_pcm_init_info (&hw->info, as);
     hw->samples = 1024;
@@ -85,7 +87,7 @@
     return 0;
 }
 
-static int no_init_in (HWVoiceIn *hw, audsettings_t *as)
+static int no_init_in (HWVoiceIn *hw, struct audsettings *as)
 {
     audio_pcm_init_info (&hw->info, as);
     hw->samples = 1024;
diff --git a/audio/ossaudio.c b/audio/ossaudio.c
index 2ccaade..f946f79 100644
--- a/audio/ossaudio.c
+++ b/audio/ossaudio.c
@@ -21,10 +21,17 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+#include <stdlib.h>
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/ioctl.h>
+#ifdef __OpenBSD__
+#include <soundcard.h>
+#else
 #include <sys/soundcard.h>
+#endif
+#include "qemu-common.h"
+#include "audio.h"
 
 #define AUDIO_CAP "oss"
 #include "audio_int.h"
@@ -143,7 +150,7 @@
 {
     switch (ossfmt) {
     case AFMT_S8:
-        *endianness =0;
+        *endianness = 0;
         *fmt = AUD_FMT_S8;
         break;
 
@@ -248,8 +255,8 @@
     }
 
     if (!abinfo.fragstotal || !abinfo.fragsize) {
-        AUD_log(AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
-                abinfo.fragstotal, abinfo.fragsize, typ);
+        AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
+                 abinfo.fragstotal, abinfo.fragsize, typ);
         goto err;
     }
 
@@ -287,7 +294,7 @@
     int err, rpos, live, decr;
     int samples;
     uint8_t *dst;
-    st_sample_t *src;
+    struct st_sample *src;
     struct audio_buf_info abinfo;
     struct count_info cntinfo;
     int bufsize;
@@ -427,7 +434,7 @@
     }
 }
 
-static int oss_init_out (HWVoiceOut *hw, audsettings_t *as)
+static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     OSSVoiceOut *oss = (OSSVoiceOut *) hw;
     struct oss_params req, obt;
@@ -435,7 +442,7 @@
     int err;
     int fd;
     audfmt_e effective_fmt;
-    audsettings_t obt_as;
+    struct audsettings obt_as;
 
     oss->fd = -1;
 
@@ -569,7 +576,7 @@
     return 0;
 }
 
-static int oss_init_in (HWVoiceIn *hw, audsettings_t *as)
+static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
 {
     OSSVoiceIn *oss = (OSSVoiceIn *) hw;
     struct oss_params req, obt;
@@ -577,7 +584,7 @@
     int err;
     int fd;
     audfmt_e effective_fmt;
-    audsettings_t obt_as;
+    struct audsettings obt_as;
 
     oss->fd = -1;
 
diff --git a/audio/rate_template.h b/audio/rate_template.h
index 398d305..bd4b1c7 100644
--- a/audio/rate_template.h
+++ b/audio/rate_template.h
@@ -27,15 +27,15 @@
  * Processed signed long samples from ibuf to obuf.
  * Return number of samples processed.
  */
-void NAME (void *opaque, st_sample_t *ibuf, st_sample_t *obuf,
+void NAME (void *opaque, struct st_sample *ibuf, struct st_sample *obuf,
            int *isamp, int *osamp)
 {
     struct rate *rate = opaque;
-    st_sample_t *istart, *iend;
-    st_sample_t *ostart, *oend;
-    st_sample_t ilast, icur, out;
+    struct st_sample *istart, *iend;
+    struct st_sample *ostart, *oend;
+    struct st_sample ilast, icur, out;
 #ifdef FLOAT_MIXENG
-    real_t t;
+    mixeng_real t;
 #else
     int64_t t;
 #endif
@@ -84,7 +84,7 @@
 #ifdef RECIPROCAL
         t = (rate->opos & UINT_MAX) * (1.f / UINT_MAX);
 #else
-        t = (rate->opos & UINT_MAX) / (real_t) UINT_MAX;
+        t = (rate->opos & UINT_MAX) / (mixeng_real) UINT_MAX;
 #endif
         out.l = (ilast.l * (1.0 - t)) + icur.l * t;
         out.r = (ilast.r * (1.0 - t)) + icur.r * t;
diff --git a/audio/sdlaudio.c b/audio/sdlaudio.c
index ea5bccd..3e88785 100644
--- a/audio/sdlaudio.c
+++ b/audio/sdlaudio.c
@@ -23,10 +23,14 @@
  */
 #include <SDL.h>
 #include <SDL_thread.h>
+#include "qemu-common.h"
+#include "audio.h"
 
 #ifndef _WIN32
 #ifdef __sun__
 #define _POSIX_PTHREAD_SEMANTICS 1
+#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
+#include <pthread.h>
 #endif
 #include <signal.h>
 #endif
@@ -78,7 +82,7 @@
     int decr;
 } SDLVoiceOut;
 
-struct SDLAudioState {
+static struct SDLAudioState {
     int exit;
     SDL_mutex *mutex;
     SDL_sem *sem;
@@ -337,7 +341,7 @@
         decr = to_mix;
         while (to_mix) {
             int chunk = audio_MIN (to_mix, hw->samples - hw->rpos);
-            st_sample_t *src = hw->mix_buf + hw->rpos;
+            struct st_sample *src = hw->mix_buf + hw->rpos;
 
             /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
             hw->clip (buf, src, chunk);
@@ -387,7 +391,7 @@
         int           samples   = bytes >> hw->info.shift;
         int           hwsamples = audio_MIN(hw->samples - hw->rpos, live);
         uint8_t*      dst       = s->data + end;
-        st_sample_t*  src       = hw->mix_buf + hw->rpos;
+        struct st_sample*  src  = hw->mix_buf + hw->rpos;
 
         if (samples == 0)
             break;
@@ -509,7 +513,7 @@
 }
 #endif
 
-static int sdl_init_out (HWVoiceOut *hw, audsettings_t *as)
+static int sdl_init_out (HWVoiceOut *hw, struct audsettings *as)
 {
     SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
     SDLAudioState *s = &glob_sdl;
@@ -518,7 +522,7 @@
     int endianess;
     int err;
     audfmt_e effective_fmt;
-    audsettings_t obt_as;
+    struct audsettings obt_as;
 
     shift <<= as->nchannels == 2;
 
diff --git a/audio/sys-queue.h b/audio/sys-queue.h
deleted file mode 100644
index 5b6e2a0..0000000
--- a/audio/sys-queue.h
+++ /dev/null
@@ -1,241 +0,0 @@
-/*
- * Copyright (c) 1991, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- *	@(#)queue.h	8.3 (Berkeley) 12/13/93
- */
-
-#ifndef	_SYS_QUEUE_H
-#define	_SYS_QUEUE_H 1
-
-/*
- * This file defines three types of data structures: lists, tail queues,
- * and circular queues.
- *
- * A list is headed by a single forward pointer (or an array of forward
- * pointers for a hash table header). The elements are doubly linked
- * so that an arbitrary element can be removed without a need to
- * traverse the list. New elements can be added to the list after
- * an existing element or at the head of the list. A list may only be
- * traversed in the forward direction.
- *
- * A tail queue is headed by a pair of pointers, one to the head of the
- * list and the other to the tail of the list. The elements are doubly
- * linked so that an arbitrary element can be removed without a need to
- * traverse the list. New elements can be added to the list after
- * an existing element, at the head of the list, or at the end of the
- * list. A tail queue may only be traversed in the forward direction.
- *
- * A circle queue is headed by a pair of pointers, one to the head of the
- * list and the other to the tail of the list. The elements are doubly
- * linked so that an arbitrary element can be removed without a need to
- * traverse the list. New elements can be added to the list before or after
- * an existing element, at the head of the list, or at the end of the list.
- * A circle queue may be traversed in either direction, but has a more
- * complex end of list detection.
- *
- * For details on the use of these macros, see the queue(3) manual page.
- */
-
-/*
- * List definitions.
- */
-#define LIST_HEAD(name, type)						\
-struct name {								\
-	struct type *lh_first;	/* first element */			\
-}
-
-#define LIST_ENTRY(type)						\
-struct {								\
-	struct type *le_next;	/* next element */			\
-	struct type **le_prev;	/* address of previous next element */	\
-}
-
-/*
- * List functions.
- */
-#define	LIST_INIT(head) {						\
-	(head)->lh_first = NULL;					\
-}
-
-#define LIST_INSERT_AFTER(listelm, elm, field) {			\
-	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
-		(listelm)->field.le_next->field.le_prev =		\
-		    &(elm)->field.le_next;				\
-	(listelm)->field.le_next = (elm);				\
-	(elm)->field.le_prev = &(listelm)->field.le_next;		\
-}
-
-#define LIST_INSERT_HEAD(head, elm, field) {				\
-	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
-		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
-	(head)->lh_first = (elm);					\
-	(elm)->field.le_prev = &(head)->lh_first;			\
-}
-
-#define LIST_REMOVE(elm, field) {					\
-	if ((elm)->field.le_next != NULL)				\
-		(elm)->field.le_next->field.le_prev = 			\
-		    (elm)->field.le_prev;				\
-	*(elm)->field.le_prev = (elm)->field.le_next;			\
-}
-
-/*
- * Tail queue definitions.
- */
-#define TAILQ_HEAD(name, type)						\
-struct name {								\
-	struct type *tqh_first;	/* first element */			\
-	struct type **tqh_last;	/* addr of last next element */		\
-}
-
-#define TAILQ_ENTRY(type)						\
-struct {								\
-	struct type *tqe_next;	/* next element */			\
-	struct type **tqe_prev;	/* address of previous next element */	\
-}
-
-/*
- * Tail queue functions.
- */
-#define	TAILQ_INIT(head) {						\
-	(head)->tqh_first = NULL;					\
-	(head)->tqh_last = &(head)->tqh_first;				\
-}
-
-#define TAILQ_INSERT_HEAD(head, elm, field) {				\
-	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
-		(elm)->field.tqe_next->field.tqe_prev =			\
-		    &(elm)->field.tqe_next;				\
-	else								\
-		(head)->tqh_last = &(elm)->field.tqe_next;		\
-	(head)->tqh_first = (elm);					\
-	(elm)->field.tqe_prev = &(head)->tqh_first;			\
-}
-
-#define TAILQ_INSERT_TAIL(head, elm, field) {				\
-	(elm)->field.tqe_next = NULL;					\
-	(elm)->field.tqe_prev = (head)->tqh_last;			\
-	*(head)->tqh_last = (elm);					\
-	(head)->tqh_last = &(elm)->field.tqe_next;			\
-}
-
-#define TAILQ_INSERT_AFTER(head, listelm, elm, field) {			\
-	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
-		(elm)->field.tqe_next->field.tqe_prev = 		\
-		    &(elm)->field.tqe_next;				\
-	else								\
-		(head)->tqh_last = &(elm)->field.tqe_next;		\
-	(listelm)->field.tqe_next = (elm);				\
-	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
-}
-
-#define TAILQ_REMOVE(head, elm, field) {				\
-	if (((elm)->field.tqe_next) != NULL)				\
-		(elm)->field.tqe_next->field.tqe_prev = 		\
-		    (elm)->field.tqe_prev;				\
-	else								\
-		(head)->tqh_last = (elm)->field.tqe_prev;		\
-	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
-}
-
-/*
- * Circular queue definitions.
- */
-#define CIRCLEQ_HEAD(name, type)					\
-struct name {								\
-	struct type *cqh_first;		/* first element */		\
-	struct type *cqh_last;		/* last element */		\
-}
-
-#define CIRCLEQ_ENTRY(type)						\
-struct {								\
-	struct type *cqe_next;		/* next element */		\
-	struct type *cqe_prev;		/* previous element */		\
-}
-
-/*
- * Circular queue functions.
- */
-#define	CIRCLEQ_INIT(head) {						\
-	(head)->cqh_first = (void *)(head);				\
-	(head)->cqh_last = (void *)(head);				\
-}
-
-#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) {		\
-	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
-	(elm)->field.cqe_prev = (listelm);				\
-	if ((listelm)->field.cqe_next == (void *)(head))		\
-		(head)->cqh_last = (elm);				\
-	else								\
-		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
-	(listelm)->field.cqe_next = (elm);				\
-}
-
-#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) {		\
-	(elm)->field.cqe_next = (listelm);				\
-	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
-	if ((listelm)->field.cqe_prev == (void *)(head))		\
-		(head)->cqh_first = (elm);				\
-	else								\
-		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
-	(listelm)->field.cqe_prev = (elm);				\
-}
-
-#define CIRCLEQ_INSERT_HEAD(head, elm, field) {				\
-	(elm)->field.cqe_next = (head)->cqh_first;			\
-	(elm)->field.cqe_prev = (void *)(head);				\
-	if ((head)->cqh_last == (void *)(head))				\
-		(head)->cqh_last = (elm);				\
-	else								\
-		(head)->cqh_first->field.cqe_prev = (elm);		\
-	(head)->cqh_first = (elm);					\
-}
-
-#define CIRCLEQ_INSERT_TAIL(head, elm, field) {				\
-	(elm)->field.cqe_next = (void *)(head);				\
-	(elm)->field.cqe_prev = (head)->cqh_last;			\
-	if ((head)->cqh_first == (void *)(head))			\
-		(head)->cqh_first = (elm);				\
-	else								\
-		(head)->cqh_last->field.cqe_next = (elm);		\
-	(head)->cqh_last = (elm);					\
-}
-
-#define	CIRCLEQ_REMOVE(head, elm, field) {				\
-	if ((elm)->field.cqe_next == (void *)(head))			\
-		(head)->cqh_last = (elm)->field.cqe_prev;		\
-	else								\
-		(elm)->field.cqe_next->field.cqe_prev =			\
-		    (elm)->field.cqe_prev;				\
-	if ((elm)->field.cqe_prev == (void *)(head))			\
-		(head)->cqh_first = (elm)->field.cqe_next;		\
-	else								\
-		(elm)->field.cqe_prev->field.cqe_next =			\
-		    (elm)->field.cqe_next;				\
-}
-#endif	/* sys/queue.h */
diff --git a/audio/wavaudio.c b/audio/wavaudio.c
index 8a500b9..6f2cc54 100644
--- a/audio/wavaudio.c
+++ b/audio/wavaudio.c
@@ -22,8 +22,11 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#define AUDIO_CAP "wav"
+#include "hw/hw.h"
 #include "qemu-timer.h"
+#include "audio.h"
+
+#define AUDIO_CAP "wav"
 #include "audio_int.h"
 #include "qemu_file.h"
 
@@ -40,7 +43,7 @@
 } WAVVoiceOut;
 
 static struct {
-    audsettings_t settings;
+    struct audsettings settings;
     const char *wav_path;
 } conf_out = {
     {
@@ -57,7 +60,7 @@
     WAVVoiceOut *wav = (WAVVoiceOut *) hw;
     int rpos, live, decr, samples;
     uint8_t *dst;
-    st_sample_t *src;
+    struct st_sample *src;
     int64_t now = qemu_get_clock (vm_clock);
     int64_t ticks = now - wav->old_ticks;
     int64_t bytes = (ticks * hw->info.bytes_per_second) / ticks_per_sec;
@@ -112,7 +115,7 @@
     }
 }
 
-static int wav_out_init (HWVoiceOut *hw, audsettings_t *as)
+static int wav_out_init (HWVoiceOut *hw, struct audsettings *as)
 {
     WAVVoiceOut *wav = (WAVVoiceOut *) hw;
     int bits16 = 0, stereo = 0;
@@ -122,7 +125,7 @@
         0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
         0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
     };
-    audsettings_t wav_as = conf_out.settings;
+    struct audsettings wav_as = conf_out.settings;
 
     (void) as;
 
@@ -245,12 +248,12 @@
 }
 
 static int
-wav_in_init (HWVoiceIn *hw, audsettings_t *as)
+wav_in_init (HWVoiceIn *hw, struct audsettings *as)
 {
     WAVVoiceIn*  wav = (WAVVoiceIn *) hw;
     const char*  path = conf_in.wav_path;
     uint8_t      hdr[44];
-    audsettings_t wav_as = *as;
+    struct audsettings wav_as = *as;
     int           nchannels, freq, format, bits;
 
     wav->f = qemu_fopen (path, "rb");
@@ -348,7 +351,7 @@
     WAVVoiceIn*   wav = (WAVVoiceIn *) hw;
     int           wpos, live, decr, samples;
     uint8_t*      src;
-    st_sample_t*  dst;
+    struct st_sample*  dst;
 
     int64_t  now   = qemu_get_clock (vm_clock);
     int64_t  ticks = now - wav->old_ticks;
@@ -417,7 +420,7 @@
     ldebug ("wav_fini");
 }
 
-struct audio_option wav_options[] = {
+static struct audio_option wav_options[] = {
     {"FREQUENCY", AUD_OPT_INT, &conf_out.settings.freq,
      "Frequency", NULL, 0},
 
diff --git a/audio/wavcapture.c b/audio/wavcapture.c
index d6f733e..1f49cd1 100644
--- a/audio/wavcapture.c
+++ b/audio/wavcapture.c
@@ -1,6 +1,6 @@
-#include "audio/audio.h"
-#include "qemu_file.h"
-#include "console.h"
+#include "hw/hw.h"
+#include "monitor.h"
+#include "audio.h"
 
 typedef struct {
     QEMUFile *f;
@@ -36,22 +36,19 @@
     uint32_t datalen = wav->bytes;
     uint32_t rifflen = datalen + 36;
 
-    if (!wav->f) {
-        return;
+    if (wav->f) {
+        le_store (rlen, rifflen, 4);
+        le_store (dlen, datalen, 4);
+
+        qemu_fseek (wav->f, 4, SEEK_SET);
+        qemu_put_buffer (wav->f, rlen, 4);
+
+        qemu_fseek (wav->f, 32, SEEK_CUR);
+        qemu_put_buffer (wav->f, dlen, 4);
+        qemu_fclose (wav->f);
     }
 
-    le_store (rlen, rifflen, 4);
-    le_store (dlen, datalen, 4);
-
-    qemu_fseek (wav->f, 4, SEEK_SET);
-    qemu_put_buffer (wav->f, rlen, 4);
-
-    qemu_fseek (wav->f, 32, SEEK_CUR);
-    qemu_put_buffer (wav->f, dlen, 4);
-    qemu_fclose (wav->f);
-    if (wav->path) {
-        qemu_free (wav->path);
-    }
+    qemu_free (wav->path);
 }
 
 static void wav_capture (void *opaque, void *buf, int size)
@@ -74,9 +71,9 @@
     WAVState *wav = opaque;
     char *path = wav->path;
 
-    term_printf ("Capturing audio(%d,%d,%d) to %s: %d bytes\n",
-                 wav->freq, wav->bits, wav->nchannels,
-                 path ? path : "<not available>", wav->bytes);
+    monitor_printf(cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
+                   wav->freq, wav->bits, wav->nchannels,
+                   path ? path : "<not available>", wav->bytes);
 }
 
 static struct capture_ops wav_capture_ops = {
@@ -87,6 +84,7 @@
 int wav_start_capture (CaptureState *s, const char *path, int freq,
                        int bits, int nchannels)
 {
+    Monitor *mon = cur_mon;
     WAVState *wav;
     uint8_t hdr[] = {
         0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
@@ -94,18 +92,19 @@
         0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
         0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
     };
-    audsettings_t as;
+    struct audsettings as;
     struct audio_capture_ops ops;
     int stereo, bits16, shift;
     CaptureVoiceOut *cap;
 
     if (bits != 8 && bits != 16) {
-        term_printf ("incorrect bit count %d, must be 8 or 16\n", bits);
+        monitor_printf(mon, "incorrect bit count %d, must be 8 or 16\n", bits);
         return -1;
     }
 
     if (nchannels != 1 && nchannels != 2) {
-        term_printf ("incorrect channel count %d, must be 1 or 2\n", bits);
+        monitor_printf(mon, "incorrect channel count %d, must be 1 or 2\n",
+                       nchannels);
         return -1;
     }
 
@@ -122,10 +121,6 @@
     ops.destroy = wav_destroy;
 
     wav = qemu_mallocz (sizeof (*wav));
-    if (!wav) {
-        AUD_log ("wav", "Could not allocate memory (%zu bytes)", sizeof (*wav));
-        return -1;
-    }
 
     shift = bits16 + stereo;
     hdr[34] = bits16 ? 0x10 : 0x08;
@@ -137,8 +132,8 @@
 
     wav->f = qemu_fopen (path, "wb");
     if (!wav->f) {
-        term_printf ("Failed to open wave file `%s'\nReason: %s\n",
-                     path, strerror (errno));
+        monitor_printf(mon, "Failed to open wave file `%s'\nReason: %s\n",
+                       path, strerror (errno));
         qemu_free (wav);
         return -1;
     }
@@ -150,9 +145,9 @@
 
     qemu_put_buffer (wav->f, hdr, sizeof (hdr));
 
-    cap = AUD_add_capture (NULL, &as, &ops, wav);
+    cap = AUD_add_capture (&as, &ops, wav);
     if (!cap) {
-        term_printf ("Failed to add audio capture\n");
+        monitor_printf(mon, "Failed to add audio capture\n");
         qemu_free (wav->path);
         qemu_fclose (wav->f);
         qemu_free (wav);
diff --git a/audio/winaudio.c b/audio/winaudio.c
index 6e8daef..ca6c487 100644
--- a/audio/winaudio.c
+++ b/audio/winaudio.c
@@ -156,7 +156,7 @@
 

 

 static int

-winaudio_out_init (HWVoiceOut *hw, audsettings_t *as)

+winaudio_out_init (HWVoiceOut *hw, struct audsettings *as)

 {

     WinAudioOut*   s = (WinAudioOut*) hw;

     MMRESULT       result;

@@ -272,7 +272,7 @@
             int           wav_bytes   = (s->write_size - s->write_pos);

             int           wav_samples = audio_MIN(wav_bytes >> hw->info.shift, live);

             int           hw_samples  = audio_MIN(hw->samples - hw->rpos, live);

-            st_sample_t*  src         = hw->mix_buf + hw->rpos;

+            struct st_sample*  src         = hw->mix_buf + hw->rpos;

             uint8_t*      dst         = (uint8_t*)wav_buffer->lpData + s->write_pos;

 

             if (wav_samples > hw_samples) {

@@ -403,7 +403,7 @@
 

 

 static int

-winaudio_in_init (HWVoiceIn *hw, audsettings_t *as)

+winaudio_in_init (HWVoiceIn *hw, struct audsettings *as)

 {

     WinAudioIn*   s = (WinAudioIn*) hw;

     MMRESULT       result;

@@ -532,7 +532,7 @@
             int           wav_bytes   = (s->read_size - s->read_pos);

             int           wav_samples = audio_MIN(wav_bytes >> hw->info.shift, live);

             int           hw_samples  = audio_MIN(hw->samples - hw->wpos, live);

-            st_sample_t*  dst         = hw->conv_buf + hw->wpos;

+            struct st_sample*  dst    = hw->conv_buf + hw->wpos;

             uint8_t*      src         = (uint8_t*)wav_buffer->lpData + s->read_pos;

 

             if (wav_samples > hw_samples) {