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

* Re: [PATCH] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
  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
  0 siblings, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-12-04 13:52 UTC (permalink / raw)
  To: Daisuke Yamane; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: usb-audio: OOB read in retire_capture_urb() with
  pkt_offset_adj
--

commit 4f42e1ac4fec821d619625b40ff2e9b8d0f517f2
Author: Daisuke Yamane <yamane07ynct@gmail.com>

ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream

This commit adds snd_usb_set_pkt_offset_quirk() and calls it on every
snd_usb_pcm_prepare() to set subs->pkt_offset_adj = 8 for Yamaha ZG02
capture streams, skipping the 8-byte header at the start of each
isochronous packet.

> diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
> index 0694c02558fea..ab149b22778fb 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;
> +	}
> +}

[Severity: Medium]
Can setting subs->pkt_offset_adj = 8 cause an out-of-bounds read past
urb->transfer_buffer in retire_capture_urb()?

retire_capture_urb() advances cp by subs->pkt_offset_adj, but unlike
subs->stream_offset_adj, it never checks that bytes >= subs->pkt_offset_adj
or subtracts subs->pkt_offset_adj from bytes before computing frames and
copying into runtime->dma_area:

sound/usb/pcm.c:retire_capture_urb() {
	...
	cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
	...
	bytes = urb->iso_frame_desc[i].actual_length;
	if (unlikely(bytes > urb->iso_frame_desc[i].length)) {
		...
		continue;
	}
	...
	frames = bytes / stride;
	if (!subs->txfr_quirk)
		bytes = frames * stride;
	...
	memcpy(runtime->dma_area + oldptr, cp, bytes);
}

data_ep_set_params() allocates urb->transfer_buffer to maxsize * u->packets
with no trailing padding, and prepare_inbound_urb() packs the packets
back-to-back.

On the last packet, a full-length transfer where (actual_length / stride) *
stride + 8 > length (such as 144 bytes with stride 16) causes memcpy() to
read up to 8 bytes past urb->transfer_buffer, which can fault or copy
adjacent memory into runtime->dma_area.

Dropping packets with bytes <= subs->pkt_offset_adj and subtracting
subs->pkt_offset_adj from bytes before computing frames in
retire_capture_urb() would prevent the over-read while keeping ZG02 frame
counts unchanged ((108 - 8) / 16 = 6, (124 - 8) / 16 = 7).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204133429.247377-1-yamane07ynct@gmail.com?part=1

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

* Re: [PATCH] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
  2026-12-04 13:52 ` sashiko-bot
@ 2026-12-04 14:04   ` Daisuke Yamane
  2026-12-04 16:08     ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Daisuke Yamane @ 2026-12-04 14:04 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-sound, Takashi Iwai, Jaroslav Kysela

2026年12月4日(金) 22:52 <sashiko-bot@kernel.org>:
>
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] ALSA: usb-audio: OOB read in retire_capture_urb() with
>   pkt_offset_adj
> --
>
> commit 4f42e1ac4fec821d619625b40ff2e9b8d0f517f2
> Author: Daisuke Yamane <yamane07ynct@gmail.com>
>
> ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
>
> This commit adds snd_usb_set_pkt_offset_quirk() and calls it on every
> snd_usb_pcm_prepare() to set subs->pkt_offset_adj = 8 for Yamaha ZG02
> capture streams, skipping the 8-byte header at the start of each
> isochronous packet.
>
> > diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
> > index 0694c02558fea..ab149b22778fb 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;
> > +     }
> > +}
>
> [Severity: Medium]
> Can setting subs->pkt_offset_adj = 8 cause an out-of-bounds read past
> urb->transfer_buffer in retire_capture_urb()?
>
> retire_capture_urb() advances cp by subs->pkt_offset_adj, but unlike
> subs->stream_offset_adj, it never checks that bytes >= subs->pkt_offset_adj
> or subtracts subs->pkt_offset_adj from bytes before computing frames and
> copying into runtime->dma_area:
>
> sound/usb/pcm.c:retire_capture_urb() {
>         ...
>         cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
>         ...
>         bytes = urb->iso_frame_desc[i].actual_length;
>         if (unlikely(bytes > urb->iso_frame_desc[i].length)) {
>                 ...
>                 continue;
>         }
>         ...
>         frames = bytes / stride;
>         if (!subs->txfr_quirk)
>                 bytes = frames * stride;
>         ...
>         memcpy(runtime->dma_area + oldptr, cp, bytes);
> }
>
> data_ep_set_params() allocates urb->transfer_buffer to maxsize * u->packets
> with no trailing padding, and prepare_inbound_urb() packs the packets
> back-to-back.
>
> On the last packet, a full-length transfer where (actual_length / stride) *
> stride + 8 > length (such as 144 bytes with stride 16) causes memcpy() to
> read up to 8 bytes past urb->transfer_buffer, which can fault or copy
> adjacent memory into runtime->dma_area.

This cannot happen with the ZG02 itself. Its capture endpoint (EP 0x81)
has wMaxPacketSize 124, which is below the 144 bytes that
data_ep_set_params() computes from freqmax, so each packet slot is 124
bytes. The largest copy is then 124 / 16 * 16 = 112 bytes starting at
offset 8, ending at byte 120 of the slot, and longer packets are
dropped by the actual_length > length check.

A device that claims 0499:1516 with a larger wMaxPacketSize could
trigger it, and the same applies in principle to the existing
pkt_offset_adj quirk for the E-mu devices. If you prefer, I can send a
v2 that subtracts pkt_offset_adj from bytes in retire_capture_urb() and
drops packets that are not longer than the offset; the ZG02 frame
counts stay the same ((108 - 8) / 16 = 6, (124 - 8) / 16 = 7).





>
> Dropping packets with bytes <= subs->pkt_offset_adj and subtracting
> subs->pkt_offset_adj from bytes before computing frames in
> retire_capture_urb() would prevent the over-read while keeping ZG02 frame
> counts unchanged ((108 - 8) / 16 = 6, (124 - 8) / 16 = 7).
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261204133429.247377-1-yamane07ynct@gmail.com?part=1

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

* Re: [PATCH] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
  2026-12-04 14:04   ` Daisuke Yamane
@ 2026-12-04 16:08     ` Takashi Iwai
  0 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2026-12-04 16:08 UTC (permalink / raw)
  To: Daisuke Yamane
  Cc: sashiko-reviews, linux-sound, Takashi Iwai, Jaroslav Kysela

On Fri, 04 Dec 2026 15:04:31 +0100,
Daisuke Yamane wrote:
> 
> 2026年12月4日(金) 22:52 <sashiko-bot@kernel.org>:
> >
> > Thank you for your contribution! Sashiko AI review found 1 potential
> > issue to consider:
> > - [Medium] ALSA: usb-audio: OOB read in retire_capture_urb() with
> >   pkt_offset_adj
> > --
> >
> > commit 4f42e1ac4fec821d619625b40ff2e9b8d0f517f2
> > Author: Daisuke Yamane <yamane07ynct@gmail.com>
> >
> > ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream
> >
> > This commit adds snd_usb_set_pkt_offset_quirk() and calls it on every
> > snd_usb_pcm_prepare() to set subs->pkt_offset_adj = 8 for Yamaha ZG02
> > capture streams, skipping the 8-byte header at the start of each
> > isochronous packet.
> >
> > > diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
> > > index 0694c02558fea..ab149b22778fb 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;
> > > +     }
> > > +}
> >
> > [Severity: Medium]
> > Can setting subs->pkt_offset_adj = 8 cause an out-of-bounds read past
> > urb->transfer_buffer in retire_capture_urb()?
> >
> > retire_capture_urb() advances cp by subs->pkt_offset_adj, but unlike
> > subs->stream_offset_adj, it never checks that bytes >= subs->pkt_offset_adj
> > or subtracts subs->pkt_offset_adj from bytes before computing frames and
> > copying into runtime->dma_area:
> >
> > sound/usb/pcm.c:retire_capture_urb() {
> >         ...
> >         cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
> >         ...
> >         bytes = urb->iso_frame_desc[i].actual_length;
> >         if (unlikely(bytes > urb->iso_frame_desc[i].length)) {
> >                 ...
> >                 continue;
> >         }
> >         ...
> >         frames = bytes / stride;
> >         if (!subs->txfr_quirk)
> >                 bytes = frames * stride;
> >         ...
> >         memcpy(runtime->dma_area + oldptr, cp, bytes);
> > }
> >
> > data_ep_set_params() allocates urb->transfer_buffer to maxsize * u->packets
> > with no trailing padding, and prepare_inbound_urb() packs the packets
> > back-to-back.
> >
> > On the last packet, a full-length transfer where (actual_length / stride) *
> > stride + 8 > length (such as 144 bytes with stride 16) causes memcpy() to
> > read up to 8 bytes past urb->transfer_buffer, which can fault or copy
> > adjacent memory into runtime->dma_area.
> 
> This cannot happen with the ZG02 itself. Its capture endpoint (EP 0x81)
> has wMaxPacketSize 124, which is below the 144 bytes that
> data_ep_set_params() computes from freqmax, so each packet slot is 124
> bytes. The largest copy is then 124 / 16 * 16 = 112 bytes starting at
> offset 8, ending at byte 120 of the slot, and longer packets are
> dropped by the actual_length > length check.

In the case of a USB device, a malformed device descriptor can be
passed easily.  That is, you cannot assume such a safety just
depending on the device ID in general, unfortunately.


thanks,

Takashi

^ permalink raw reply	[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