Skip to contentSkip to conversation

ALSA: 6fire: S/PDIF output

PATCH Applied Piotr Gliźniewicz 2 participants 5 messages Oct 16 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: 6fire: S/PDIF output

The DMX 6Fire USB has a coaxial S/PDIF output, which the driver keeps disabled. This series enables it as playback channels 7 and 8 of the existing PCM device, the layout chosen in the RFC thread [1].

Patch 1 takes stream_mutex in the "Digital Thru Playback Route" put. It fixes a race with prepare that exists in mainline, and patch 3 depends on the lock.

Patch 2 adds a playback channel map in the order of the manual of the card, FL FR FC LFE RL RR on Line Out 1 to 6.

Patch 3 adds the output with the "IEC958 Playback Default", "IEC958 Playback Con Mask" and "IEC958 Playback Switch" controls. With 8 channels the card runs at 96 kHz at most. The Pro Audio profile of PipeWire 1.6.9 opened the device with 8 channels, so with this series it gets 8 channels at up to 96 kHz instead of 6 channels at up to 192 kHz. A client that asks for 6 channels still gets 176.4 and 192 kHz.

A UCM profile for alsa-ucm-conf will describe the channels: the default variant with analog 5.1 and S/PDIF as two sinks at up to 96 kHz, and a "HiFi Analog" variant with analog 5.1 only at up to 192 kHz, which turns the switch off while in use. I tested a draft with PipeWire 1.6.9 and this series; I will send it after the kernel patches are accepted.

[1] https://lore.kernel.org/linux-sound/[email protected]/

Piotr Gliźniewicz (3):
ALSA: 6fire: take stream_mutex in the Digital Thru control
ALSA: 6fire: add a playback channel map
ALSA: 6fire: add S/PDIF output

 sound/usb/6fire/control.c |  32 +++-
 sound/usb/6fire/pcm.c     | 334 ++++++++++++++++++++++++++++++++++++--
 sound/usb/6fire/pcm.h     |   7 +-
 3 files changed, 351 insertions(+), 22 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/3 ALSA: 6fire: take stream_mutex in the Digital Thru control

usb6fire_control_digital_thru_put() calls usb6fire_control_streaming_update(), which on an idle card with Digital Thru on selects altsetting 2 and writes the clock register for 96 kHz through usb6fire_control_set_rate(). usb6fire_pcm_prepare() calls both functions through usb6fire_pcm_set_rate() under rt->stream_mutex. The put does not take stream_mutex, so the two can run at the same time.

500 runs of a 0.2 s aplay at 48 kHz while a second loop toggled "Digital Thru Playback Route" with amixer (12693 on/off cycles, kernel 7.2.9): in 216 cases the two usb_set_interface() calls ran at the same time and the kernel logged "sysfs: cannot create duplicate filename '.../3-2:1.1/ep_82'" with a stack dump. 27 of the aplay runs failed, and for each of them the kernel logged "could not start pcm stream".

Take stream_mutex in the put. The pcm runtime is created before the controls and freed with the card, so chip->pcm is valid whenever the put runs. The put waits for a running prepare, which makes synchronous USB transfers under stream_mutex; on a card that responds late the wait can reach about 12 s, or 24 s with Digital Thru on, and during the wait the other controls of the card can be neither read nor written, because snd_ctl_elem_write() holds controls_rwsem around the put. Before this patch the put already made transfers of the same kind under controls_rwsem. No code that holds stream_mutex takes controls_rwsem.

The same test on kernel 7.2.10 with this patch (12863 cycles) gave no failed aplay run and no "duplicate filename" message.

Fixes 2475b0d40761 ("ALSA: 6fire - Add support of digital-thru mixer")
Assisted-by LLM
---
 sound/usb/6fire/control.c | 7 +++++++
 1 file changed, 7 insertions(+)
sound/usb/6fire/control.c +7 -0
index 10104cb42..020e9676d 100644
@@ -20,6 +20,7 @@
 #include "control.h"
 #include "comm.h"
 #include "chip.h"
+#include "pcm.h"
 
 static const char * const opt_coax_texts[2] = { "Optical", "Coax" };
 static const char * const line_phono_texts[2] = { "Line", "Phono" };
@@ -383,6 +384,12 @@ static int usb6fire_control_digital_thru_put(struct snd_kcontrol *kcontrol,
 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
 	int changed = 0;
 
+	/*
+	 * with Digital Thru on, streaming_update() selects the altsetting and
+	 * writes the clock register of an idle card; prepare does the same
+	 * under stream_mutex
+	 */
+	guard(mutex)(&rt->chip->pcm->stream_mutex);
 	if (rt->digital_thru_switch != ucontrol->value.integer.value[0]) {
 		rt->digital_thru_switch = ucontrol->value.integer.value[0];
 		usb6fire_control_streaming_update(rt);
Signature · 1 line
2.56.0
#2 lore raw

PATCH 2/3 ALSA: 6fire: add a playback channel map

In reply to Piotr Gliźniewicz

The driver registers no "Playback Channel Map" control, so a program that needs the speaker position of each playback channel has to assume an order; speaker-test, for example, assumes FL FR RL RR FC LFE for 6 channels. Channel n of the PCM goes to Line Out n, and the manual of the card assigns Line Out 1 to 6 to front left and right, centre and subwoofer, and rear left and right.

Add a playback channel map in the order of the manual,
FL FR FC LFE RL RR. A stream with 2 to 5 channels gets the first positions of the list. A 1-channel stream gets MONO, as in every 1-channel entry of a channel map in sound/; the card plays it on Line Out 1 only. The map has an entry for every channel count from 1 to 6, because reading the control fails with -EINVAL for a count without an entry.

Tested on a DMX 6Fire USB with kernel 7.2.10: during playback with 1, 3, 5 and 6 channels the control reads MONO, FL FR FC, FL FR FC LFE RL and FL FR FC LFE RL RR, and the "Front Center" test of speaker-test -c 6 played on Line Out 3.

Assisted-by LLM
---
 sound/usb/6fire/pcm.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
sound/usb/6fire/pcm.c +30 -0
index 0285d79ac..d99395229 100644
@@ -612,6 +612,28 @@ static void usb6fire_pcm_free(struct pcm_runtime *rt)
 	kfree(rt);
 }
 
+/*
+ * playback channel map: channel n goes to analog output n, which the
+ * manual of the card assigns to FL FR FC LFE RL RR for n = 1..6; a
+ * 1-channel stream is MONO, as in snd_pcm_std_chmaps, and plays on
+ * analog output 1 only
+ */
+static const struct snd_pcm_chmap_elem usb6fire_pcm_chmaps[] = {
+	{ .channels = 1, .map = { SNDRV_CHMAP_MONO } },
+	{ .channels = 2, .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
+	{ .channels = 3, .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
+				  SNDRV_CHMAP_FC } },
+	{ .channels = 4, .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
+				  SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
+	{ .channels = 5, .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
+				  SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
+				  SNDRV_CHMAP_RL } },
+	{ .channels = 6, .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
+				  SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
+				  SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
+	{ }
+};
+
 int usb6fire_pcm_init(struct sfire_chip *chip)
 {
 	int i;
@@ -665,6 +687,14 @@ int usb6fire_pcm_init(struct sfire_chip *chip)
 	rt->instance = pcm;
 
 	chip->pcm = rt;
+
+	/* on error, rt is freed with the card */
+	ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
+			usb6fire_pcm_chmaps, OUT_N_CHANNELS, 0, NULL);
+	if (ret < 0) {
+		dev_err(&chip->dev->dev, "cannot add channel map.\n");
+		return ret;
+	}
 	return 0;
 
  error:
Signature · 1 line
2.56.0
#3 lore raw

PATCH 3/3 ALSA: 6fire: add S/PDIF output

In reply to Piotr Gliźniewicz

The DMX 6Fire USB has a coaxial S/PDIF output, which the driver keeps disabled. Enable it as playback channels 7 and 8 of the existing PCM device, the layout chosen in the RFC thread linked below. The protocol was taken from a usbmon capture of the vendor's Windows driver and checked against a disassembly of that driver.

The packets of altsetting 3 (176.4 and 192 kHz) have room for the six analog channels only, so the output is available up to 96 kHz. hw_params rules allow more than 6 channels only up to 96 kHz, and prepare rejects more than 6 channels while the other stream runs at 176.4 or 192 kHz. A client that takes channels_max therefore gets 8 channels at up to 96 kHz instead of 6 channels at up to 192 kHz (the Pro Audio profile of PipeWire 1.6.9 opened the device with 8 channels); a client that asks for 6 channels still gets 192 kHz. A UCM profile can describe the channels and offer a 6-channel configuration with 192 kHz. The channel map marks channels 7 and 8 UNKNOWN; a program that ignores the map and assumes the 7.1 order sends the side pair to the S/PDIF output.

The card has one rate and one frame layout for both directions, set by the first prepare and kept until the last close, and the register bit that enables the transmitter is the bit that widens the playback frame from 6 to 8 slots. The driver therefore sets the bit at every rate up to 96 kHz, whichever direction starts first, so that a playback opened later can use 8 channels; the transmitter sends what is in slots 6 and 7, zeros while no playback runs. With "IEC958 Playback Switch" off (it is on by default), the driver sends zeros in the pair and omits the block start flag, and the receiver used for testing loses lock. Two versions in which the switch also cleared the bit were tried with PipeWire and a UCM profile and dropped. With the bit tied to the switch, the channel count depended on the switch and a change had to be refused while a stream ran, which broke profile changes. With the bit set by the switch or by an 8-channel playback, the frame width depended on which stream started first, and an 8-channel playback was refused while a capture ran with the switch off. With the bit tied to the rate, as in the vendor driver, the coaxial output carries a signal whenever the card streams at up to 96 kHz; a receiver that selects its input when it detects a signal was not tested. "Digital Thru Playback Route" has
priority: with it on, the output carries the coaxial input.

The channel status is consumer, 24 bit, with the sample rate and the channel numbers. "IEC958 Playback Default" sets the non-audio, copyright and emphasis bits, the category code and the generation bit; "IEC958 Playback Con Mask" lists these bits. With the non-audio bit set (AC3, DTS), the validity bit of every subframe is set as well, as the vendor driver does. A change of "IEC958 Playback Default" or of the switch takes effect at the next 192-frame block.

Tested on a DMX 6Fire USB at 44.1, 48 and 96 kHz with a second interface locked to the output: a tagged ramp on channels 7/8 arrived without a missing, repeated or changed sample for 4.7 s at each rate, and at 48 kHz a -20 dBFS tone arrived at -20.0 dBFS while a tone on channel 3 played on Line Out 3. With the switch off from the start, the second interface did not lock during an 8-channel, a 6-channel or a capture-only stream.

---
 sound/usb/6fire/control.c |  25 +++-
 sound/usb/6fire/pcm.c     | 308 +++++++++++++++++++++++++++++++++++---
 sound/usb/6fire/pcm.h     |   7 +-
 3 files changed, 316 insertions(+), 24 deletions(-)
sound/usb/6fire/control.c +21 -4
index 020e9676d..da5a2d7ac 100644
@@ -149,9 +149,14 @@ static int usb6fire_control_set_channels(
 	if (ret < 0)
 		return ret;
 
-	/* disable digital inputs and outputs */
-	/* TODO: use spdif_x to enable/disable digital channels */
-	ret = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00, 0x00);
+	/*
+	 * digital (S/PDIF) input and output: bit 0 adds four slots (4-7) to
+	 * the capture frame, which carry the subframes of the receiver; up to
+	 * two receiver frames arrive per card frame; bit 1 enables the
+	 * transmitter, which encodes the playback pair in slots 6/7 (pcm.c)
+	 */
+	ret = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00,
+			(spdif_in ? 0x01 : 0x00) | (spdif_out ? 0x02 : 0x00));
 	if (ret < 0)
 		return ret;
 
@@ -161,14 +166,26 @@ static int usb6fire_control_set_channels(
 static int usb6fire_control_streaming_update(struct control_runtime *rt)
 {
 	struct comm_runtime *comm_rt = rt->chip->comm;
+	int err = 0;
+	int ret;
 
 	if (comm_rt) {
+		/*
+		 * the digital pairs are enabled only while a stream runs; the
+		 * streaming register is written even if this write fails.
+		 * usb6fire_control_digital_thru_put() holds stream_mutex, so
+		 * this write cannot run after set_channels() and before
+		 * streaming starts in usb6fire_pcm_set_rate()
+		 */
+		if (!rt->usb_streaming)
+			err = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00, 0x00);
 		if (!rt->usb_streaming && rt->digital_thru_switch)
 			usb6fire_control_set_rate(rt,
 				DIGITAL_THRU_ONLY_SAMPLERATE);
-		return comm_rt->write16(comm_rt, 0x02, 0x00, 0x00,
+		ret = comm_rt->write16(comm_rt, 0x02, 0x00, 0x00,
 			(rt->usb_streaming ? 0x01 : 0x00) |
 			(rt->digital_thru_switch ? 0x08 : 0x00));
+		return err < 0 ? err : ret;
 	}
 	return -EINVAL;
 }
sound/usb/6fire/pcm.c +289 -19

453 lines not on this page · raw message

sound/usb/6fire/pcm.h +6 -1
index b586fe220..f1b867b9c 100644
@@ -53,7 +53,12 @@ struct pcm_runtime {
 	int in_packet_size;
 	int out_packet_size;
 	int in_n_analog; /* number of analog channels soundcard sends */
-	int out_n_analog; /* number of analog channels soundcard receives */
+	int out_n_slots; /* 32-bit slots per frame the soundcard receives */
+	bool spdif_switch; /* "IEC958 Playback Switch" */
+	bool spdif_on; /* copy of spdif_switch, read at the start of each block */
+	unsigned int spdif_frame; /* position in the channel status block */
+	u8 spdif_status[24]; /* IEC 60958 channel status of the S/PDIF output */
+	u32 spdif_bits; /* status bytes 0 and 1, "IEC958 Playback Default" */
 
 	struct mutex stream_mutex;
 	u8 stream_state; /* one of STREAM_XXX (pcm.c) */
Signature · 1 line
2.56.0
Applied
#4 lore raw
In reply to Piotr Gliźniewicz
Quoted 27 lines · Piotr Gliźniewicz

The DMX 6Fire USB has a coaxial S/PDIF output, which the driver keeps disabled. This series enables it as playback channels 7 and 8 of the existing PCM device, the layout chosen in the RFC thread [1].

Patch 1 takes stream_mutex in the "Digital Thru Playback Route" put. It fixes a race with prepare that exists in mainline, and patch 3 depends on the lock.

Patch 2 adds a playback channel map in the order of the manual of the card, FL FR FC LFE RL RR on Line Out 1 to 6.

Patch 3 adds the output with the "IEC958 Playback Default", "IEC958 Playback Con Mask" and "IEC958 Playback Switch" controls. With 8 channels the card runs at 96 kHz at most. The Pro Audio profile of PipeWire 1.6.9 opened the device with 8 channels, so with this series it gets 8 channels at up to 96 kHz instead of 6 channels at up to 192 kHz. A client that asks for 6 channels still gets 176.4 and 192 kHz.

A UCM profile for alsa-ucm-conf will describe the channels: the default variant with analog 5.1 and S/PDIF as two sinks at up to 96 kHz, and a "HiFi Analog" variant with analog 5.1 only at up to 192 kHz, which turns the switch off while in use. I tested a draft with PipeWire 1.6.9 and this series; I will send it after the kernel patches are accepted.

Quoted 6 lines

[1] https://lore.kernel.org/linux-sound/[email protected]/

Piotr Gliźniewicz (3):
ALSA: 6fire: take stream_mutex in the Digital Thru control
ALSA: 6fire: add a playback channel map
ALSA: 6fire: add S/PDIF output

Applied to for-next branch now. Thanks.

Takashi