snd_hda: Implement automatic redirection between associations

For audio to be redirected to the headphones/headset after plugging the
jack, or back to the speaker/internal mic when unplugging it, the
speaker and headphone pins need to be part of the same association
(i.e., the same PCM device). This patch makes it possible to redirect
audio even between different associations, which can reduce the need for
manual pin patching.

The idea is that we issue a devctl_notify() from within the jack
detection callback whenever a jack is (un-)plugged to redirect audio to
the appropriate device. Then the snd.conf devd script is responsible for
using virtual_oss to change the playback/recording device to whatever
snd_hda(4) selected. The reason for requiring virtual_oss is that it has
hot-swapping support, which is necessary for jack redirection.

Sponsored by:	The FreeBSD Foundation
MFC after:	2 days
Differential Revision:	https://reviews.freebsd.org/D50070
This commit is contained in:
Christos Margiolis 2025-09-30 12:52:44 +02:00
parent 3cbd1f8168
commit 2ffaca551e
4 changed files with 84 additions and 13 deletions

View file

@ -51,6 +51,11 @@ NVMEDIR= ${DEVDDIR}
NVME+= nvmf.conf
NVMEPACKAGE= nvme-tools
CONFGROUPS+= SND
SNDDIR= ${DEVDDIR}
SND+= snd.conf
SNDPACKAGE= snd
.if ${MK_USB} != "no"
DEVD+= uath.conf ulpt.conf
.endif

View file

@ -652,6 +652,22 @@ and
for details.
.El
.Pp
.Bl -column "System" "Subsystem" "1234567" -compact
.Sy "System" Ta Sy "Subsystem" Ta Sy "Type" Ta Sy "Description"
.It Li SND Ta Ta Ta
Events related to the
.Xr sound 4
driver.
.It Li SND Ta Li CONN Ta Li IN Ta
Connected input device specified in
.Pa cdev
variable.
.It Li SND Ta Li CONN Ta Li OUT Ta
Connected output device specified in
.Pa cdev
variable.
.El
.Pp
.\"
.\" End of tables
.\"

23
sbin/devd/snd.conf Normal file
View file

@ -0,0 +1,23 @@
# Audio redirection
notify 0 {
match "system" "SND";
match "subsystem" "CONN";
match "type" "IN";
match "cdev" "dsp[0-9]+";
# FIXME: We are hardcoding /dev/vdsp.ctl here, simply because it is a
# common virtual_oss control device name. Until we find a proper way to
# define control devices here, /dev/vdsp.ctl can be changed to the
# control device of choice.
action "/usr/sbin/virtual_oss_cmd /dev/vdsp.ctl -R /dev/$cdev";
};
notify 0 {
match "system" "SND";
match "subsystem" "CONN";
match "type" "OUT";
match "cdev" "dsp[0-9]+";
# FIXME: See comment above.
action "/usr/sbin/virtual_oss_cmd /dev/vdsp.ctl -P /dev/$cdev";
};

View file

@ -532,9 +532,11 @@ static void
hdaa_presence_handler(struct hdaa_widget *w)
{
struct hdaa_devinfo *devinfo = w->devinfo;
struct hdaa_audio_as *as;
struct hdaa_audio_as *as, *asp;
char buf[32];
uint32_t res;
int connected, old;
int connected, old, i;
bool active;
if (w->enable == 0 || w->type !=
HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
@ -552,13 +554,6 @@ hdaa_presence_handler(struct hdaa_widget *w)
if (connected == old)
return;
w->wclass.pin.connected = connected;
HDA_BOOTVERBOSE(
if (connected || old != 2) {
device_printf(devinfo->dev,
"Pin sense: nid=%d sense=0x%08x (%sconnected)\n",
w->nid, res, !connected ? "dis" : "");
}
);
as = &devinfo->as[w->bindas];
if (as->hpredir >= 0 && as->pins[15] == w->nid)
@ -567,6 +562,38 @@ hdaa_presence_handler(struct hdaa_widget *w)
hdaa_autorecsrc_handler(as, w);
if (old != 2)
hdaa_channels_handler(as);
if (connected || old != 2) {
HDA_BOOTVERBOSE(
device_printf(devinfo->dev,
"Pin sense: nid=%d sense=0x%08x (%sconnected)\n",
w->nid, res, !connected ? "dis" : "");
);
if (as->hpredir >= 0)
return;
for (i = 0, active = false; i < devinfo->num_devs; i++) {
if (device_get_unit(devinfo->devs[i].dev) == snd_unit) {
active = true;
break;
}
}
/* Proceed only if we are currently using this codec. */
if (!active)
return;
for (i = 0; i < devinfo->ascnt; i++) {
asp = &devinfo->as[i];
if (!asp->enable)
continue;
if ((connected && asp->index == as->index) ||
(!connected && asp->dir == as->dir)) {
snprintf(buf, sizeof(buf), "cdev=dsp%d",
device_get_unit(asp->pdevinfo->dev));
devctl_notify("SND", "CONN",
asp->dir == HDAA_CTL_IN ? "IN" : "OUT", buf);
break;
}
}
}
}
/*
@ -6193,16 +6220,16 @@ hdaa_configure(device_t dev)
device_printf(dev, "Applying direct built-in patches...\n");
);
hdaa_patch_direct(devinfo);
HDA_BOOTHVERBOSE(
device_printf(dev, "Pin sense init...\n");
);
hdaa_sense_init(devinfo);
HDA_BOOTHVERBOSE(
device_printf(dev, "Creating PCM devices...\n");
);
hdaa_unlock(devinfo);
hdaa_create_pcms(devinfo);
hdaa_lock(devinfo);
HDA_BOOTHVERBOSE(
device_printf(dev, "Pin sense init...\n");
);
hdaa_sense_init(devinfo);
HDA_BOOTVERBOSE(
if (devinfo->quirks != 0) {