From nobody Sat Jul 27 11:56:19 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WWNPc1m6bz5S6hh; Sat, 27 Jul 2024 11:56:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WWNPb6xd8z42j5; Sat, 27 Jul 2024 11:56:19 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722081380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=H/z63VDyCCc7RkE/9SlnXrXncykaSTySxFhHsFhnYks=; b=fQkTxTO+ffQfIVGuY49EVpLNBfJBspzPbuTy+PlYS4XtH72aGhm5bMNpJLOdznRsZ3GtWx Ap26qOd/C62qvqAKCf2hkQPoVFNdrd0yKjdoQ7d6kMGivvKNCddAoEzlEEgm0GJ2cbW+rx sqXSQAHF+gH/PexChZo1N/HYbNMGfHW0J91jU0D12EqKhE9wB9aMnXsnUvuzG7mMpsL1ma Y0OWwQ393n0bFXub+vycAAmkgDY/vrVUDicFjjd5NWfs9tDv7l9eyQiS9J50nXFtNDyhXJ DOw9DI5Hp2/4GLJ0uZkvj5sG2misnxCLl+Y1nF5C8QUoUSreYRfDfXvKBF/VrA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1722081380; a=rsa-sha256; cv=none; b=LgyFWvI3KoE1I8eTmJQvCUjw9HSrK1ydzCmzbB3Prg0Dtu2ITxVzkSHAMOmspMHDETIEqV RIgNWmRGuV+xUO77rw6e5sY9zMBXyTt2YN8+cCmAmxhGJXc5EE8DugG1g5BJqhVG8X9bj4 BTO6NvjST7KPf7D6fg1ocQd1+cQsC3xEqUV33adgBLzsHFXuWsZGbeHWCkx2bWtxy58fqt f9K2KnhYqaL8Ui358sSQXWCnbUo3EhnqycaTGtyY/DvE+MT5vwjDz3QgUUMPIcEzZ9Uome 10+TTfE4qoGsAe3INzZMyLKRt/npxT9IWJOoxhYa2O4h7ukcbod+OGQ1WIVvpg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722081380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=H/z63VDyCCc7RkE/9SlnXrXncykaSTySxFhHsFhnYks=; b=SVYiCV+0QFmLRFVe5oTGz48JfHiUFF7UXowBDNX6fKsayLk/6Dnt9AaaRUpCXxZswpFb4B e/EVdPypSuOLee+4XFT42ZEh128J4XWflluVBWDSEwQEVSZxL6TBMr/cYB113hH82aeaxg 9b0puD09LeSqVDdi0gmljec1xMPyUfh5LYlJLRnCtz4ZSdfIz3iAY23WT1QWH8IQxtXqVn WtbKapOkPyPWRtyNTiiwbpxt+FvgRlPuEMTBK/5W+wSA5ID6pXbMDcNuL/obiLavlBIJnX r/eagOUTPAwZ/A0EkMCjGR8pFVWu7YBeaYH+bWyLtDrG4Vk99HL1GTN/ULA/ug== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WWNPb6JFHz15yL; Sat, 27 Jul 2024 11:56:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46RBuJNc039700; Sat, 27 Jul 2024 11:56:19 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46RBuJ48039697; Sat, 27 Jul 2024 11:56:19 GMT (envelope-from git) Date: Sat, 27 Jul 2024 11:56:19 GMT Message-Id: <202407271156.46RBuJ48039697@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: c15c9315b2cb - main - sound: Implement dummy driver List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c15c9315b2cb7601cc337f7d5a8e124f4b2d5861 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=c15c9315b2cb7601cc337f7d5a8e124f4b2d5861 commit c15c9315b2cb7601cc337f7d5a8e124f4b2d5861 Author: Christos Margiolis AuthorDate: 2024-07-27 11:54:45 +0000 Commit: Christos Margiolis CommitDate: 2024-07-27 11:54:45 +0000 sound: Implement dummy driver Sponsored by: The FreeBSD Foundation MFC after: 2 days Reviewed by: dev_submerge.ch Differential Revision: https://reviews.freebsd.org/D45967 --- share/man/man4/Makefile | 1 + share/man/man4/snd_dummy.4 | 69 +++++++ sys/dev/sound/dummy.c | 353 ++++++++++++++++++++++++++++++++ sys/modules/sound/driver/Makefile | 1 + sys/modules/sound/driver/dummy/Makefile | 7 + 5 files changed, 431 insertions(+) diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile index 36d945d64a60..b4a8e484b137 100644 --- a/share/man/man4/Makefile +++ b/share/man/man4/Makefile @@ -528,6 +528,7 @@ MAN= aac.4 \ snd_cmi.4 \ snd_cs4281.4 \ snd_csa.4 \ + snd_dummy.4 \ snd_emu10k1.4 \ snd_emu10kx.4 \ snd_envy24.4 \ diff --git a/share/man/man4/snd_dummy.4 b/share/man/man4/snd_dummy.4 new file mode 100644 index 000000000000..2b9d26d318ef --- /dev/null +++ b/share/man/man4/snd_dummy.4 @@ -0,0 +1,69 @@ +.\"- +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.\" Copyright (c) 2024 The FreeBSD Foundation +.\" +.\" Portions of this software were developed by Christos Margiolis +.\" under sponsorship from the FreeBSD Foundation. +.\" +.\" 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 AUTHOR 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 AUTHOR 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. +.\" +.Dd July 15, 2024 +.Dt SND_DUMMY 4 +.Os +.Sh NAME +.Nm snd_dummy +.Nd Dummy audio driver +.Sh SYNOPSIS +To load the driver at boot time, place the following line in +.Xr loader.conf 5 : +.Bd -literal -offset indent +snd_dummy_load="YES" +.Ed +.Sh DESCRIPTION +The +.Nm +driver implements a virtual testing device, meaning it does not correspond to a +physical sound card. +It is intended for testing, so that test programs do not need to rely on +hardware being present in the machine in order to run. +.Pp +The driver attaches as a regular PCM device, with two channels (one playback +and one recording), as well as a mixer. +.Pp +Playback works by discarding all input, and recording by returning silence +(zeros). +.Sh SEE ALSO +.Xr sound 4 , +.Xr loader.conf 5 , +.Xr loader 8 +.Sh AUTHORS +The +.Nm +driver was implemented by +.An Christos Margiolis Aq Mt christos@FreeBSD.org +under sponsorship from the +.Fx +Foundation. +.Sh CAVEATS +Because the driver automatically attaches once the module is loaded, it can +only be attached once. diff --git a/sys/dev/sound/dummy.c b/sys/dev/sound/dummy.c new file mode 100644 index 000000000000..f8193a73fcc8 --- /dev/null +++ b/sys/dev/sound/dummy.c @@ -0,0 +1,353 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 The FreeBSD Foundation + * + * This software was developed by Christos Margiolis + * under sponsorship from the FreeBSD Foundation. + * + * 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 AUTHOR 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 AUTHOR 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 + +#include +#include +#include +#include + +#ifdef HAVE_KERNEL_OPTION_HEADERS +#include "opt_snd.h" +#endif + +#include +#include + +#define DUMMY_NPCHAN 1 +#define DUMMY_NRCHAN 1 +#define DUMMY_NCHAN (DUMMY_NPCHAN + DUMMY_NRCHAN) + +struct dummy_chan { + struct dummy_softc *sc; + struct pcm_channel *chan; + struct snd_dbuf *buf; + struct pcmchan_caps *caps; + uint32_t ptr; + int dir; + int run; +}; + +struct dummy_softc { + struct snddev_info info; + device_t dev; + uint32_t cap_fmts[4]; + struct pcmchan_caps caps; + int chnum; + struct dummy_chan chans[DUMMY_NCHAN]; + struct callout callout; + struct mtx *lock; +}; + +static void +dummy_chan_io(void *arg) +{ + struct dummy_softc *sc = arg; + struct dummy_chan *ch; + int i = 0; + + snd_mtxlock(sc->lock); + + for (i = 0; i < sc->chnum; i++) { + ch = &sc->chans[i]; + if (!ch->run) + continue; + if (ch->dir == PCMDIR_PLAY) + ch->ptr += sndbuf_getblksz(ch->buf); + else + sndbuf_fillsilence(ch->buf); + snd_mtxunlock(sc->lock); + chn_intr(ch->chan); + snd_mtxlock(sc->lock); + } + callout_schedule(&sc->callout, 1); + + snd_mtxunlock(sc->lock); +} + +static int +dummy_chan_free(kobj_t obj, void *data) +{ + struct dummy_chan *ch =data; + uint8_t *buf; + + buf = sndbuf_getbuf(ch->buf); + if (buf != NULL) + free(buf, M_DEVBUF); + + return (0); +} + +static void * +dummy_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, + struct pcm_channel *c, int dir) +{ + struct dummy_softc *sc; + struct dummy_chan *ch; + uint8_t *buf; + size_t bufsz; + + sc = devinfo; + + snd_mtxlock(sc->lock); + + ch = &sc->chans[sc->chnum++]; + ch->sc = sc; + ch->dir = dir; + ch->chan = c; + ch->buf = b; + ch->caps = &sc->caps; + + snd_mtxunlock(sc->lock); + + bufsz = pcm_getbuffersize(sc->dev, 2048, 2048, 65536); + buf = malloc(bufsz, M_DEVBUF, M_WAITOK | M_ZERO); + if (sndbuf_setup(ch->buf, buf, bufsz) != 0) { + dummy_chan_free(obj, ch); + return (NULL); + } + + return (ch); +} + +static int +dummy_chan_setformat(kobj_t obj, void *data, uint32_t format) +{ + struct dummy_chan *ch = data; + int i; + + for (i = 0; ch->caps->fmtlist[i]; i++) + if (format == ch->caps->fmtlist[i]) + return (0); + + return (EINVAL); +} + +static uint32_t +dummy_chan_setspeed(kobj_t obj, void *data, uint32_t speed) +{ + struct dummy_chan *ch = data; + + RANGE(speed, ch->caps->minspeed, ch->caps->maxspeed); + + return (speed); +} + +static uint32_t +dummy_chan_setblocksize(kobj_t obj, void *data, uint32_t blocksize) +{ + struct dummy_chan *ch = data; + + return (sndbuf_getblksz(ch->buf)); +} + +static int +dummy_chan_trigger(kobj_t obj, void *data, int go) +{ + struct dummy_chan *ch = data; + struct dummy_softc *sc = ch->sc; + + snd_mtxlock(sc->lock); + + switch (go) { + case PCMTRIG_START: + if (!callout_active(&sc->callout)) + callout_reset(&sc->callout, 1, dummy_chan_io, sc); + ch->ptr = 0; + ch->run = 1; + break; + case PCMTRIG_STOP: + case PCMTRIG_ABORT: + ch->run = 0; + if (callout_active(&sc->callout)) + callout_stop(&sc->callout); + default: + break; + } + + snd_mtxunlock(sc->lock); + + return (0); +} + +static uint32_t +dummy_chan_getptr(kobj_t obj, void *data) +{ + struct dummy_chan *ch = data; + + return (ch->run ? ch->ptr : 0); +} + +static struct pcmchan_caps * +dummy_chan_getcaps(kobj_t obj, void *data) +{ + struct dummy_chan *ch = data; + + return (ch->caps); +} + +static kobj_method_t dummy_chan_methods[] = { + KOBJMETHOD(channel_init, dummy_chan_init), + KOBJMETHOD(channel_free, dummy_chan_free), + KOBJMETHOD(channel_setformat, dummy_chan_setformat), + KOBJMETHOD(channel_setspeed, dummy_chan_setspeed), + KOBJMETHOD(channel_setblocksize,dummy_chan_setblocksize), + KOBJMETHOD(channel_trigger, dummy_chan_trigger), + KOBJMETHOD(channel_getptr, dummy_chan_getptr), + KOBJMETHOD(channel_getcaps, dummy_chan_getcaps), + KOBJMETHOD_END +}; + +CHANNEL_DECLARE(dummy_chan); + +static int +dummy_mixer_init(struct snd_mixer *m) +{ + struct dummy_softc *sc; + + sc = mix_getdevinfo(m); + if (sc == NULL) + return (-1); + + pcm_setflags(sc->dev, pcm_getflags(sc->dev) | SD_F_SOFTPCMVOL); + mix_setdevs(m, SOUND_MASK_PCM | SOUND_MASK_VOLUME | SOUND_MASK_RECLEV); + mix_setrecdevs(m, SOUND_MASK_RECLEV); + + return (0); +} + +static int +dummy_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) +{ + return (0); +} + +static uint32_t +dummy_mixer_setrecsrc(struct snd_mixer *m, uint32_t src) +{ + return (src == SOUND_MASK_RECLEV ? src : 0); +} + +static kobj_method_t dummy_mixer_methods[] = { + KOBJMETHOD(mixer_init, dummy_mixer_init), + KOBJMETHOD(mixer_set, dummy_mixer_set), + KOBJMETHOD(mixer_setrecsrc, dummy_mixer_setrecsrc), + KOBJMETHOD_END +}; + +MIXER_DECLARE(dummy_mixer); + +static void +dummy_identify(driver_t *driver, device_t parent) +{ + if (device_find_child(parent, driver->name, -1) != NULL) + return; + if (BUS_ADD_CHILD(parent, 0, driver->name, -1) == NULL) + device_printf(parent, "add child failed\n"); +} + +static int +dummy_probe(device_t dev) +{ + device_set_desc(dev, "Dummy Audio Device"); + + return (0); +} + +static int +dummy_attach(device_t dev) +{ + struct dummy_softc *sc; + char status[SND_STATUSLEN]; + int i = 0; + + sc = device_get_softc(dev); + sc->dev = dev; + sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_dummy softc"); + + sc->cap_fmts[0] = SND_FORMAT(AFMT_S32_LE, 2, 0); + sc->cap_fmts[1] = SND_FORMAT(AFMT_S24_LE, 2, 0); + sc->cap_fmts[2] = SND_FORMAT(AFMT_S16_LE, 2, 0); + sc->cap_fmts[3] = 0; + sc->caps = (struct pcmchan_caps){ + 8000, /* minspeed */ + 96000, /* maxspeed */ + sc->cap_fmts, /* fmtlist */ + 0, /* caps */ + }; + + pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE); + if (pcm_register(dev, sc, DUMMY_NPCHAN, DUMMY_NRCHAN)) + return (ENXIO); + for (i = 0; i < DUMMY_NPCHAN; i++) + pcm_addchan(dev, PCMDIR_PLAY, &dummy_chan_class, sc); + for (i = 0; i < DUMMY_NRCHAN; i++) + pcm_addchan(dev, PCMDIR_REC, &dummy_chan_class, sc); + + snprintf(status, SND_STATUSLEN, "on %s", + device_get_nameunit(device_get_parent(dev))); + pcm_setstatus(dev, status); + mixer_init(dev, &dummy_mixer_class, sc); + callout_init(&sc->callout, 1); + + return (0); +} + +static int +dummy_detach(device_t dev) +{ + struct dummy_softc *sc = device_get_softc(dev); + int err; + + err = pcm_unregister(dev); + snd_mtxfree(sc->lock); + + return (err); +} + +static device_method_t dummy_methods[] = { + /* Device interface */ + DEVMETHOD(device_identify, dummy_identify), + DEVMETHOD(device_probe, dummy_probe), + DEVMETHOD(device_attach, dummy_attach), + DEVMETHOD(device_detach, dummy_detach), + DEVMETHOD_END +}; + +static driver_t dummy_driver = { + "pcm", + dummy_methods, + sizeof(struct dummy_softc), +}; + +DRIVER_MODULE(snd_dummy, nexus, dummy_driver, 0, 0); +MODULE_DEPEND(snd_dummy, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); +MODULE_VERSION(snd_dummy, 1); diff --git a/sys/modules/sound/driver/Makefile b/sys/modules/sound/driver/Makefile index 9b157f0c76ab..ff9499fdf841 100644 --- a/sys/modules/sound/driver/Makefile +++ b/sys/modules/sound/driver/Makefile @@ -8,6 +8,7 @@ SUBDIR= als4000 atiixp cs4281 ${_csa} emu10k1 emu10kx SUBDIR+= envy24 envy24ht es137x fm801 hda hdsp hdspe ich SUBDIR+= ${_maestro3} neomagic solo spicds t4dwave via8233 SUBDIR+= via82c686 vibes driver uaudio +SUBDIR+= dummy .if ${MK_SOURCELESS_UCODE} != "no" _csa= csa diff --git a/sys/modules/sound/driver/dummy/Makefile b/sys/modules/sound/driver/dummy/Makefile new file mode 100644 index 000000000000..fb4127d35073 --- /dev/null +++ b/sys/modules/sound/driver/dummy/Makefile @@ -0,0 +1,7 @@ +.PATH: ${SRCTOP}/sys/dev/sound + +KMOD= snd_dummy +SRCS= bus_if.h device_if.h +SRCS+= dummy.c + +.include