Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/usermode add vaudio(4) audio device driver



details:   https://anonhg.NetBSD.org/src/rev/ac04b4afe068
branches:  trunk
changeset: 772257:ac04b4afe068
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Mon Dec 26 21:06:42 2011 +0000

description:
add vaudio(4) audio device driver

diffstat:

 sys/arch/usermode/conf/GENERIC.common |    6 +-
 sys/arch/usermode/conf/files.usermode |    6 +-
 sys/arch/usermode/dev/mainbus.c       |   11 +-
 sys/arch/usermode/dev/vaudio.c        |  423 ++++++++++++++++++++++++++++++++++
 sys/arch/usermode/include/mainbus.h   |    6 +-
 sys/arch/usermode/include/thunk.h     |   19 +-
 sys/arch/usermode/usermode/machdep.c  |   19 +-
 sys/arch/usermode/usermode/thunk.c    |   88 ++++++-
 8 files changed, 567 insertions(+), 11 deletions(-)

diffs (truncated from 730 to 300 lines):

diff -r b7600dc0b58d -r ac04b4afe068 sys/arch/usermode/conf/GENERIC.common
--- a/sys/arch/usermode/conf/GENERIC.common     Mon Dec 26 20:26:38 2011 +0000
+++ b/sys/arch/usermode/conf/GENERIC.common     Mon Dec 26 21:06:42 2011 +0000
@@ -1,9 +1,9 @@
-# $NetBSD: GENERIC.common,v 1.2 2011/12/26 12:39:19 jmcneill Exp $
+# $NetBSD: GENERIC.common,v 1.3 2011/12/26 21:06:42 jmcneill Exp $
 
 include "arch/usermode/conf/std.usermode"
 
 options        INCLUDE_CONFIG_FILE
-#ident                 "GENERIC-$Revision: 1.2 $"
+#ident                 "GENERIC-$Revision: 1.3 $"
 maxusers       32
 
 makeoptions    DEBUG="-O1 -g3"
@@ -50,6 +50,8 @@
 ttycons0       at mainbus?
 ld0            at mainbus?
 veth0          at mainbus?
+vaudio0                at mainbus?
+audio0         at vaudio0
 
 #options               SDL
 #genfb0                at thunkbus?
diff -r b7600dc0b58d -r ac04b4afe068 sys/arch/usermode/conf/files.usermode
--- a/sys/arch/usermode/conf/files.usermode     Mon Dec 26 20:26:38 2011 +0000
+++ b/sys/arch/usermode/conf/files.usermode     Mon Dec 26 21:06:42 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.usermode,v 1.13 2011/12/26 12:39:19 jmcneill Exp $
+# $NetBSD: files.usermode,v 1.14 2011/12/26 21:06:42 jmcneill Exp $
 
 maxpartitions 8
 maxusers 8 16 64
@@ -31,6 +31,10 @@
 attach veth at thunkbus
 file   arch/usermode/dev/if_veth.c             veth
 
+device vaudio { } : audiobus, auconv, aurateconv, mulaw
+attach vaudio at thunkbus
+file   arch/usermode/dev/vaudio.c              vaudio
+
 attach ld at thunkbus with ld_thunkbus
 file   arch/usermode/dev/ld_thunkbus.c         ld_thunkbus
 
diff -r b7600dc0b58d -r ac04b4afe068 sys/arch/usermode/dev/mainbus.c
--- a/sys/arch/usermode/dev/mainbus.c   Mon Dec 26 20:26:38 2011 +0000
+++ b/sys/arch/usermode/dev/mainbus.c   Mon Dec 26 21:06:42 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mainbus.c,v 1.6 2011/12/26 12:39:19 jmcneill Exp $ */
+/* $NetBSD: mainbus.c,v 1.7 2011/12/26 21:06:42 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -31,7 +31,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.6 2011/12/26 12:39:19 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.7 2011/12/26 21:06:42 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -57,6 +57,7 @@
 extern char *usermode_root_image_path;
 extern char *usermode_tap_device;
 extern char *usermode_tap_eaddr;
+extern char *usermode_audio_device;
 
 static int
 mainbus_match(device_t parent, cfdata_t match, void *opaque)
@@ -97,6 +98,12 @@
                config_found_ia(self, "thunkbus", &taa, mainbus_print);
        }
 
+       if (usermode_audio_device) {
+               taa.taa_type = THUNKBUS_TYPE_VAUDIO;
+               taa.u.vaudio.device = usermode_audio_device;
+               config_found_ia(self, "thunkbus", &taa, mainbus_print);
+       }
+
        if (usermode_root_image_path) {
                taa.taa_type = THUNKBUS_TYPE_DISKIMAGE;
                taa.u.diskimage.path = usermode_root_image_path;
diff -r b7600dc0b58d -r ac04b4afe068 sys/arch/usermode/dev/vaudio.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/usermode/dev/vaudio.c    Mon Dec 26 21:06:42 2011 +0000
@@ -0,0 +1,423 @@
+/* $NetBSD: vaudio.c,v 1.1 2011/12/26 21:09:22 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2011 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: vaudio.c,v 1.1 2011/12/26 21:09:22 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/audioio.h>
+
+#include <machine/mainbus.h>
+#include <machine/thunk.h>
+
+#include <dev/audio_if.h>
+#include <dev/auconv.h>
+
+static const struct audio_format vaudio_audio_formats[1] = {
+       { NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
+         2, AUFMT_STEREO, 0, { 8000, 48000 } },
+};
+
+struct vaudio_stream {
+       struct vaudio_softc             *st_softc;
+       void *                          st_sih;
+       callout_t                       st_callout;
+       void                            (*st_intr)(void *);
+       void *                          st_intrarg;
+       uint8_t *                       st_start;
+       uint8_t *                       st_end;
+       uint8_t *                       st_cur;
+       int                             st_blksize;
+       bool                            st_running;
+};
+
+struct vaudio_softc {
+       device_t                        sc_dev;
+       void *                          sc_audiodev;
+       const char *                    sc_audiopath;
+       int                             sc_audiofd;
+       struct audio_encoding_set *     sc_encodings;
+       audio_params_t                  sc_pparam;
+       audio_params_t                  sc_rparam;
+       kmutex_t                        sc_lock;
+       kmutex_t                        sc_intr_lock;
+
+       struct vaudio_stream            sc_play;
+       struct vaudio_stream            sc_record;
+};
+
+static int     vaudio_match(device_t, cfdata_t, void *);
+static void    vaudio_attach(device_t, device_t, void *);
+
+static void    vaudio_intr(void *);
+static void    vaudio_softintr_play(void *);
+static void    vaudio_softintr_record(void *);
+
+static int     vaudio_open(void *, int);
+static void    vaudio_close(void *);
+static int     vaudio_drain(void *);
+static int     vaudio_query_encoding(void *, audio_encoding_t *);
+static int     vaudio_set_params(void *, int, int, audio_params_t *,
+                                 audio_params_t *, stream_filter_list_t *,
+                                 stream_filter_list_t *);
+static int     vaudio_commit_settings(void *);
+static int     vaudio_trigger_output(void *, void *, void *, int,
+                                     void (*)(void *), void *,
+                                     const audio_params_t *);
+static int     vaudio_trigger_input(void *, void *, void *, int,
+                                    void (*)(void *), void *,
+                                    const audio_params_t *);
+static int     vaudio_halt_output(void *);
+static int     vaudio_halt_input(void *);
+static int     vaudio_getdev(void *, struct audio_device *);
+static int     vaudio_set_port(void *, mixer_ctrl_t *);
+static int     vaudio_get_port(void *, mixer_ctrl_t *);
+static int     vaudio_query_devinfo(void *, mixer_devinfo_t *);
+static int     vaudio_get_props(void *);
+static void    vaudio_get_locks(void *, kmutex_t **, kmutex_t **);     
+
+CFATTACH_DECL_NEW(vaudio, sizeof(struct vaudio_softc),
+    vaudio_match, vaudio_attach, NULL, NULL);
+
+static const struct audio_hw_if vaudio_hw_if = {
+       .open = vaudio_open,
+       .close = vaudio_close,
+       .drain = vaudio_drain,
+       .query_encoding = vaudio_query_encoding,
+       .set_params = vaudio_set_params,
+       .commit_settings = vaudio_commit_settings,
+       .halt_output = vaudio_halt_output,
+       .halt_input = vaudio_halt_input,
+       .getdev = vaudio_getdev,
+       .set_port = vaudio_set_port,
+       .get_port = vaudio_get_port,
+       .query_devinfo = vaudio_query_devinfo,
+       .get_props = vaudio_get_props,
+       .trigger_output = vaudio_trigger_output,
+       .trigger_input = vaudio_trigger_input,
+       .get_locks = vaudio_get_locks,
+};
+
+static int
+vaudio_match(device_t parent, cfdata_t match, void *opaque)
+{
+       struct thunkbus_attach_args *taa = opaque;
+
+       if (taa->taa_type != THUNKBUS_TYPE_VAUDIO)
+               return 0;
+
+       return 1;
+}
+
+static void
+vaudio_attach(device_t parent, device_t self, void *opaque)
+{
+       struct vaudio_softc *sc = device_private(self);
+       struct thunkbus_attach_args *taa = opaque;
+       int error;
+
+       aprint_naive("\n");
+       aprint_normal(": Virtual Audio (device = %s)\n", taa->u.vaudio.device);
+
+       sc->sc_dev = self;
+       sc->sc_audiopath = taa->u.vaudio.device;
+       sc->sc_audiofd = thunk_audio_open(sc->sc_audiopath);
+       if (sc->sc_audiofd == -1) {
+               aprint_error_dev(self, "couldn't open audio device: %d\n",
+                   thunk_geterrno());
+               return;
+       }
+
+       mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
+       mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
+
+       error = auconv_create_encodings(vaudio_audio_formats,
+           __arraycount(vaudio_audio_formats), &sc->sc_encodings);
+       if (error) {
+               aprint_error_dev(self, "couldn't create encodings\n");
+               return;
+       }
+
+       sc->sc_play.st_softc = sc;
+       sc->sc_play.st_sih = softint_establish(SOFTINT_SERIAL,
+           vaudio_softintr_play, &sc->sc_play);
+       callout_init(&sc->sc_play.st_callout, 0);
+       callout_setfunc(&sc->sc_play.st_callout, vaudio_intr, &sc->sc_play);
+
+       sc->sc_record.st_softc = sc;
+       sc->sc_record.st_sih = softint_establish(SOFTINT_SERIAL,
+           vaudio_softintr_record, &sc->sc_record);
+       callout_init(&sc->sc_record.st_callout, 0);
+       callout_setfunc(&sc->sc_record.st_callout, vaudio_intr, &sc->sc_record);
+
+       sc->sc_audiodev = audio_attach_mi(&vaudio_hw_if, sc, self);
+}
+
+static void
+vaudio_intr(void *opaque)
+{
+       struct vaudio_stream *st = opaque;
+
+       softint_schedule(st->st_sih);
+}
+
+static void
+vaudio_softintr_play(void *opaque)
+{
+       struct vaudio_stream *st = opaque;
+       struct vaudio_softc *sc = st->st_softc;
+
+       while (st->st_running) {
+               if (thunk_audio_pollout(sc->sc_audiofd) < st->st_blksize)
+                       break;
+               thunk_audio_write(sc->sc_audiofd, st->st_cur, st->st_blksize);
+               mutex_spin_enter(&sc->sc_intr_lock);
+               st->st_intr(st->st_intrarg);
+               mutex_spin_exit(&sc->sc_intr_lock);
+               st->st_cur += st->st_blksize;
+               if (st->st_cur >= st->st_end)
+                       st->st_cur = st->st_start;
+       }
+
+       if (st->st_running) {
+               callout_schedule(&st->st_callout, 1);
+       }
+}



Home | Main Index | Thread Index | Old Index