Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
@ 2026-12-04 13:34 Daisuke Yamane
  2026-12-04 13:52 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Daisuke Yamane @ 2026-12-04 13:34 UTC (permalink / raw)
  To: Jaroslav Kysela, Takashi Iwai; +Cc: linux-sound, Daisuke Yamane

The Yamaha ZG02 (0499:1516) does not send plain audio frames on its
capture endpoint (EP 0x81). A usbmon capture shows that each
isochronous packet is laid out as follows:

   4 bytes  sequence number
   4 bytes  payload length in bytes (96 for 6 frames, 112 for 7)
  96 bytes  6 frames of 4 channels, S32_LE (112 bytes for 7 frames)
   4 bytes  the same sequence number again

A packet is 108 bytes for the usual 6 frames. The device sends 7
frames now and then as its asynchronous rate adjustment (1 of 24096
packets in a 3 second capture), giving a 124-byte packet. The sequence
number advances by one per packet with no gaps and keeps a constant
offset from the host's USB microframe number, so it appears to be a
counter locked to SOF.

The driver copies frames from the start of each packet. In every 125 us
packet, frame 0 of channels 1-2 therefore receives the sequence number
and the payload length, the remaining frames of channels 1-2 carry the
previous frame's samples, and the last frame of channels 1-2 is dropped.
Channels 3-4 happen to stay aligned. On channels 1-2 (the streaming mix
in the current layout) this is heard as constant distortion: a 1 kHz
sine played into the device and recorded back measures THD+N of about
-8 dB at every level from -36 to -6 dBFS.

Skip the 8-byte header with pkt_offset_adj for the capture substream.
Set it from a new snd_usb_set_pkt_offset_quirk(), called on every
prepare. snd_usb_set_format_quirk() is not enough: it runs only when
the data endpoint is newly prepared, and as the ZG02 uses implicit
feedback, a running playback stream may already have prepared EP 0x81
as its sync endpoint when capture starts.
The 4-byte trailer is not copied because the frame count is derived
from actual_length / stride (108 / 16 = 6, 124 / 16 = 7). The
implicit feedback calculation also uses actual_length / stride, so it
is unchanged.

With the header skipped, each device frame is ordered microphone pair
first, streaming mix second. Capture channels 1-2 become the
microphone and channels 3-4 the streaming mix, so the two pairs swap
compared to the layout described in the commit that added the quirk.

Re-decoding the captured packets with the 8-byte offset gives a signal
whose second difference is uniform across all six frame positions of a
packet; the current decoding shows a discontinuity about 70-140 times
larger at frame positions 0-2.

Tested on a ZG02 on v7.4-rc5: a 1 kHz sine looped back through the
streaming mix measures THD+N of -7.8 dB at every level without this
patch. With it, the two channels measure -75 and -78 dB at -36 dBFS
and -105 and -108 dB at -6 dBFS, both when capture is started first
and when playback is started first.

The usbmon analysis, the change and this description were prepared
with the help of an AI coding assistant; see the notes below.

Fixes: e7c66b3e249e ("ALSA: usb-audio: Add quirk for Yamaha ZG02")
Signed-off-by: Daisuke Yamane <yamane07ynct@gmail.com>
---
Use of AI tools:
- Claude Opus 5.5 (Anthropic), used through Claude Code in a VM, under
  my direction. It decoded the usbmon captures and found the header
  layout, wrote the change and the changelog, wrote and ran the test
  scripts behind the notes below (loopback THD+N and microphone
  recordings) in a VM with the device passed through, and wrote the
  test script for other ZG02 owners linked at the end. I connected and
  operated the device, spoke into the microphone for the recordings,
  and reviewed the change, the description and the test results.

Testing notes:
- v7.4-rc5 (19cc08c1a6a6) with this patch, ZG02 passed through to a
  QEMU/Incus VM, ALSA hw device. The unpatched module was built from
  the same tree for comparison. The quirk entry from e7c66b3e249e binds
  the device without quirk_alias.
- Each order (capture first, playback first) was run after rebinding
  the driver, so that no earlier capture had set up the substream.
  Three runs of five levels each.
- Now and then a single recording had a gap of a few ms in the played
  tone (THD+N -14 to -28 dB) with no header pattern; it was repeated.
  This is a playback glitch, not related to capture.
- Microphone pair: on the same v7.4-rc5 kernel, a microphone on the
  ZG02 was recorded with arecord (hw device, 4 channels, 40 s of speech
  and silence). The speech is on channels 1-2 (identical, as the input
  is mono) with peaks up to -22 dBFS, no clipping and no sign of the
  per-packet pattern that the header causes on channels 1-2 without
  the fix. The same change backported to Ubuntu's 6.8 kernel gave the
  same result in five recordings.
- A test script for ZG02 owners who want to check this patch (loopback
  through the streaming mix in both orders, optional microphone check):
  https://github.com/Ponsuke07/zg02-capture-test

 sound/usb/pcm.c    |  1 +
 sound/usb/quirks.c | 17 +++++++++++++++++
 sound/usb/quirks.h |  1 +
 3 files changed, 19 insertions(+)

diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index 57a2649..1d86fe1 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -697,6 +697,7 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
 		return ret;
 	else if (ret > 0)
 		snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
+	snd_usb_set_pkt_offset_quirk(subs);
 	ret = 0;
 
 	/* reset the pointer */
diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
index bd00db8..cb3b58c 100644
--- a/sound/usb/quirks.c
+++ b/sound/usb/quirks.c
@@ -2138,6 +2138,23 @@ void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
 	}
 }
 
+/*
+ * Called on every prepare. snd_usb_set_format_quirk() runs only when the
+ * data endpoint is newly prepared, which is not the case for a capture
+ * endpoint that a running playback stream already prepared as its
+ * implicit feedback source.
+ */
+void snd_usb_set_pkt_offset_quirk(struct snd_usb_substream *subs)
+{
+	switch (subs->stream->chip->usb_id) {
+	case USB_ID(0x0499, 0x1516): /* Yamaha ZG02 */
+		/* capture packets: [u32 seq][u32 len] frames [u32 seq] */
+		if (subs->direction == SNDRV_PCM_STREAM_CAPTURE)
+			subs->pkt_offset_adj = 8;
+		break;
+	}
+}
+
 int snd_usb_select_mode_quirk(struct snd_usb_audio *chip,
 			      const struct audioformat *fmt)
 {
diff --git a/sound/usb/quirks.h b/sound/usb/quirks.h
index 0a723c2..38b3d13 100644
--- a/sound/usb/quirks.h
+++ b/sound/usb/quirks.h
@@ -27,6 +27,7 @@ int snd_usb_apply_boot_quirk_once(struct usb_device *dev,
 
 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
 			      const struct audioformat *fmt);
+void snd_usb_set_pkt_offset_quirk(struct snd_usb_substream *subs);
 
 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip,
 				 const struct audioformat *fp);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04 16:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 13:34 [PATCH] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream Daisuke Yamane
2026-12-04 13:52 ` sashiko-bot
2026-12-04 14:04   ` Daisuke Yamane
2026-12-04 16:08     ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox