git: 65dec169468c - stable/14 - sound: Make dev.pcm.X.mode dynamic

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Mon, 17 Mar 2025 18:36:54 UTC
The branch stable/14 has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=65dec169468cef17fcda8e7e72a460c1df2a630f

commit 65dec169468cef17fcda8e7e72a460c1df2a630f
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2025-03-10 20:47:39 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2025-03-17 18:29:17 +0000

    sound: Make dev.pcm.X.mode dynamic
    
    Currently dev.pcm.X.mode is calculated only once in pcm_sysinit(), which
    is called by pcm_register() during attach, but this can result in
    inconsistencies.
    
    For some context, what pcm_mode_init() does is, it checks if "playcount"
    is positive, in which case we assume the device supports playback. The
    same is done for "reccount" for recording, and if "mixer_dev" is not
    NULL, we know the device has a mixer.
    
    The "playcount" and "reccount" variables correspond to the number of
    _primary_ playback/recording channels, so we can assume that the primary
    channels have been created before reaching pcm_mode_init(). However, for
    the mixer that's not always the case. If the mixer is created _after_
    pcm_register(), as is the case for snd_dummy(4) for example,
    pcm_mode_init() will see that "mixer_dev" is NULL, and report that the
    device does not have a mixer, whereas in reality we just created it
    afterwards.
    
    While this could be fixed by simply creating the mixers always before
    pcm_register(), it is better to be robust and calculate the mode
    dynamically.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D49024
    
    (cherry picked from commit ab95710f30f7255d3a6be22a1a2c375ee0f96868)
---
 sys/dev/sound/pcm/sound.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index ac5d11b83ea3..bbb6c8dff0de 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -361,28 +361,32 @@ sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
 	return (err);
 }
 
-static u_int8_t
-pcm_mode_init(struct snddev_info *d)
+static int
+sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)
 {
-	u_int8_t mode = 0;
+	struct snddev_info *d;
+	int mode = 0;
 
+	d = oidp->oid_arg1;
+	if (!PCM_REGISTERED(d))
+		return (ENODEV);
+
+	PCM_LOCK(d);
 	if (d->playcount > 0)
 		mode |= PCM_MODE_PLAY;
 	if (d->reccount > 0)
 		mode |= PCM_MODE_REC;
 	if (d->mixer_dev != NULL)
 		mode |= PCM_MODE_MIXER;
+	PCM_UNLOCK(d);
 
-	return (mode);
+	return (sysctl_handle_int(oidp, &mode, 0, req));
 }
 
 static void
 pcm_sysinit(device_t dev)
 {
   	struct snddev_info *d = device_get_softc(dev);
-	u_int8_t mode;
-
-	mode = pcm_mode_init(d);
 
 	sysctl_ctx_init(&d->play_sysctl_ctx);
 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
@@ -403,9 +407,10 @@ pcm_sysinit(device_t dev)
 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d,
 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
 	    "bit-perfect playback/recording (0=disable, 1=enable)");
-	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
-	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
-	    OID_AUTO, "mode", CTLFLAG_RD, NULL, mode,
+	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
+	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
+	    "mode", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, d, sizeof(d),
+	    sysctl_dev_pcm_mode, "I",
 	    "mode (1=mixer, 2=play, 4=rec. The values are OR'ed if more than "
 	    "one mode is supported)");
 	vchan_initsys(dev);