]> code.delx.au - pulseaudio/blobdiff - src/utils/padsp.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / utils / padsp.c
index 13f571e1e68ae9aa8759235d1f5ae5b388f45f41..b48af93cd67a5b05060ba56a67a25b3cb8d9b587 100644 (file)
@@ -2,17 +2,20 @@
 
 /***
   This file is part of PulseAudio.
+
+  Copyright 2006 Lennart Poettering
+  Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
+
   PulseAudio is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public License
   along with PulseAudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -36,6 +39,7 @@
 #include <pthread.h>
 #include <unistd.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <dlfcn.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <signal.h>
 
+#ifdef __linux__
 #include <linux/sockios.h>
+#endif
 
 #include <pulse/pulseaudio.h>
 #include <pulsecore/llist.h>
 #include <pulsecore/gccmacro.h>
 
+/* On some systems SIOCINQ isn't defined, but FIONREAD is just an alias */
+#if !defined(SIOCINQ) && defined(FIONREAD)
+# define SIOCINQ FIONREAD
+#endif
+
+/* make sure gcc doesn't redefine open and friends as macros */
+#undef open
+#undef open64
+
 typedef enum {
     FD_INFO_MIXER,
     FD_INFO_STREAM,
@@ -61,7 +76,7 @@ struct fd_info {
     pthread_mutex_t mutex;
     int ref;
     int unusable;
-    
+
     fd_info_type_t type;
     int app_fd, thread_fd;
 
@@ -73,8 +88,11 @@ struct fd_info {
     pa_context *context;
     pa_stream *play_stream;
     pa_stream *rec_stream;
+    int play_precork;
+    int rec_precork;
 
     pa_io_event *io_event;
+    pa_io_event_flags_t io_flags;
 
     void *buf;
     size_t rec_offset;
@@ -84,7 +102,9 @@ struct fd_info {
     pa_cvolume sink_volume, source_volume;
     uint32_t sink_index, source_index;
     int volume_modify_count;
-    
+
+    int optr_n_blocks;
+
     PA_LLIST_FIELDS(fd_info);
 };
 
@@ -100,8 +120,18 @@ static int (*_ioctl)(int, int, void*) = NULL;
 static int (*_close)(int) = NULL;
 static int (*_open)(const char *, int, mode_t) = NULL;
 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
+static int (*_stat)(const char *, struct stat *) = NULL;
+#ifdef _STAT_VER
+static int (*___xstat)(int, const char *, struct stat *) = NULL;
+#endif
+#ifdef HAVE_OPEN64
 static int (*_open64)(const char *, int, mode_t) = NULL;
 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
+static int (*_stat64)(const char *, struct stat64 *) = NULL;
+#ifdef _STAT_VER
+static int (*___xstat64)(int, const char *, struct stat64 *) = NULL;
+#endif
+#endif
 static int (*_fclose)(FILE *f) = NULL;
 static int (*_access)(const char *, int) = NULL;
 
@@ -152,6 +182,38 @@ do { \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
+#define LOAD_STAT_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!_stat) \
+        _stat = (int (*)(const char *, struct stat *)) dlsym_fn(RTLD_NEXT, "stat"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
+#define LOAD_STAT64_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!_stat64) \
+        _stat64 = (int (*)(const char *, struct stat64 *)) dlsym_fn(RTLD_NEXT, "stat64"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
+#define LOAD_XSTAT_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!___xstat) \
+        ___xstat = (int (*)(int, const char *, struct stat *)) dlsym_fn(RTLD_NEXT, "__xstat"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
+#define LOAD_XSTAT64_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!___xstat64) \
+        ___xstat64 = (int (*)(int, const char *, struct stat64 *)) dlsym_fn(RTLD_NEXT, "__xstat64"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
 #define LOAD_FOPEN_FUNC() \
 do { \
     pthread_mutex_lock(&func_mutex); \
@@ -178,32 +240,32 @@ do { \
 
 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
-    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
     goto label; \
 } \
-} while(0);
+} while(0)
 
 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
     !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
-    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
     goto label; \
 } \
-} while(0);
+} while(0)
 
 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
     !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
-    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s\n", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
     goto label; \
 } \
-} while(0);
+} while(0)
 
 static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
 
-#define DEBUG_LEVEL_ALWAYS             0
-#define DEBUG_LEVEL_NORMAL             1
-#define DEBUG_LEVEL_VERBOSE            2
+#define DEBUG_LEVEL_ALWAYS                0
+#define DEBUG_LEVEL_NORMAL                1
+#define DEBUG_LEVEL_VERBOSE                2
 
 static void debug(int level, const char *format, ...) {
     va_list ap;
@@ -235,7 +297,7 @@ static int padsp_disabled(void) {
      * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
      * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
      * of 7 disables padsp entirely. */
-    
+
     pthread_mutex_lock(&func_mutex);
     if (!sym_resolved) {
         sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
@@ -246,14 +308,14 @@ static int padsp_disabled(void) {
 
     if (!sym)
         return 0;
-    
+
     return *sym;
 }
 
 static int dsp_cloak_enable(void) {
     if (padsp_disabled() & 1)
         return 0;
-    
+
     if (getenv("PADSP_NO_DSP"))
         return 0;
 
@@ -289,7 +351,7 @@ static int function_enter(void) {
     /* Avoid recursive calls */
     static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
     pthread_once(&recursion_key_once, recursion_key_alloc);
-    
+
     if (pthread_getspecific(recursion_key))
         return 0;
 
@@ -307,10 +369,10 @@ static void fd_info_free(fd_info *i) {
     debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
 
     dsp_drain(i);
-    
+
     if (i->mainloop)
         pa_threaded_mainloop_stop(i->mainloop);
-    
+
     if (i->play_stream) {
         pa_stream_disconnect(i->play_stream);
         pa_stream_unref(i->play_stream);
@@ -325,7 +387,7 @@ static void fd_info_free(fd_info *i) {
         pa_context_disconnect(i->context);
         pa_context_unref(i->context);
     }
-    
+
     if (i->mainloop)
         pa_threaded_mainloop_free(i->mainloop);
 
@@ -347,7 +409,7 @@ static void fd_info_free(fd_info *i) {
 
 static fd_info *fd_info_ref(fd_info *i) {
     assert(i);
-    
+
     pthread_mutex_lock(&i->mutex);
     assert(i->ref >= 1);
     i->ref++;
@@ -363,7 +425,7 @@ static void fd_info_unref(fd_info *i) {
     pthread_mutex_lock(&i->mutex);
     assert(i->ref >= 1);
     r = --i->ref;
-       debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
+        debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
     pthread_mutex_unlock(&i->mutex);
 
     if (r <= 0)
@@ -391,7 +453,7 @@ static void context_state_cb(pa_context *c, void *userdata) {
 
 static void reset_params(fd_info *i) {
     assert(i);
-    
+
     i->sample_spec.format = PA_SAMPLE_U8;
     i->sample_spec.channels = 1;
     i->sample_spec.rate = 8000;
@@ -405,9 +467,9 @@ static const char *client_name(char *buf, size_t n) {
 
     if ((e = getenv("PADSP_CLIENT_NAME")))
         return e;
-    
+
     if (pa_get_binary_name(p, sizeof(p)))
-        snprintf(buf, n, "OSS Emulation[%s]", pa_path_get_filename(p));
+        snprintf(buf, n, "OSS Emulation[%s]", p);
     else
         snprintf(buf, n, "OSS");
 
@@ -427,7 +489,7 @@ static void atfork_prepare(void) {
     fd_info *i;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
-    
+
     function_enter();
 
     pthread_mutex_lock(&fd_infos_mutex);
@@ -439,13 +501,13 @@ static void atfork_prepare(void) {
 
     pthread_mutex_lock(&func_mutex);
 
-    
+
     debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
 }
 
 static void atfork_parent(void) {
     fd_info *i;
-    
+
     debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
 
     pthread_mutex_unlock(&func_mutex);
@@ -458,19 +520,19 @@ static void atfork_parent(void) {
     pthread_mutex_unlock(&fd_infos_mutex);
 
     function_exit();
-    
+
     debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
 }
 
 static void atfork_child(void) {
     fd_info *i;
-    
+
     debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
 
     /* We do only the bare minimum to get all fds closed */
     pthread_mutex_init(&func_mutex, NULL);
     pthread_mutex_init(&fd_infos_mutex, NULL);
-    
+
     for (i = fd_infos; i; i = i->next) {
         pthread_mutex_init(&i->mutex, NULL);
 
@@ -543,7 +605,7 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
     signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
 
     pthread_once(&install_atfork_once, install_atfork);
-    
+
     if (!(i = malloc(sizeof(fd_info)))) {
         *_errno = ENOMEM;
         goto fail;
@@ -556,7 +618,10 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
     i->context = NULL;
     i->play_stream = NULL;
     i->rec_stream = NULL;
+    i->play_precork = 0;
+    i->rec_precork = 0;
     i->io_event = NULL;
+    i->io_flags = 0;
     pthread_mutex_init(&i->mutex, NULL);
     i->ref = 1;
     i->buf = NULL;
@@ -567,6 +632,7 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
     i->volume_modify_count = 0;
     i->sink_index = (uint32_t) -1;
     i->source_index = (uint32_t) -1;
+    i->optr_n_blocks = 0;
     PA_LLIST_INIT(fd_info, i);
 
     reset_params(i);
@@ -623,12 +689,12 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
 unlock_and_fail:
 
     pa_threaded_mainloop_unlock(i->mainloop);
-    
+
 fail:
 
     if (i)
         fd_info_unref(i);
-    
+
     return NULL;
 }
 
@@ -656,7 +722,7 @@ static fd_info* fd_info_find(int fd) {
     fd_info *i;
 
     pthread_mutex_lock(&fd_infos_mutex);
-    
+
     for (i = fd_infos; i; i = i->next)
         if (i->app_fd == fd && !i->unusable) {
             fd_info_ref(i);
@@ -664,7 +730,7 @@ static fd_info* fd_info_find(int fd) {
         }
 
     pthread_mutex_unlock(&fd_infos_mutex);
-    
+
     return i;
 }
 
@@ -673,6 +739,13 @@ static void fix_metrics(fd_info *i) {
     char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
 
     fs = pa_frame_size(&i->sample_spec);
+
+    /* Don't fix things more than necessary */
+    if ((i->fragment_size % fs) == 0 &&
+        i->n_fragments >= 2 &&
+        i->fragment_size > 0)
+        return;
+
     i->fragment_size = (i->fragment_size/fs)*fs;
 
     /* Number of fragments set? */
@@ -702,13 +775,10 @@ static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
 
     if (i->io_event) {
         pa_mainloop_api *api;
-        pa_io_event_flags_t flags;
         size_t n;
 
         api = pa_threaded_mainloop_get_api(i->mainloop);
 
-        flags = 0;
-
         if (s == i->play_stream) {
             n = pa_stream_writable_size(i->play_stream);
             if (n == (size_t)-1) {
@@ -717,7 +787,9 @@ static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
             }
 
             if (n >= i->fragment_size)
-                flags |= PA_IO_EVENT_INPUT;
+                i->io_flags |= PA_IO_EVENT_INPUT;
+            else
+                i->io_flags &= ~PA_IO_EVENT_INPUT;
         }
 
         if (s == i->rec_stream) {
@@ -728,10 +800,12 @@ static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
             }
 
             if (n >= i->fragment_size)
-                flags |= PA_IO_EVENT_OUTPUT;
+                i->io_flags |= PA_IO_EVENT_OUTPUT;
+            else
+                i->io_flags &= ~PA_IO_EVENT_OUTPUT;
         }
 
-        api->io_enable(i->io_event, flags);
+        api->io_enable(i->io_event, i->io_flags);
     }
 }
 
@@ -750,6 +824,7 @@ static void fd_info_shutdown(fd_info *i) {
         api = pa_threaded_mainloop_get_api(i->mainloop);
         api->io_free(i->io_event);
         i->io_event = NULL;
+        i->io_flags = 0;
     }
 
     if (i->thread_fd >= 0) {
@@ -760,13 +835,10 @@ static void fd_info_shutdown(fd_info *i) {
 
 static int fd_info_copy_data(fd_info *i, int force) {
     size_t n;
-    pa_io_event_flags_t flags;
 
     if (!i->play_stream && !i->rec_stream)
         return -1;
 
-    flags = 0;
-
     if ((i->play_stream) && (pa_stream_get_state(i->play_stream) == PA_STREAM_READY)) {
         n = pa_stream_writable_size(i->play_stream);
 
@@ -807,7 +879,9 @@ static int fd_info_copy_data(fd_info *i, int force) {
         }
 
         if (n >= i->fragment_size)
-            flags |= PA_IO_EVENT_INPUT;
+            i->io_flags |= PA_IO_EVENT_INPUT;
+        else
+            i->io_flags &= ~PA_IO_EVENT_INPUT;
     }
 
     if ((i->rec_stream) && (pa_stream_get_state(i->rec_stream) == PA_STREAM_READY)) {
@@ -860,14 +934,16 @@ static int fd_info_copy_data(fd_info *i, int force) {
         }
 
         if (n >= i->fragment_size)
-            flags |= PA_IO_EVENT_OUTPUT;
+            i->io_flags |= PA_IO_EVENT_OUTPUT;
+        else
+            i->io_flags &= ~PA_IO_EVENT_OUTPUT;
     }
 
     if (i->io_event) {
         pa_mainloop_api *api;
 
         api = pa_threaded_mainloop_get_api(i->mainloop);
-        api->io_enable(i->io_event, flags);
+        api->io_enable(i->io_event, i->io_flags);
     }
 
     return 0;
@@ -882,9 +958,21 @@ static void stream_state_cb(pa_stream *s, void * userdata) {
         case PA_STREAM_READY:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
             break;
-            
+
         case PA_STREAM_FAILED:
-            debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+            if (s == i->play_stream) {
+                debug(DEBUG_LEVEL_NORMAL,
+                    __FILE__": pa_stream_connect_playback() failed: %s\n",
+                    pa_strerror(pa_context_errno(i->context)));
+                pa_stream_unref(i->play_stream);
+                i->play_stream = NULL;
+            } else if (s == i->rec_stream) {
+                debug(DEBUG_LEVEL_NORMAL,
+                    __FILE__": pa_stream_connect_record() failed: %s\n",
+                    pa_strerror(pa_context_errno(i->context)));
+                pa_stream_unref(i->rec_stream);
+                i->rec_stream = NULL;
+            }
             fd_info_shutdown(i);
             break;
 
@@ -897,8 +985,8 @@ static void stream_state_cb(pa_stream *s, void * userdata) {
 
 static int create_playback_stream(fd_info *i) {
     pa_buffer_attr attr;
-    int n;
-    
+    int n, flags;
+
     assert(i);
 
     fix_metrics(i);
@@ -917,8 +1005,13 @@ static int create_playback_stream(fd_info *i) {
     attr.tlength = i->fragment_size * i->n_fragments;
     attr.prebuf = i->fragment_size;
     attr.minreq = i->fragment_size;
-    
-    if (pa_stream_connect_playback(i->play_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
+
+    flags = PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE;
+    if (i->play_precork) {
+        flags |= PA_STREAM_START_CORKED;
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": creating stream corked\n");
+    }
+    if (pa_stream_connect_playback(i->play_stream, NULL, &attr, flags, NULL, NULL) < 0) {
         debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
@@ -927,7 +1020,7 @@ static int create_playback_stream(fd_info *i) {
     setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
     n = i->fragment_size;
     setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
-    
+
     return 0;
 
 fail:
@@ -936,8 +1029,8 @@ fail:
 
 static int create_record_stream(fd_info *i) {
     pa_buffer_attr attr;
-    int n;
-    
+    int n, flags;
+
     assert(i);
 
     fix_metrics(i);
@@ -954,9 +1047,14 @@ static int create_record_stream(fd_info *i) {
     memset(&attr, 0, sizeof(attr));
     attr.maxlength = i->fragment_size * (i->n_fragments+1);
     attr.fragsize = i->fragment_size;
-    
-    if (pa_stream_connect_record(i->rec_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) < 0) {
-        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+
+    flags = PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE;
+    if (i->rec_precork) {
+        flags |= PA_STREAM_START_CORKED;
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": creating stream corked\n");
+    }
+    if (pa_stream_connect_record(i->rec_stream, NULL, &attr, flags) < 0) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
@@ -964,7 +1062,7 @@ static int create_record_stream(fd_info *i) {
     setsockopt(i->app_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
     n = i->fragment_size;
     setsockopt(i->thread_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
-    
+
     return 0;
 
 fail:
@@ -978,12 +1076,21 @@ static void free_streams(fd_info *i) {
         pa_stream_disconnect(i->play_stream);
         pa_stream_unref(i->play_stream);
         i->play_stream = NULL;
+        i->io_flags |= PA_IO_EVENT_INPUT;
     }
 
     if (i->rec_stream) {
         pa_stream_disconnect(i->rec_stream);
         pa_stream_unref(i->rec_stream);
         i->rec_stream = NULL;
+        i->io_flags |= PA_IO_EVENT_OUTPUT;
+    }
+
+    if (i->io_event) {
+        pa_mainloop_api *api;
+
+        api = pa_threaded_mainloop_get_api(i->mainloop);
+        api->io_enable(i->io_event, i->io_flags);
     }
 }
 
@@ -991,7 +1098,7 @@ static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_even
     fd_info *i = userdata;
 
     pa_threaded_mainloop_signal(i->mainloop, 0);
-    
+
     if (flags & PA_IO_EVENT_INPUT) {
 
         if (!i->play_stream) {
@@ -1001,7 +1108,7 @@ static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_even
             if (fd_info_copy_data(i, 0) < 0)
                 goto fail;
         }
-        
+
     } else if (flags & PA_IO_EVENT_OUTPUT) {
 
         if (!i->rec_stream) {
@@ -1016,7 +1123,7 @@ static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_even
         goto fail;
 
     return;
-    
+
 fail:
     /* We can't do anything better than removing the event source */
     fd_info_shutdown(i);
@@ -1027,7 +1134,6 @@ static int dsp_open(int flags, int *_errno) {
     pa_mainloop_api *api;
     int ret;
     int f;
-    pa_io_event_flags_t ioflags;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
 
@@ -1049,25 +1155,25 @@ static int dsp_open(int flags, int *_errno) {
 
     switch (flags & O_ACCMODE) {
     case O_RDONLY:
-        ioflags = PA_IO_EVENT_OUTPUT;
+        i->io_flags = PA_IO_EVENT_OUTPUT;
         shutdown(i->thread_fd, SHUT_RD);
         shutdown(i->app_fd, SHUT_WR);
         break;
     case O_WRONLY:
-        ioflags = PA_IO_EVENT_INPUT;
+        i->io_flags = PA_IO_EVENT_INPUT;
         shutdown(i->thread_fd, SHUT_WR);
         shutdown(i->app_fd, SHUT_RD);
         break;
     case O_RDWR:
-        ioflags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
+        i->io_flags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
         break;
     default:
         return -1;
     }
 
-    if (!(i->io_event = api->io_new(api, i->thread_fd, ioflags, io_event_cb, i)))
+    if (!(i->io_event = api->io_new(api, i->thread_fd, i->io_flags, io_event_cb, i)))
         goto fail;
-    
+
     pa_threaded_mainloop_unlock(i->mainloop);
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
@@ -1075,7 +1181,7 @@ static int dsp_open(int flags, int *_errno) {
     fd_info_add_to_list(i);
     ret = i->app_fd;
     fd_info_unref(i);
-    
+
     return ret;
 
 fail:
@@ -1083,7 +1189,7 @@ fail:
 
     if (i)
         fd_info_unref(i);
-    
+
     *_errno = EIO;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
@@ -1105,7 +1211,7 @@ static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, v
 
     if (!pa_cvolume_equal(&i->sink_volume, &si->volume))
         i->volume_modify_count++;
-    
+
     i->sink_volume = si->volume;
     i->sink_index = si->index;
 
@@ -1127,7 +1233,7 @@ static void source_info_cb(pa_context *context, const pa_source_info *si, int eo
 
     if (!pa_cvolume_equal(&i->source_volume, &si->volume))
         i->volume_modify_count++;
-    
+
     i->source_volume = si->volume;
     i->source_index = si->index;
 
@@ -1160,13 +1266,13 @@ static int mixer_open(int flags, int *_errno) {
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
 
-    if (!(i = fd_info_new(FD_INFO_MIXER, _errno))) 
+    if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
         return -1;
-    
+
     pa_threaded_mainloop_lock(i->mainloop);
 
     pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
-    
+
     if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, context_success_cb, i))) {
         debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
         *_errno = EIO;
@@ -1241,7 +1347,7 @@ static int mixer_open(int flags, int *_errno) {
     fd_info_add_to_list(i);
     ret = i->app_fd;
     fd_info_unref(i);
-    
+
     return ret;
 
 fail:
@@ -1252,7 +1358,7 @@ fail:
 
     if (i)
         fd_info_unref(i);
-    
+
     *_errno = EIO;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
@@ -1262,18 +1368,18 @@ fail:
 
 static int sndstat_open(int flags, int *_errno) {
     static const char sndstat[] =
-        "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
+        "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
         "Kernel: POSIX\n"
         "Config options: 0\n"
         "\n"
         "Installed drivers:\n"
-        "Type 255: Polypaudio Virtual OSS\n"
+        "Type 255: PulseAudio Virtual OSS\n"
         "\n"
         "Card config:\n"
-        "Polypaudio Virtual OSS\n"
+        "PulseAudio Virtual OSS\n"
         "\n"
         "Audio devices:\n"
-        "0: Polypaudio Virtual OSS\n"
+        "0: PulseAudio Virtual OSS\n"
         "\n"
         "Synth devices: NOT ENABLED IN CONFIG\n"
         "\n"
@@ -1282,7 +1388,7 @@ static int sndstat_open(int flags, int *_errno) {
         "Timers:\n"
         "\n"
         "Mixers:\n"
-        "0: Polypaudio Virtual OSS\n";
+        "0: PulseAudio Virtual OSS\n";
 
     char fn[] = "/tmp/padsp-sndstat-XXXXXX";
     mode_t u;
@@ -1290,8 +1396,12 @@ static int sndstat_open(int flags, int *_errno) {
     int e;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
-    
-    if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
+
+    if (flags != O_RDONLY
+#ifdef O_LARGEFILE
+        && flags != (O_RDONLY|O_LARGEFILE)
+#endif
+       ) {
         *_errno = EACCES;
         debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
         goto fail;
@@ -1330,46 +1440,55 @@ fail:
     return -1;
 }
 
-int open(const char *filename, int flags, ...) {
-    va_list args;
-    mode_t mode = 0;
+static int real_open(const char *filename, int flags, mode_t mode) {
     int r, _errno = 0;
 
     debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename);
 
-    va_start(args, flags);
-    if (flags & O_CREAT)
-        mode = va_arg(args, mode_t);
-    va_end(args);
-
     if (!function_enter()) {
         LOAD_OPEN_FUNC();
         return _open(filename, flags, mode);
     }
 
-    if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
+    if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0))
         r = dsp_open(flags, &_errno);
-    } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
+    else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0)
         r = mixer_open(flags, &_errno);
-    } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
+    else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0)
         r = sndstat_open(flags, &_errno);
-    else {
+    else {
         function_exit();
         LOAD_OPEN_FUNC();
         return _open(filename, flags, mode);
     }
 
     function_exit();
-    
+
     if (_errno)
         errno = _errno;
-    
+
     return r;
 }
 
+int open(const char *filename, int flags, ...) {
+    va_list args;
+    mode_t mode = 0;
+
+    if (flags & O_CREAT) {
+        va_start(args, flags);
+        if (sizeof(mode_t) < sizeof(int))
+            mode = va_arg(args, int);
+        else
+            mode = va_arg(args, mode_t);
+        va_end(args);
+    }
+
+    return real_open(filename, flags, mode);
+}
+
 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
     int ret = -1;
-    
+
     switch (request) {
         case SOUND_MIXER_READ_DEVMASK :
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
@@ -1382,7 +1501,7 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
 
             *(int*) argp = SOUND_MASK_IGAIN;
             break;
-            
+
         case SOUND_MIXER_READ_STEREODEVS:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
 
@@ -1393,7 +1512,7 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
             if (i->source_volume.channels > 1)
                 *(int*) argp |= SOUND_MASK_IGAIN;
             pa_threaded_mainloop_unlock(i->mainloop);
-            
+
             break;
 
         case SOUND_MIXER_READ_RECSRC:
@@ -1411,7 +1530,7 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
 
             *(int*) argp = 0;
             break;
-    
+
         case SOUND_MIXER_READ_PCM:
         case SOUND_MIXER_READ_IGAIN: {
             pa_cvolume *v;
@@ -1441,14 +1560,14 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
         case SOUND_MIXER_WRITE_IGAIN: {
             pa_cvolume v, *pv;
 
-            if (request == SOUND_MIXER_READ_PCM)
+            if (request == SOUND_MIXER_WRITE_PCM)
                 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
             else
                 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_IGAIN\n");
 
             pa_threaded_mainloop_lock(i->mainloop);
 
-            if (request == SOUND_MIXER_READ_PCM) {
+            if (request == SOUND_MIXER_WRITE_PCM) {
                 v = i->sink_volume;
                 pv = &i->sink_volume;
             } else {
@@ -1462,10 +1581,10 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
             if (!pa_cvolume_equal(pv, &v)) {
                 pa_operation *o;
 
-                if (request == SOUND_MIXER_READ_PCM)
-                    o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, NULL, NULL);
+                if (request == SOUND_MIXER_WRITE_PCM)
+                    o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, context_success_cb, i);
                 else
-                    o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, NULL, NULL);
+                    o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, context_success_cb, i);
 
                 if (!o)
                     debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
@@ -1474,23 +1593,23 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
                     i->operation_success = 0;
                     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
                         CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
-                        
+
                         pa_threaded_mainloop_wait(i->mainloop);
                     }
                 exit_loop:
-                    
+
                     if (!i->operation_success)
                         debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
 
                     pa_operation_unref(o);
                 }
-                
+
                 /* We don't wait for completion here */
                 i->volume_modify_count++;
             }
-            
+
             pa_threaded_mainloop_unlock(i->mainloop);
-            
+
             break;
         }
 
@@ -1500,14 +1619,14 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
 
             memset(mi, 0, sizeof(mixer_info));
-            strncpy(mi->id, "POLYPAUDIO", sizeof(mi->id));
-            strncpy(mi->name, "Polypaudio Virtual OSS", sizeof(mi->name));
+            strncpy(mi->id, "PULSEAUDIO", sizeof(mi->id));
+            strncpy(mi->name, "PulseAudio Virtual OSS", sizeof(mi->name));
             pa_threaded_mainloop_lock(i->mainloop);
             mi->modify_counter = i->volume_modify_count;
             pa_threaded_mainloop_unlock(i->mainloop);
             break;
         }
-            
+
         default:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
 
@@ -1516,44 +1635,44 @@ static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno
     }
 
     ret = 0;
-    
+
 fail:
-    
+
     return ret;
 }
 
 static int map_format(int *fmt, pa_sample_spec *ss) {
-    
+
     switch (*fmt) {
         case AFMT_MU_LAW:
             ss->format = PA_SAMPLE_ULAW;
             break;
-            
+
         case AFMT_A_LAW:
             ss->format = PA_SAMPLE_ALAW;
             break;
-            
+
         case AFMT_S8:
             *fmt = AFMT_U8;
             /* fall through */
         case AFMT_U8:
             ss->format = PA_SAMPLE_U8;
             break;
-            
+
         case AFMT_U16_BE:
             *fmt = AFMT_S16_BE;
             /* fall through */
         case AFMT_S16_BE:
             ss->format = PA_SAMPLE_S16BE;
             break;
-            
+
         case AFMT_U16_LE:
             *fmt = AFMT_S16_LE;
             /* fall through */
         case AFMT_S16_LE:
             ss->format = PA_SAMPLE_S16LE;
             break;
-            
+
         default:
             ss->format = PA_SAMPLE_S16NE;
             *fmt = AFMT_S16_NE;
@@ -1576,6 +1695,7 @@ static int map_format_back(pa_sample_format_t format) {
 }
 
 static int dsp_flush_fd(int fd) {
+#ifdef SIOCINQ
     int l;
 
     if (ioctl(fd, SIOCINQ, &l) < 0) {
@@ -1594,6 +1714,10 @@ static int dsp_flush_fd(int fd) {
     }
 
     return 0;
+#else
+# warning "Your platform does not support SIOCINQ, something might not work as intended."
+    return 0;
+#endif
 }
 
 static int dsp_flush_socket(fd_info *i) {
@@ -1618,15 +1742,16 @@ static int dsp_flush_socket(fd_info *i) {
 }
 
 static int dsp_empty_socket(fd_info *i) {
+#ifdef SIOCINQ
     int ret = -1;
-    
+
     /* Empty the socket */
     for (;;) {
         int l;
-        
+
         if (i->thread_fd < 0)
             break;
-        
+
         if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
             break;
@@ -1636,11 +1761,15 @@ static int dsp_empty_socket(fd_info *i) {
             ret = 0;
             break;
         }
-        
+
         pa_threaded_mainloop_wait(i->mainloop);
     }
 
     return ret;
+#else
+# warning "Your platform does not support SIOCINQ, something might not work as intended."
+    return 0;
+#endif
 }
 
 static int dsp_drain(fd_info *i) {
@@ -1649,19 +1778,19 @@ static int dsp_drain(fd_info *i) {
 
     if (!i->mainloop)
         return 0;
-    
+
     debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
 
     pa_threaded_mainloop_lock(i->mainloop);
 
     if (dsp_empty_socket(i) < 0)
         goto fail;
-    
+
     if (!i->play_stream)
         goto fail;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
-        
+
     if (!(o = pa_stream_drain(i->play_stream, stream_success_cb, i))) {
         debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
@@ -1670,7 +1799,7 @@ static int dsp_drain(fd_info *i) {
     i->operation_success = 0;
     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
         PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
-            
+
         pa_threaded_mainloop_wait(i->mainloop);
     }
 
@@ -1680,9 +1809,9 @@ static int dsp_drain(fd_info *i) {
     }
 
     r = 0;
-    
+
 fail:
-    
+
     if (o)
         pa_operation_unref(o);
 
@@ -1704,7 +1833,7 @@ static int dsp_trigger(fd_info *i) {
         goto fail;
 
     debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
-        
+
     if (!(o = pa_stream_trigger(i->play_stream, stream_success_cb, i))) {
         debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
@@ -1713,7 +1842,7 @@ static int dsp_trigger(fd_info *i) {
     i->operation_success = 0;
     while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
         PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
-            
+
         pa_threaded_mainloop_wait(i->mainloop);
     }
 
@@ -1723,9 +1852,47 @@ static int dsp_trigger(fd_info *i) {
     }
 
     r = 0;
-    
+
+fail:
+
+    if (o)
+        pa_operation_unref(o);
+
+    pa_threaded_mainloop_unlock(i->mainloop);
+
+    return 0;
+}
+
+static int dsp_cork(fd_info *i, pa_stream *s, int b) {
+    pa_operation *o = NULL;
+    int r = -1;
+
+    pa_threaded_mainloop_lock(i->mainloop);
+
+    if (!(o = pa_stream_cork(s, b, stream_success_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_cork(): %s\n", pa_strerror(pa_context_errno(i->context)));
+        goto fail;
+    }
+
+    i->operation_success = 0;
+    while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
+        if (s == i->play_stream)
+            PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
+        else if (s == i->rec_stream)
+            RECORD_STREAM_CHECK_DEAD_GOTO(i, fail);
+
+        pa_threaded_mainloop_wait(i->mainloop);
+    }
+
+    if (!i->operation_success) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_cork(): %s\n", pa_strerror(pa_context_errno(i->context)));
+        goto fail;
+    }
+
+    r = 0;
+
 fail:
-    
+
     if (o)
         pa_operation_unref(o);
 
@@ -1736,11 +1903,21 @@ fail:
 
 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
     int ret = -1;
-    
+
+    if (i->thread_fd == -1) {
+        /*
+         * We've encountered some fatal error and are just waiting
+         * for a close.
+         */
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": got ioctl 0x%08lx in fatal error state\n", request);
+        *_errno = EIO;
+        return -1;
+    }
+
     switch (request) {
         case SNDCTL_DSP_SETFMT: {
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
-            
+
             pa_threaded_mainloop_lock(i->mainloop);
 
             if (*(int*) argp == AFMT_QUERY)
@@ -1753,12 +1930,12 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
             pa_threaded_mainloop_unlock(i->mainloop);
             break;
         }
-            
+
         case SNDCTL_DSP_SPEED: {
             pa_sample_spec ss;
             int valid;
             char t[256];
-            
+
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
 
             pa_threaded_mainloop_lock(i->mainloop);
@@ -1770,7 +1947,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
                 i->sample_spec = ss;
                 free_streams(i);
             }
-            
+
             debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
 
             pa_threaded_mainloop_unlock(i->mainloop);
@@ -1782,24 +1959,24 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
             break;
         }
-            
+
         case SNDCTL_DSP_STEREO:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
-            
+
             pa_threaded_mainloop_lock(i->mainloop);
-            
+
             i->sample_spec.channels = *(int*) argp ? 2 : 1;
             free_streams(i);
-            
+
             pa_threaded_mainloop_unlock(i->mainloop);
             return 0;
 
         case SNDCTL_DSP_CHANNELS: {
             pa_sample_spec ss;
             int valid;
-            
+
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
-            
+
             pa_threaded_mainloop_lock(i->mainloop);
 
             ss = i->sample_spec;
@@ -1809,7 +1986,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
                 i->sample_spec = ss;
                 free_streams(i);
             }
-            
+
             pa_threaded_mainloop_unlock(i->mainloop);
 
             if (!valid) {
@@ -1827,17 +2004,17 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
             fix_metrics(i);
             *(int*) argp = i->fragment_size;
-            
+
             pa_threaded_mainloop_unlock(i->mainloop);
-            
+
             break;
 
         case SNDCTL_DSP_SETFRAGMENT:
-            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
-            
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%08x\n", *(int*) argp);
+
             pa_threaded_mainloop_lock(i->mainloop);
-            
-            i->fragment_size = 1 << (*(int*) argp);
+
+            i->fragment_size = 1 << ((*(int*) argp) & 31);
             i->n_fragments = (*(int*) argp) >> 16;
 
             /* 0x7FFF means that we can set whatever we like */
@@ -1845,26 +2022,30 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
                 i->n_fragments = 12;
 
             free_streams(i);
-            
+
             pa_threaded_mainloop_unlock(i->mainloop);
-            
+
             break;
-            
+
         case SNDCTL_DSP_GETCAPS:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
-            
-            *(int*)  argp = DSP_CAP_DUPLEX | DSP_CAP_MULTI;
+
+            *(int*)  argp = DSP_CAP_DUPLEX | DSP_CAP_TRIGGER
+#ifdef DSP_CAP_MULTI
+              | DSP_CAP_MULTI
+#endif
+              ;
             break;
 
         case SNDCTL_DSP_GETODELAY: {
             int l;
-            
+
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
-            
+
             pa_threaded_mainloop_lock(i->mainloop);
 
             *(int*) argp = 0;
-            
+
             for (;;) {
                 pa_usec_t usec;
 
@@ -1882,13 +2063,17 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
                 pa_threaded_mainloop_wait(i->mainloop);
             }
-            
+
         exit_loop:
-            
+
+#ifdef SIOCINQ
             if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
                 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
             else
                 *(int*) argp += l;
+#else
+# warning "Your platform does not support SIOCINQ, something might not work as intended."
+#endif
 
             pa_threaded_mainloop_unlock(i->mainloop);
 
@@ -1896,38 +2081,76 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
             break;
         }
-            
+
         case SNDCTL_DSP_RESET: {
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
-            
+
             pa_threaded_mainloop_lock(i->mainloop);
 
             free_streams(i);
             dsp_flush_socket(i);
-            reset_params(i);
-            
+
+            i->optr_n_blocks = 0;
+
             pa_threaded_mainloop_unlock(i->mainloop);
             break;
         }
-            
+
         case SNDCTL_DSP_GETFMTS: {
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
-            
+
             *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
             break;
         }
 
         case SNDCTL_DSP_POST:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
-            
-            if (dsp_trigger(i) < 0) 
+
+            if (dsp_trigger(i) < 0)
                 *_errno = EIO;
             break;
 
-        case SNDCTL_DSP_SYNC: 
+        case SNDCTL_DSP_GETTRIGGER:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETTRIGGER\n");
+
+            *(int*) argp = 0;
+            if (!i->play_precork)
+                *(int*) argp |= PCM_ENABLE_OUTPUT;
+            if (!i->rec_precork)
+                *(int*) argp |= PCM_ENABLE_INPUT;
+
+            break;
+
+        case SNDCTL_DSP_SETTRIGGER:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETTRIGGER: 0x%08x\n", *(int*) argp);
+
+            if (!i->io_event) {
+                *_errno = EIO;
+                break;
+            }
+
+            i->play_precork = !((*(int*) argp) & PCM_ENABLE_OUTPUT);
+
+            if (i->play_stream) {
+                if (dsp_cork(i, i->play_stream, !((*(int*) argp) & PCM_ENABLE_OUTPUT)) < 0)
+                    *_errno = EIO;
+                if (dsp_trigger(i) < 0)
+                    *_errno = EIO;
+            }
+
+            i->rec_precork = !((*(int*) argp) & PCM_ENABLE_INPUT);
+
+            if (i->rec_stream) {
+                if (dsp_cork(i, i->rec_stream, !((*(int*) argp) & PCM_ENABLE_INPUT)) < 0)
+                    *_errno = EIO;
+            }
+
+            break;
+
+        case SNDCTL_DSP_SYNC:
             debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
-            
-            if (dsp_drain(i) < 0) 
+
+            if (dsp_drain(i) < 0)
                 *_errno = EIO;
 
             break;
@@ -1935,7 +2158,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
         case SNDCTL_DSP_GETOSPACE:
         case SNDCTL_DSP_GETISPACE: {
             audio_buf_info *bi = (audio_buf_info*) argp;
-            int l;
+            int l = 0;
             size_t k = 0;
 
             if (request == SNDCTL_DSP_GETOSPACE)
@@ -1954,10 +2177,14 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
                 } else
                     k = i->fragment_size * i->n_fragments;
 
+#ifdef SIOCINQ
                 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
                     debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
                     l = 0;
                 }
+#else
+# warning "Your platform does not dsp_flush_fd, something might not work as intended."
+#endif
 
                 bi->bytes = k > (size_t) l ? k - l : 0;
             } else {
@@ -1967,11 +2194,14 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
                 } else
                     k = 0;
 
+#ifdef SIOCINQ
                 if (ioctl(i->app_fd, SIOCINQ, &l) < 0) {
                     debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
                     l = 0;
                 }
-
+#else
+# warning "Your platform does not dsp_flush_fd, something might not work as intended."
+#endif
                 bi->bytes = k + l;
             }
 
@@ -1985,18 +2215,95 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
             break;
         }
-            
+
+        case SOUND_PCM_READ_RATE:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_PCM_READ_RATE\n");
+
+            pa_threaded_mainloop_lock(i->mainloop);
+            *(int*) argp = i->sample_spec.rate;
+            pa_threaded_mainloop_unlock(i->mainloop);
+            break;
+
+        case SOUND_PCM_READ_CHANNELS:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_PCM_READ_CHANNELS\n");
+
+            pa_threaded_mainloop_lock(i->mainloop);
+            *(int*) argp = i->sample_spec.channels;
+            pa_threaded_mainloop_unlock(i->mainloop);
+            break;
+
+        case SOUND_PCM_READ_BITS:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_PCM_READ_BITS\n");
+
+            pa_threaded_mainloop_lock(i->mainloop);
+            *(int*) argp = pa_sample_size(&i->sample_spec)*8;
+            pa_threaded_mainloop_unlock(i->mainloop);
+            break;
+
+        case SNDCTL_DSP_GETOPTR: {
+            count_info *info;
+
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOPTR\n");
+
+            info = (count_info*) argp;
+            memset(info, 0, sizeof(*info));
+
+            pa_threaded_mainloop_lock(i->mainloop);
+
+            for (;;) {
+                pa_usec_t usec;
+
+                PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
+
+                if (pa_stream_get_time(i->play_stream, &usec) >= 0) {
+                    size_t k = pa_usec_to_bytes(usec, &i->sample_spec);
+                    int m;
+
+                    info->bytes = (int) k;
+                    m = k / i->fragment_size;
+                    info->blocks = m - i->optr_n_blocks;
+                    i->optr_n_blocks = m;
+
+                    break;
+                }
+
+                if (pa_context_errno(i->context) != PA_ERR_NODATA) {
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                    break;
+                }
+
+                pa_threaded_mainloop_wait(i->mainloop);
+            }
+
+            pa_threaded_mainloop_unlock(i->mainloop);
+
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": GETOPTR bytes=%i, blocks=%i, ptr=%i\n", info->bytes, info->blocks, info->ptr);
+
+            break;
+        }
+
+        case SNDCTL_DSP_GETIPTR:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": invalid ioctl SNDCTL_DSP_GETIPTR\n");
+            goto inval;
+
+        case SNDCTL_DSP_SETDUPLEX:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETDUPLEX\n");
+            /* this is a no-op */
+            break;
+
         default:
-            debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
+            /* Mixer ioctls are valid on /dev/dsp aswell */
+            return mixer_ioctl(i, request, argp, _errno);
 
+inval:
             *_errno = EINVAL;
             goto fail;
     }
 
     ret = 0;
-    
+
 fail:
-    
+
     return ret;
 }
 
@@ -2027,14 +2334,14 @@ int ioctl(int fd, unsigned long request, ...) {
         r = mixer_ioctl(i, request, argp, &_errno);
     else
         r = dsp_ioctl(i, request, argp, &_errno);
-    
+
     fd_info_unref(i);
 
     if (_errno)
         errno = _errno;
 
     function_exit();
-    
+
     return r;
 }
 
@@ -2056,13 +2363,20 @@ int close(int fd) {
 
     fd_info_remove_from_list(i);
     fd_info_unref(i);
-    
+
     function_exit();
 
     return 0;
 }
 
 int access(const char *pathname, int mode) {
+
+    if (!pathname) {
+        /* Firefox needs this. See #27 */
+        errno = EFAULT;
+        return -1;
+    }
+
     debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname);
 
     if (strcmp(pathname, "/dev/dsp") != 0 &&
@@ -2084,16 +2398,122 @@ int access(const char *pathname, int mode) {
     return 0;
 }
 
+int stat(const char *pathname, struct stat *buf) {
+#ifdef HAVE_OPEN64
+    struct stat64 parent;
+#else
+    struct stat parent;
+#endif
+    int ret;
+
+    if (!pathname || !buf) {
+        errno = EFAULT;
+        return -1;
+    }
+
+    if (strcmp(pathname, "/dev/dsp") != 0 &&
+        strcmp(pathname, "/dev/adsp") != 0 &&
+        strcmp(pathname, "/dev/sndstat") != 0 &&
+        strcmp(pathname, "/dev/mixer") != 0) {
+        debug(DEBUG_LEVEL_VERBOSE, __FILE__": stat(%s)\n", pathname);
+        LOAD_STAT_FUNC();
+        return _stat(pathname, buf);
+    }
+
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": stat(%s)\n", pathname);
+
+#ifdef _STAT_VER
+#ifdef HAVE_OPEN64
+    ret = __xstat64(_STAT_VER, "/dev", &parent);
+#else
+    ret = __xstat(_STAT_VER, "/dev", &parent);
+#endif
+#else
+#ifdef HAVE_OPEN64
+    ret = stat64("/dev", &parent);
+#else
+    ret = stat("/dev", &parent);
+#endif
+#endif
+
+    if (ret) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": unable to stat \"/dev\"\n");
+        return -1;
+    }
+
+    buf->st_dev = parent.st_dev;
+    buf->st_ino = 0xDEADBEEF;   /* FIXME: Can we do this in a safe way? */
+    buf->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
+    buf->st_nlink = 1;
+    buf->st_uid = getuid();
+    buf->st_gid = getgid();
+    buf->st_rdev = 0x0E03;      /* FIXME: Linux specific */
+    buf->st_size = 0;
+    buf->st_atime = 1181557705;
+    buf->st_mtime = 1181557705;
+    buf->st_ctime = 1181557705;
+    buf->st_blksize = 1;
+    buf->st_blocks = 0;
+
+    return 0;
+}
+
+#ifdef HAVE_OPEN64
+
+int stat64(const char *pathname, struct stat64 *buf) {
+    struct stat oldbuf;
+    int ret;
+
+    if (!pathname || !buf) {
+        errno = EFAULT;
+        return -1;
+    }
+
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": stat64(%s)\n", pathname);
+
+    if (strcmp(pathname, "/dev/dsp") != 0 &&
+        strcmp(pathname, "/dev/adsp") != 0 &&
+        strcmp(pathname, "/dev/sndstat") != 0 &&
+        strcmp(pathname, "/dev/mixer") != 0) {
+        LOAD_STAT64_FUNC();
+        return _stat64(pathname, buf);
+    }
+
+    ret = stat(pathname, &oldbuf);
+    if (ret)
+        return ret;
+
+    buf->st_dev = oldbuf.st_dev;
+    buf->st_ino = oldbuf.st_ino;
+    buf->st_mode = oldbuf.st_mode;
+    buf->st_nlink = oldbuf.st_nlink;
+    buf->st_uid = oldbuf.st_uid;
+    buf->st_gid = oldbuf.st_gid;
+    buf->st_rdev = oldbuf.st_rdev;
+    buf->st_size = oldbuf.st_size;
+    buf->st_atime = oldbuf.st_atime;
+    buf->st_mtime = oldbuf.st_mtime;
+    buf->st_ctime = oldbuf.st_ctime;
+    buf->st_blksize = oldbuf.st_blksize;
+    buf->st_blocks = oldbuf.st_blocks;
+
+    return 0;
+}
+
 int open64(const char *filename, int flags, ...) {
     va_list args;
     mode_t mode = 0;
 
     debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename);
-    
-    va_start(args, flags);
-    if (flags & O_CREAT)
-        mode = va_arg(args, mode_t);
-    va_end(args);
+
+    if (flags & O_CREAT) {
+        va_start(args, flags);
+        if (sizeof(mode_t) < sizeof(int))
+            mode = va_arg(args, int);
+        else
+            mode = va_arg(args, mode_t);
+        va_end(args);
+    }
 
     if (strcmp(filename, "/dev/dsp") != 0 &&
         strcmp(filename, "/dev/adsp") != 0 &&
@@ -2103,14 +2523,72 @@ int open64(const char *filename, int flags, ...) {
         return _open64(filename, flags, mode);
     }
 
-    return open(filename, flags, mode);
+    return real_open(filename, flags, mode);
+}
+
+#endif
+
+#ifdef _STAT_VER
+
+int __xstat(int ver, const char *pathname, struct stat *buf) {
+    if (!pathname || !buf) {
+        errno = EFAULT;
+        return -1;
+    }
+
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": __xstat(%s)\n", pathname);
+
+    if (strcmp(pathname, "/dev/dsp") != 0 &&
+        strcmp(pathname, "/dev/adsp") != 0 &&
+        strcmp(pathname, "/dev/sndstat") != 0 &&
+        strcmp(pathname, "/dev/mixer") != 0) {
+        LOAD_XSTAT_FUNC();
+        return ___xstat(ver, pathname, buf);
+    }
+
+    if (ver != _STAT_VER) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    return stat(pathname, buf);
+}
+
+#ifdef HAVE_OPEN64
+
+int __xstat64(int ver, const char *pathname, struct stat64 *buf) {
+    if (!pathname || !buf) {
+        errno = EFAULT;
+        return -1;
+    }
+
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": __xstat64(%s)\n", pathname);
+
+    if (strcmp(pathname, "/dev/dsp") != 0 &&
+        strcmp(pathname, "/dev/adsp") != 0 &&
+        strcmp(pathname, "/dev/sndstat") != 0 &&
+        strcmp(pathname, "/dev/mixer") != 0) {
+        LOAD_XSTAT64_FUNC();
+        return ___xstat64(ver, pathname, buf);
+    }
+
+    if (ver != _STAT_VER) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    return stat64(pathname, buf);
 }
 
+#endif
+
+#endif
+
 FILE* fopen(const char *filename, const char *mode) {
     FILE *f = NULL;
     int fd;
     mode_t m;
-    
+
     debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename);
 
     if (strcmp(filename, "/dev/dsp") != 0 &&
@@ -2137,17 +2615,19 @@ FILE* fopen(const char *filename, const char *mode) {
     if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
         m = O_RDWR;
 
-    if ((fd = open(filename, m)) < 0)
+    if ((fd = real_open(filename, m, 0)) < 0)
         return NULL;
 
     if (!(f = fdopen(fd, mode))) {
         close(fd);
         return NULL;
     }
-    
+
     return f;
 }
 
+#ifdef HAVE_OPEN64
+
 FILE *fopen64(const char *filename, const char *mode) {
 
     debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename);
@@ -2163,6 +2643,8 @@ FILE *fopen64(const char *filename, const char *mode) {
     return fopen(filename, mode);
 }
 
+#endif
+
 int fclose(FILE *f) {
     fd_info *i;
 
@@ -2184,9 +2666,9 @@ int fclose(FILE *f) {
     /* Dirty trick to avoid that the fd is not freed twice, once by us
      * and once by the real fclose() */
     i->app_fd = -1;
-    
+
     fd_info_unref(i);
-    
+
     function_exit();
 
     LOAD_FCLOSE_FUNC();