All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: alsa-devel@alsa-project.org
Cc: Matwey Kornilov <matwey.kornilov@gmail.com>,
	Dylan Robinson <dylan_robinson@motu.com>,
	Keith Milner <kamilner@superlative.org>
Subject: [PATCH 03/41] ALSA: usb-audio: Check valid altsetting at parsing rates for UAC2/3
Date: Mon, 23 Nov 2020 09:53:09 +0100	[thread overview]
Message-ID: <20201123085347.19667-4-tiwai@suse.de> (raw)
In-Reply-To: <20201123085347.19667-1-tiwai@suse.de>

The current driver code assumes blindly that all found sample rates for
the same endpoint from the UAC2 and UAC3 descriptors can be used no
matter which altsetting, but actually this was wrong: some devices
accept only limited sample rates in each altsetting.  For determining
which altsetting supports which rate, we need to verify each sample rate
and check the validity via UAC2_AS_VAL_ALT_SETTINGS.  This control
reports back the available altsettings as a bitmap.

This patch implements the missing piece above, the verification and
reconstructs the sample rate tables based on the result.

An open question is how to deal with the altsettings that ended up
with no valid sample rates after verification.  At least, there is a
device that showed this problem although the sample rates did work in
the later usage (see bug link).  For now, we accept such an altset as
is, assuming that it's a firmware bug.

Reported-by: Dylan Robinson <dylan_robinson@motu.com>
Tested-by: Keith Milner <kamilner@superlative.org>
Tested-by: Dylan Robinson <dylan_robinson@motu.com>
BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1178203
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/usb/clock.c  | 106 ++++++++++++++++++++++++++++++-----------------------
 sound/usb/clock.h  |   4 ++
 sound/usb/format.c |  93 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 158 insertions(+), 45 deletions(-)

diff --git a/sound/usb/clock.c b/sound/usb/clock.c
index f3ca59005d91..f174230d07d5 100644
--- a/sound/usb/clock.c
+++ b/sound/usb/clock.c
@@ -560,16 +560,60 @@ static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
 	return le32_to_cpu(data);
 }
 
+/*
+ * Try to set the given sample rate:
+ *
+ * Return 0 if the clock source is read-only, the actual rate on success,
+ * or a negative error code.
+ *
+ * This function gets called from format.c to validate each sample rate, too.
+ * Hence no message is shown upon error
+ */
+int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
+				 const struct audioformat *fmt,
+				 int clock, int rate)
+{
+	bool writeable;
+	u32 bmControls;
+	__le32 data;
+	int err;
+
+	if (fmt->protocol == UAC_VERSION_3) {
+		struct uac3_clock_source_descriptor *cs_desc;
+
+		cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
+		bmControls = le32_to_cpu(cs_desc->bmControls);
+	} else {
+		struct uac_clock_source_descriptor *cs_desc;
+
+		cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
+		bmControls = cs_desc->bmControls;
+	}
+
+	writeable = uac_v2v3_control_is_writeable(bmControls,
+						  UAC2_CS_CONTROL_SAM_FREQ);
+	if (!writeable)
+		return 0;
+
+	data = cpu_to_le32(rate);
+	err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR,
+			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
+			      UAC2_CS_CONTROL_SAM_FREQ << 8,
+			      snd_usb_ctrl_intf(chip) | (clock << 8),
+			      &data, sizeof(data));
+	if (err < 0)
+		return err;
+
+	return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
+}
+
 static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
-			      struct usb_host_interface *alts,
-			      struct audioformat *fmt, int rate)
+				struct usb_host_interface *alts,
+				struct audioformat *fmt, int rate)
 {
 	struct usb_device *dev = chip->dev;
-	__le32 data;
-	int err, cur_rate, prev_rate;
+	int cur_rate, prev_rate;
 	int clock;
-	bool writeable;
-	u32 bmControls;
 
 	/* First, try to find a valid clock. This may trigger
 	 * automatic clock selection if the current clock is not
@@ -592,50 +636,22 @@ static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
 	if (prev_rate == rate)
 		goto validation;
 
-	if (fmt->protocol == UAC_VERSION_3) {
-		struct uac3_clock_source_descriptor *cs_desc;
-
-		cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
-		bmControls = le32_to_cpu(cs_desc->bmControls);
-	} else {
-		struct uac_clock_source_descriptor *cs_desc;
-
-		cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
-		bmControls = cs_desc->bmControls;
+	cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate);
+	if (cur_rate < 0) {
+		usb_audio_err(chip,
+			      "%d:%d: cannot set freq %d (v2/v3): err %d\n",
+			      iface, fmt->altsetting, rate, cur_rate);
+		return cur_rate;
 	}
 
-	writeable = uac_v2v3_control_is_writeable(bmControls,
-						  UAC2_CS_CONTROL_SAM_FREQ);
-	if (writeable) {
-		data = cpu_to_le32(rate);
-		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
-				      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
-				      UAC2_CS_CONTROL_SAM_FREQ << 8,
-				      snd_usb_ctrl_intf(chip) | (clock << 8),
-				      &data, sizeof(data));
-		if (err < 0) {
-			usb_audio_err(chip,
-				"%d:%d: cannot set freq %d (v2/v3): err %d\n",
-				iface, fmt->altsetting, rate, err);
-			return err;
-		}
-
-		cur_rate = get_sample_rate_v2v3(chip, iface,
-						fmt->altsetting, clock);
-	} else {
+	if (!cur_rate)
 		cur_rate = prev_rate;
-	}
 
 	if (cur_rate != rate) {
-		if (!writeable) {
-			usb_audio_warn(chip,
-				 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
-				 iface, fmt->altsetting, rate, cur_rate);
-			return -ENXIO;
-		}
-		usb_audio_dbg(chip,
-			"current rate %d is different from the runtime rate %d\n",
-			cur_rate, rate);
+		usb_audio_warn(chip,
+			       "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
+			       fmt->iface, fmt->altsetting, rate, cur_rate);
+		return -ENXIO;
 	}
 
 	/* Some devices doesn't respond to sample rate changes while the
diff --git a/sound/usb/clock.h b/sound/usb/clock.h
index 68df0fbe09d0..97597f5a3c18 100644
--- a/sound/usb/clock.h
+++ b/sound/usb/clock.h
@@ -9,4 +9,8 @@ int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
 int snd_usb_clock_find_source(struct snd_usb_audio *chip,
 			      struct audioformat *fmt, bool validate);
 
+int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
+				 const struct audioformat *fmt,
+				 int clock, int rate);
+
 #endif /* __USBAUDIO_CLOCK_H */
diff --git a/sound/usb/format.c b/sound/usb/format.c
index 3bfead393aa3..3348032daa16 100644
--- a/sound/usb/format.c
+++ b/sound/usb/format.c
@@ -417,6 +417,97 @@ static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
 	return -ENODEV;
 }
 
+/* check whether the given altsetting is supported for the already set rate */
+static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
+					int altsetting)
+{
+	struct usb_device *dev = chip->dev;
+	__le64 raw_data = 0;
+	u64 data;
+	int err;
+
+	/* we assume 64bit is enough for any altsettings */
+	if (snd_BUG_ON(altsetting >= 64 - 8))
+		return false;
+
+	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
+			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
+			      UAC2_AS_VAL_ALT_SETTINGS << 8,
+			      iface, &raw_data, sizeof(raw_data));
+	if (err < 0)
+		return false;
+
+	data = le64_to_cpu(raw_data);
+	/* first byte contains the bitmap size */
+	if ((data & 0xff) * 8 < altsetting)
+		return false;
+	if (data & (1ULL << (altsetting + 8)))
+		return true;
+
+	return false;
+}
+
+/*
+ * Validate each sample rate with the altsetting
+ * Rebuild the rate table if only partial values are valid
+ */
+static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
+					   struct audioformat *fp,
+					   int clock)
+{
+	struct usb_device *dev = chip->dev;
+	unsigned int *table;
+	unsigned int nr_rates;
+	unsigned int rate_min = 0x7fffffff;
+	unsigned int rate_max = 0;
+	unsigned int rates = 0;
+	int i, err;
+
+	table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
+	if (!table)
+		return -ENOMEM;
+
+	/* clear the interface altsetting at first */
+	usb_set_interface(dev, fp->iface, 0);
+
+	nr_rates = 0;
+	for (i = 0; i < fp->nr_rates; i++) {
+		err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
+						   fp->rate_table[i]);
+		if (err < 0)
+			continue;
+
+		if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting)) {
+			table[nr_rates++] = fp->rate_table[i];
+			if (rate_min > fp->rate_table[i])
+				rate_min = fp->rate_table[i];
+			if (rate_max < fp->rate_table[i])
+				rate_max = fp->rate_table[i];
+			rates |= snd_pcm_rate_to_rate_bit(fp->rate_table[i]);
+		}
+	}
+
+	if (!nr_rates) {
+		usb_audio_dbg(chip,
+			      "No valid sample rate available for %d:%d, assuming a firmware bug\n",
+			      fp->iface, fp->altsetting);
+		nr_rates = fp->nr_rates; /* continue as is */
+	}
+
+	if (fp->nr_rates == nr_rates) {
+		kfree(table);
+		return 0;
+	}
+
+	kfree(fp->rate_table);
+	fp->rate_table = table;
+	fp->nr_rates = nr_rates;
+	fp->rate_min = rate_min;
+	fp->rate_max = rate_max;
+	fp->rates = rates;
+	return 0;
+}
+
 /*
  * parse the format descriptor and stores the possible sample rates
  * on the audioformat table (audio class v2 and v3).
@@ -509,6 +600,8 @@ static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
 	 * allocated, so the rates will be stored */
 	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
 
+	ret = validate_sample_rate_table_v2v3(chip, fp, clock);
+
 err_free:
 	kfree(data);
 err:
-- 
2.16.4


  parent reply	other threads:[~2020-11-23  8:56 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23  8:53 [PATCH 00/41] USB audio refactoring for better implicit feedback support Takashi Iwai
2020-11-23  8:53 ` [PATCH 01/41] ALSA: usb-audio: Handle discrete rates properly in hw constraints Takashi Iwai
2020-11-23  8:53 ` [PATCH 02/41] ALSA: usb-audio: Don't call usb_set_interface() at trigger callback Takashi Iwai
2020-11-23  8:53 ` Takashi Iwai [this message]
2020-11-23  8:53 ` [PATCH 04/41] ALSA: usb-audio: Check implicit feedback EP generically for UAC2 Takashi Iwai
2020-11-23  8:53 ` [PATCH 05/41] ALSA: usb-audio: Add snd_usb_get_endpoint() helper Takashi Iwai
2020-11-23  8:53 ` [PATCH 06/41] ALSA: usb-audio: Set and clear sync EP link properly Takashi Iwai
2020-11-23  8:53 ` [PATCH 07/41] ALSA: usb-audio: Improve some debug prints Takashi Iwai
2020-11-23  8:53 ` [PATCH 08/41] ALSA: usb-audio: Track implicit fb sync endpoint in audioformat list Takashi Iwai
2020-11-23  8:53 ` [PATCH 09/41] ALSA: usb-audio: Move snd_usb_autoresume() call out of setup_hw_info() Takashi Iwai
2020-11-23  8:53 ` [PATCH 10/41] ALSA: usb-audio: Add hw constraint for implicit fb sync Takashi Iwai
2020-11-23  8:53 ` [PATCH 11/41] ALSA: usb-audio: Simplify hw_params rules Takashi Iwai
2020-11-23  8:53 ` [PATCH 12/41] ALSA: usb-audio: Drop debug.h Takashi Iwai
2020-11-23  8:53 ` [PATCH 13/41] ALSA: usb-audio: Avoid doubly initialization for implicit fb Takashi Iwai
2020-11-23  8:53 ` [PATCH 14/41] ALSA: usb-audio: Create endpoint objects at parsing phase Takashi Iwai
2021-01-03 17:09   ` [PATCH 14/41] ALSA: usb-audio: Create endpoint objects at parsing phase - Pioneer DJ DJM-250MK2 stopped working František Kučera
2021-01-03 17:19     ` Takashi Iwai
2021-01-03 18:15       ` František Kučera
2021-01-05  9:29         ` Takashi Iwai
2021-01-05 13:20           ` Takashi Iwai
2021-01-05 22:27             ` František Kučera
2021-01-06  9:03               ` Takashi Iwai
2021-01-06 19:01                 ` František Kučera
2021-01-07 13:30                   ` Takashi Iwai
2020-11-23  8:53 ` [PATCH 15/41] ALSA: usb-audio: Drop keep_interface flag again Takashi Iwai
2020-11-23  8:53 ` [PATCH 16/41] ALSA: usb-audio: Add snd_usb_get_host_interface() helper Takashi Iwai
2020-11-23  8:53 ` [PATCH 17/41] ALSA: usb-audio: Don't set altsetting before initializing sample rate Takashi Iwai
2020-11-23  8:53 ` [PATCH 18/41] ALSA: usb-audio: Pass snd_usb_audio object to quirk functions Takashi Iwai
2020-11-23  8:53 ` [PATCH 19/41] ALSA: usb-audio: Simplify snd_usb_init_sample_rate() arguments Takashi Iwai
2020-11-23  8:53 ` [PATCH 20/41] ALSA: usb-audio: Simplify snd_usb_init_pitch() arguments Takashi Iwai
2020-11-23  8:53 ` [PATCH 21/41] ALSA: usb-audio: Stop both endpoints properly at error Takashi Iwai
2020-11-23  8:53 ` [PATCH 22/41] ALSA: usb-audio: Set callbacks via snd_usb_endpoint_set_callback() Takashi Iwai
2020-11-23  8:53 ` [PATCH 23/41] ALSA: usb-audio: Always set up the parameters after resume Takashi Iwai
2020-11-23  8:53 ` [PATCH 24/41] ALSA: usb-audio: Fix EP matching for continuous rates Takashi Iwai
2020-11-23  8:53 ` [PATCH 25/41] ALSA: usb-audio: Refactor endpoint management Takashi Iwai
2020-11-23  8:53 ` [PATCH 26/41] ALSA: usb-audio: Fix possible stall of implicit fb packet ring-buffer Takashi Iwai
2020-11-23  8:53 ` [PATCH 27/41] ALSA: usb-audio: Constify audioformat pointer references Takashi Iwai
2020-11-23  8:53 ` [PATCH 28/41] ALSA: usb-audio: Use atomic_t for endpoint use_count Takashi Iwai
2020-11-23  8:53 ` [PATCH 29/41] ALSA: usb-audio: Refactoring endpoint URB deactivation Takashi Iwai
2020-11-23  8:53 ` [PATCH 30/41] ALSA: usb-audio: Drop unneeded snd_usb_substream fields Takashi Iwai
2020-11-23  8:53 ` [PATCH 31/41] ALSA: usb-audio: Unify the code for the next packet size calculation Takashi Iwai
2020-11-23  8:53 ` [PATCH 32/41] ALSA: usb-audio: Simplify rate_min/max and rates set up Takashi Iwai
2020-11-23  8:53 ` [PATCH 33/41] ALSA: usb-audio: Replace slave/master terms Takashi Iwai
2020-11-23  8:53 ` [PATCH 34/41] ALSA: usb-audio: Use unsigned char for iface and altsettings fields Takashi Iwai
2020-11-23  8:53 ` [PATCH 35/41] ALSA: usb-audio: Show sync endpoint information in proc outputs Takashi Iwai
2020-11-23  8:53 ` [PATCH 36/41] ALSA: usb-audio: Quirk for BOSS GT-001 Takashi Iwai
2020-11-23  8:53 ` [PATCH 37/41] ALSA: usb-audio: Factor out the implicit feedback quirk code Takashi Iwai
2020-11-23  8:53 ` [PATCH 38/41] ALSA: usb-audio: Add generic implicit fb parsing Takashi Iwai
2020-11-23  8:53 ` [PATCH 39/41] ALSA: usb-audio: Add implicit_fb module option Takashi Iwai
2020-11-23  9:46   ` Pavel Hofman
2020-11-23  8:53 ` [PATCH 40/41] ALSA: usb-audio: Fix quirks for other BOSS devices Takashi Iwai
2020-11-23  8:53 ` [PATCH 41/41] ALSA: usb-audio: Fix MOTU M-Series quirks Takashi Iwai

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=20201123085347.19667-4-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=dylan_robinson@motu.com \
    --cc=kamilner@superlative.org \
    --cc=matwey.kornilov@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.