Skip to content

Commit 93db51d

Browse files
committed
ALSA: usb-audio: Check valid altsetting at parsing rates for UAC2/3
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 <[email protected]> Tested-by: Keith Milner <[email protected]> Tested-by: Dylan Robinson <[email protected]> BugLink: https://bugzilla.suse.com/show_bug.cgi?id=1178203 Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 4974b79 commit 93db51d

File tree

3 files changed

+158
-45
lines changed

3 files changed

+158
-45
lines changed

sound/usb/clock.c

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -560,16 +560,60 @@ static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
560560
return le32_to_cpu(data);
561561
}
562562

563+
/*
564+
* Try to set the given sample rate:
565+
*
566+
* Return 0 if the clock source is read-only, the actual rate on success,
567+
* or a negative error code.
568+
*
569+
* This function gets called from format.c to validate each sample rate, too.
570+
* Hence no message is shown upon error
571+
*/
572+
int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
573+
const struct audioformat *fmt,
574+
int clock, int rate)
575+
{
576+
bool writeable;
577+
u32 bmControls;
578+
__le32 data;
579+
int err;
580+
581+
if (fmt->protocol == UAC_VERSION_3) {
582+
struct uac3_clock_source_descriptor *cs_desc;
583+
584+
cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
585+
bmControls = le32_to_cpu(cs_desc->bmControls);
586+
} else {
587+
struct uac_clock_source_descriptor *cs_desc;
588+
589+
cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
590+
bmControls = cs_desc->bmControls;
591+
}
592+
593+
writeable = uac_v2v3_control_is_writeable(bmControls,
594+
UAC2_CS_CONTROL_SAM_FREQ);
595+
if (!writeable)
596+
return 0;
597+
598+
data = cpu_to_le32(rate);
599+
err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR,
600+
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
601+
UAC2_CS_CONTROL_SAM_FREQ << 8,
602+
snd_usb_ctrl_intf(chip) | (clock << 8),
603+
&data, sizeof(data));
604+
if (err < 0)
605+
return err;
606+
607+
return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
608+
}
609+
563610
static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
564-
struct usb_host_interface *alts,
565-
struct audioformat *fmt, int rate)
611+
struct usb_host_interface *alts,
612+
struct audioformat *fmt, int rate)
566613
{
567614
struct usb_device *dev = chip->dev;
568-
__le32 data;
569-
int err, cur_rate, prev_rate;
615+
int cur_rate, prev_rate;
570616
int clock;
571-
bool writeable;
572-
u32 bmControls;
573617

574618
/* First, try to find a valid clock. This may trigger
575619
* 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,
592636
if (prev_rate == rate)
593637
goto validation;
594638

595-
if (fmt->protocol == UAC_VERSION_3) {
596-
struct uac3_clock_source_descriptor *cs_desc;
597-
598-
cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
599-
bmControls = le32_to_cpu(cs_desc->bmControls);
600-
} else {
601-
struct uac_clock_source_descriptor *cs_desc;
602-
603-
cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
604-
bmControls = cs_desc->bmControls;
639+
cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate);
640+
if (cur_rate < 0) {
641+
usb_audio_err(chip,
642+
"%d:%d: cannot set freq %d (v2/v3): err %d\n",
643+
iface, fmt->altsetting, rate, cur_rate);
644+
return cur_rate;
605645
}
606646

607-
writeable = uac_v2v3_control_is_writeable(bmControls,
608-
UAC2_CS_CONTROL_SAM_FREQ);
609-
if (writeable) {
610-
data = cpu_to_le32(rate);
611-
err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
612-
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
613-
UAC2_CS_CONTROL_SAM_FREQ << 8,
614-
snd_usb_ctrl_intf(chip) | (clock << 8),
615-
&data, sizeof(data));
616-
if (err < 0) {
617-
usb_audio_err(chip,
618-
"%d:%d: cannot set freq %d (v2/v3): err %d\n",
619-
iface, fmt->altsetting, rate, err);
620-
return err;
621-
}
622-
623-
cur_rate = get_sample_rate_v2v3(chip, iface,
624-
fmt->altsetting, clock);
625-
} else {
647+
if (!cur_rate)
626648
cur_rate = prev_rate;
627-
}
628649

629650
if (cur_rate != rate) {
630-
if (!writeable) {
631-
usb_audio_warn(chip,
632-
"%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
633-
iface, fmt->altsetting, rate, cur_rate);
634-
return -ENXIO;
635-
}
636-
usb_audio_dbg(chip,
637-
"current rate %d is different from the runtime rate %d\n",
638-
cur_rate, rate);
651+
usb_audio_warn(chip,
652+
"%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
653+
fmt->iface, fmt->altsetting, rate, cur_rate);
654+
return -ENXIO;
639655
}
640656

641657
/* Some devices doesn't respond to sample rate changes while the

sound/usb/clock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
99
int snd_usb_clock_find_source(struct snd_usb_audio *chip,
1010
struct audioformat *fmt, bool validate);
1111

12+
int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
13+
const struct audioformat *fmt,
14+
int clock, int rate);
15+
1216
#endif /* __USBAUDIO_CLOCK_H */

sound/usb/format.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,97 @@ static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
417417
return -ENODEV;
418418
}
419419

420+
/* check whether the given altsetting is supported for the already set rate */
421+
static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
422+
int altsetting)
423+
{
424+
struct usb_device *dev = chip->dev;
425+
__le64 raw_data = 0;
426+
u64 data;
427+
int err;
428+
429+
/* we assume 64bit is enough for any altsettings */
430+
if (snd_BUG_ON(altsetting >= 64 - 8))
431+
return false;
432+
433+
err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
434+
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
435+
UAC2_AS_VAL_ALT_SETTINGS << 8,
436+
iface, &raw_data, sizeof(raw_data));
437+
if (err < 0)
438+
return false;
439+
440+
data = le64_to_cpu(raw_data);
441+
/* first byte contains the bitmap size */
442+
if ((data & 0xff) * 8 < altsetting)
443+
return false;
444+
if (data & (1ULL << (altsetting + 8)))
445+
return true;
446+
447+
return false;
448+
}
449+
450+
/*
451+
* Validate each sample rate with the altsetting
452+
* Rebuild the rate table if only partial values are valid
453+
*/
454+
static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
455+
struct audioformat *fp,
456+
int clock)
457+
{
458+
struct usb_device *dev = chip->dev;
459+
unsigned int *table;
460+
unsigned int nr_rates;
461+
unsigned int rate_min = 0x7fffffff;
462+
unsigned int rate_max = 0;
463+
unsigned int rates = 0;
464+
int i, err;
465+
466+
table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
467+
if (!table)
468+
return -ENOMEM;
469+
470+
/* clear the interface altsetting at first */
471+
usb_set_interface(dev, fp->iface, 0);
472+
473+
nr_rates = 0;
474+
for (i = 0; i < fp->nr_rates; i++) {
475+
err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
476+
fp->rate_table[i]);
477+
if (err < 0)
478+
continue;
479+
480+
if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting)) {
481+
table[nr_rates++] = fp->rate_table[i];
482+
if (rate_min > fp->rate_table[i])
483+
rate_min = fp->rate_table[i];
484+
if (rate_max < fp->rate_table[i])
485+
rate_max = fp->rate_table[i];
486+
rates |= snd_pcm_rate_to_rate_bit(fp->rate_table[i]);
487+
}
488+
}
489+
490+
if (!nr_rates) {
491+
usb_audio_dbg(chip,
492+
"No valid sample rate available for %d:%d, assuming a firmware bug\n",
493+
fp->iface, fp->altsetting);
494+
nr_rates = fp->nr_rates; /* continue as is */
495+
}
496+
497+
if (fp->nr_rates == nr_rates) {
498+
kfree(table);
499+
return 0;
500+
}
501+
502+
kfree(fp->rate_table);
503+
fp->rate_table = table;
504+
fp->nr_rates = nr_rates;
505+
fp->rate_min = rate_min;
506+
fp->rate_max = rate_max;
507+
fp->rates = rates;
508+
return 0;
509+
}
510+
420511
/*
421512
* parse the format descriptor and stores the possible sample rates
422513
* 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,
509600
* allocated, so the rates will be stored */
510601
parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
511602

603+
ret = validate_sample_rate_table_v2v3(chip, fp, clock);
604+
512605
err_free:
513606
kfree(data);
514607
err:

0 commit comments

Comments
 (0)