* [PATCH v2 1/2] ALSA: usb-audio: Don't read past the packet with pkt_offset_adj
2026-12-06 6:56 [PATCH v2 0/2] ALSA: usb-audio: Fix Yamaha ZG02 capture Daisuke Yamane
@ 2026-12-06 6:56 ` Daisuke Yamane
2026-12-06 6:56 ` [PATCH v2 2/2] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream Daisuke Yamane
1 sibling, 0 replies; 3+ messages in thread
From: Daisuke Yamane @ 2026-12-06 6:56 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, Daisuke Yamane, stable
retire_capture_urb() starts the copy pkt_offset_adj bytes into each
packet but derives the number of frames from the full actual_length,
so the copy ends pkt_offset_adj bytes after the received data. The
packets of a URB lie back to back in transfer_buffer, so for the last
packet of a URB that fills its slot, memcpy() reads past the end of
the buffer. A device can send such a packet regardless of the layout
that its quirk expects.
Subtract pkt_offset_adj from the byte count, as is already done for
stream_offset_adj. A packet shorter than the offset gives no frames.
For a packet of the offset followed by whole frames, the frame count
is unchanged when a frame is larger than the offset; otherwise
offset / frame size frames (rounded down) past the received data were
copied, which this drops.
This was found by the Sashiko AI review of a patch that adds a second
user of pkt_offset_adj; the change and this description were prepared
with the help of an AI coding assistant.
Fixes: 1539d4f82ad5 ("ALSA: usb: Add quirk for 192KHz recording on E-Mu devices")
Cc: stable@vger.kernel.org
Signed-off-by: Daisuke Yamane <yamane07ynct@gmail.com>
---
Tested only on the Yamaha ZG02 together with patch 2/2 (see there);
not tested on E-Mu hardware.
sound/usb/pcm.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index 57a2649..d88ef29 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -1343,6 +1343,8 @@ static void retire_capture_urb(struct snd_usb_substream *subs,
i, bytes, urb->iso_frame_desc[i].length);
continue;
}
+ /* the bytes skipped by pkt_offset_adj are not frames */
+ bytes -= min(subs->pkt_offset_adj, bytes);
if (subs->stream_offset_adj > 0) {
unsigned int adj = min(subs->stream_offset_adj, bytes);
cp += adj;
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH v2 2/2] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
2026-12-06 6:56 [PATCH v2 0/2] ALSA: usb-audio: Fix Yamaha ZG02 capture Daisuke Yamane
2026-12-06 6:56 ` [PATCH v2 1/2] ALSA: usb-audio: Don't read past the packet with pkt_offset_adj Daisuke Yamane
@ 2026-12-06 6:56 ` Daisuke Yamane
1 sibling, 0 replies; 3+ messages in thread
From: Daisuke Yamane @ 2026-12-06 6:56 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +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, with the previous patch, the
frame count is (actual_length - 8) / stride (100 / 16 = 6,
116 / 16 = 7). The implicit feedback calculation uses
actual_length / stride, which gives the same counts, 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>
---
Changes in v2:
- New patch 1/2: retire_capture_urb() now takes pkt_offset_adj off the
byte count, so a malformed packet cannot make the copy run past the
URB buffer (Sashiko review; Takashi: a device descriptor can be
forged, so the ZG02's wMaxPacketSize is no guarantee).
- The frame count in the description is now (actual_length - 8) / stride.
- Retested the series on v7.4-rc5 in both orders, three runs each:
channels 1-2 silent, channels 3-4 clean apart from playback gaps as
noted below, no header pattern in any recording. Patch 1/2 does not
change the captured data for the 16-byte frames used here, so the
results in the description still apply.
- v1: https://lore.kernel.org/all/20261204133429.247377-1-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 d88ef29..4d35c3e 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] 3+ messages in thread