From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796540241; b=uKOeobmuI83lQA0XZelMVX/eHRXQ6hkCMfVgDU5wlmB06CpYKECvZZyqooH2DDYVoVw6 VuUGCPZ9Atv1GJmesGscH1Rwy+LSnuVNDvPEWHiWii9pCIZtBE2PATVDt/TfP4+tl/zO6 EJ5MhCN042z9c43IeeVHzJtfqoHSdQBVHqOZrVVGmeZHNDHDcAw3St02fAS9TaaJbYfmm PcSUQ88ELjO38PzyjSNNMnk76ToHa45zNq3GsVLscsmjgOPK4JeQQe+bLUMdFim2asd1W fHb4raq4YNl39JOweH//r3JDClvXVvLF1fAMptZ2dAzX1CeN+64PMW+7sKXGoWO838w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796540241; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=gkWoZqO0R8NKK+St2DzYoXSMCeX7UGvZuxaA4xik+kM=; b=xtLYhFIvI4EkTotCyNvWGoZNW/mQK29L9Ua5/2ejTFwVcg1BcSW1frIjZ3LL09d7dgAe MRMKqQm0Q70wxwb8GVSqtqymNiKAi5azNgrC112gOPeI6d3JrK3/qrWnBxm8Jsei3XV0t Red8pqKvS0xxsay0V2fOh3geey6uXMoEMk5DknbWx1EM8kYbcEOyqLiYF7j3jk2ELg3gE lTaX1CpLtK8GbsrnODaU8rDZNeCvDx2ssB/CkrubC7S2VBHhndtAHCNW8ztkbLg7jNNyZ r3/QtYAVEqQanIWnveFYMipWocTefn2Cc6NAnt99elua/lOyrMtqrTwlpSzEQiwzdVA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cPb7APsa; arc=none smtp.remote-ip=74.125.82.170 Received: from mail-dy1-f170.google.com (mail-dy1-f170.google.com [74.125.82.170]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C536606A31F for ; Sun, 6 Dec 2026 06:57:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9C536606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cPb7APsa Received: by mail-dy1-f170.google.com with SMTP id 5a478bee46e88-3866a784546so3189703eec.0 for ; Sat, 05 Dec 2026 22:57:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796540240; x=1797145040; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=gkWoZqO0R8NKK+St2DzYoXSMCeX7UGvZuxaA4xik+kM=; b=cPb7APsadBp5Y1SwrkAjE1vR4W0uLoQuPpgag3h3T35BAkD3rZQCerKT7y/ZnmJVdg zl81G1ycPFD6aTDH2SEywgLtXp/kco4MP/lY3WNMZajxaoKj/6LSlTKbQ7kQGR95Hexa cAy9/CfUrUSmwVJ9kuaPo3zoFJfWODB9KiltcQFFAW+uNQmmZuZRXcdUy11FN/PqKhb0 8+WR/YsHVUfG+9i8u7dku7Sv76/mUaP/1W97hXGOL7zJiE6mLRmAwGKxFML96xjfH01K Nf+gRLehhrA2dpQ7l83yjgX8H5qiup+58d3vfF4fIvs1Lfc6/MntQEDXjakKc2G9c0hB DFzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796540240; x=1797145040; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=gkWoZqO0R8NKK+St2DzYoXSMCeX7UGvZuxaA4xik+kM=; b=sQ9yPfuRXJ/hBg68NwE8S6JkaXNyYiG/Md+QcETag3zHcWQ2/oS8pI0/0Yu4pvlbxW gWtxMtMi03rWoE4uQPX//yIDbHq8uwzCDU3Q9X1j+XAX9XGdgqbbTfatCBqxuoE9SoLM WaG2CIdgULe8sPFsYXYtBVffOxjQWv3M/hmx4+vR7+8StSpLZS15WJhomYOoBgyILsS7 r7h0iegOAlYEWKTHYiBecZSB6cr2ShK4vjCMOSaGQGoY85a8n0ah80qH7/jDInxvb3hX ckNIClPAOwPOj/YxLjQJZP9avp4gicDaGQZ74hOjlItlVzwPF/v9rDoK66B5H2N0/aYP tz/w== X-Gm-Message-State: APuU4Z8uANEvC4BDPGX9pf/EPIOe3l3igX51LSk5ttEWhxJqoVf1VOom HtyGf5h5vtcPLtVQfZonoUeFoM/sSbVhtmUjSgkzSSmYcZG3fLkm1dI8DC+HPd8R X-Gm-Gg: AZFdTZcUJ76alOvRdDmbEdL2nj7KM2IgFugIbhom/QI1pDhFOa0+TKIhGhHZpvoNY8p GPIw6J/2gKLiJFrWOwyZNi7Zh19/x26ZfWt7HRxQlmEbRT/GHhasdBVYLuQXzYsJYa+jq3lWB2P HXmxarVm4fj4KFyH3XVWJueQRqG3FNyZdw/Pvb8FySgu4yKnIu10msZhNvf0HS8MXcoW3KJBFdW XXEmufruSQmRptM6e7LuHAu+9Ud5Hj9mdobdJuIa+prHa8KPs/qYs9QU2/19btcy4TpHkE1ONn/ T1iPsc3AeW969y0CVLLMoCiKTcaRVJDDSNvo6cMsp0s8OHKG+2nD5jS3t64e0wTHt2jgMRYKi0g 4ayFhQKMHzrJJD40pZX3erhxgtpD/b8pI9tMjVWS3OLRmMpx/MGYwPncSCKsO3o9EuZcZNzZ2Xx Dzg/1dykh1BO8PoitgBdZ1F9RYAhIABfBoSDxkTbTXJ1xur9fnogDvxhDc0ZNW2wRDqx2f97UIM QvVThYtf9EpEwMYe1dtmTNb079Ucw74WjRXKB4CgOY4T3ChlW1NDchgXUSOFL3yRkebygwAXN1Y LHuBc7wlpydsgeacf7SxXmvelpsYyBmKYFxvbxPa4WnpQvJ+IYAaDA== X-Received: by 2002:a05:7301:fe94:b0:383:600a:4577 with SMTP id 5a478bee46e88-387521129cdmr7272942eec.23.1796540239587; Sat, 05 Dec 2026 22:57:19 -0800 (PST) Received: from ico.. (softbank060117076056.bbtec.net. [60.117.76.56]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e5626sm24573790eec.17.2026.12.05.22.57.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 22:57:17 -0800 (PST) From: Daisuke Yamane To: Takashi Iwai , Jaroslav Kysela Cc: linux-sound@vger.kernel.org, Daisuke Yamane Subject: [PATCH v2 2/2] ALSA: usb-audio: Skip packet header in Yamaha ZG02 capture stream Date: Sun, 6 Dec 2026 15:56:52 +0900 Message-ID: <20261206065652.51981-3-yamane07ynct@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261206065652.51981-1-yamane07ynct@gmail.com> References: <20261206065652.51981-1-yamane07ynct@gmail.com> X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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