Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Lee Volkov <voilthuman@gmail.com>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	Lee Volkov <voilthuman@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v2 2/2] ALSA: usb-audio: Reuse active sync endpoint format for implicit feedback
Date: Sat,  5 Dec 2026 22:29:28 +0300	[thread overview]
Message-ID: <20261205192928.27775-3-voilthuman@gmail.com> (raw)
In-Reply-To: <20261205192928.27775-1-voilthuman@gmail.com>

On devices where implicit feedback shares a capture endpoint with
multiple altsettings (such as Solid State Logic SSL 12, where 16/12ch
capture altsets support up to 96 kHz and 10ch Altset 3 supports up to
192 kHz), opening capture on a non-first altsetting (e.g. Altset 3 at
48 or 96 kHz) and then starting playback triggers a failure:

snd_usb_find_implicit_fb_sync_format() scans subs->fmt_list from the
first entry and picks Altset 1 because it also matches the rate and
channel-agnostic check, ignoring that the shared sync endpoint is
already open in Altset 3. snd_usb_endpoint_open() then rejects the
mismatch in endpoint_compatible() and fails playback with -EINVAL
("Incompatible EP setup for 0x81").

Pass the caller's current subs->sync_endpoint (ref_ep) so we look up the
live sync endpoint with snd_usb_get_endpoint() under chip->mutex and
only reuse ep->cur_audiofmt when it is in use by another stream
(ep != ref_ep || ep->opened > 1). This prevents a repeated hw_params()
call without hw_free() from pinning the sync endpoint to the caller's
own about-to-be-closed format.

Fixes: 5d15f1eb4560 ("ALSA: usb-audio: Choose audioformat of a counter-part substream")
Cc: stable@vger.kernel.org
Signed-off-by: Lee Volkov <voilthuman@gmail.com>
---
Changes in v2:
- Drop superfluous #include <linux/cleanup.h> (Takashi Iwai).
- Pass subs->sync_endpoint as ref_ep from snd_usb_hw_params() and check
  (ep != ref_ep || ep->opened > 1) before reusing ep->cur_audiofmt so a
  repeated hw_params() call without hw_free() does not reuse the caller's
  own about-to-be-closed sync format (Sashiko AI review).

 sound/usb/implicit.c | 25 ++++++++++++++++++++++++-
 sound/usb/implicit.h |  6 +++++-
 sound/usb/pcm.c      |  1 +
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/sound/usb/implicit.c b/sound/usb/implicit.c
index bd4d569a8..755df2b1d 100644
--- a/sound/usb/implicit.c
+++ b/sound/usb/implicit.c
@@ -16,6 +16,7 @@
 #include "card.h"
 #include "helper.h"
 #include "pcm.h"
+#include "endpoint.h"
 #include "implicit.h"
 
 enum {
@@ -459,10 +460,12 @@ snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
 				     const struct audioformat *target,
 				     const struct snd_pcm_hw_params *params,
 				     int stream,
+				     const struct snd_usb_endpoint *ref_ep,
 				     bool *fixed_rate)
 {
 	struct snd_usb_substream *subs;
-	const struct audioformat *fp, *sync_fmt = NULL;
+	struct snd_usb_endpoint *ep;
+	const struct audioformat *fp, *sync_fmt = NULL, *active_fmt = NULL;
 	int score, high_score;
 
 	/* Use the original audioformat as fallback for the shared altset */
@@ -475,6 +478,26 @@ snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
 	if (!subs)
 		goto end;
 
+	/*
+	 * If the shared sync endpoint is already open by another stream in a
+	 * compatible format, reuse that active audioformat instead of picking
+	 * another altsetting and failing endpoint_compatible() later.
+	 */
+	scoped_guard(mutex, &chip->mutex) {
+		ep = snd_usb_get_endpoint(chip, target->sync_ep);
+		if (ep && ep->opened && ep->cur_audiofmt &&
+		    (ep != ref_ep || ep->opened > 1) &&
+		    match_endpoint_audioformats(subs, ep->cur_audiofmt,
+						params_rate(params),
+						params_channels(params),
+						params_format(params)))
+			active_fmt = ep->cur_audiofmt;
+	}
+	if (active_fmt) {
+		sync_fmt = active_fmt;
+		goto end;
+	}
+
 	high_score = 0;
 	list_for_each_entry(fp, &subs->fmt_list, list) {
 		score = match_endpoint_audioformats(subs, fp,
diff --git a/sound/usb/implicit.h b/sound/usb/implicit.h
index 7f1577b6c..554f70a77 100644
--- a/sound/usb/implicit.h
+++ b/sound/usb/implicit.h
@@ -5,10 +5,14 @@
 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
 				    struct audioformat *fmt,
 				    struct usb_host_interface *alts);
+struct snd_usb_endpoint;
+
 const struct audioformat *
 snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
 				     const struct audioformat *target,
 				     const struct snd_pcm_hw_params *params,
-				     int stream, bool *fixed_rate);
+				     int stream,
+				     const struct snd_usb_endpoint *ref_ep,
+				     bool *fixed_rate);
 
 #endif /* __USBAUDIO_IMPLICIT_H */
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index 57a26492d..5f19cbf25 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -507,6 +507,7 @@ int snd_usb_hw_params(struct snd_usb_substream *subs,
 		sync_fmt = snd_usb_find_implicit_fb_sync_format(chip, fmt,
 								hw_params,
 								!subs->direction,
+								subs->sync_endpoint,
 								&sync_fixed_rate);
 		if (!sync_fmt) {
 			usb_audio_dbg(chip,
-- 
2.55.0


      parent reply	other threads:[~2026-12-05 19:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261205192928.27775-1-voilthuman@gmail.com>
2026-12-05 19:29 ` [PATCH v2 1/2] ALSA: usb-audio: Filter invalid capture rates on Solid State Logic SSL 12 Lee Volkov
2026-12-05 19:29 ` Lee Volkov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205192928.27775-3-voilthuman@gmail.com \
    --to=voilthuman@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=stable@vger.kernel.org \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox