clock.c 18.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11
/*
 *   Clock domain and sample rate management functions
 */

#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/usb.h>
#include <linux/usb/audio.h>
#include <linux/usb/audio-v2.h>
12
#include <linux/usb/audio-v3.h>
13 14 15 16 17 18 19 20

#include <sound/core.h>
#include <sound/info.h>
#include <sound/pcm.h>

#include "usbaudio.h"
#include "card.h"
#include "helper.h"
21
#include "clock.h"
22
#include "quirks.h"
23

24 25
static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
				 bool (*validator)(void *, int), u8 type)
26
{
27
	void *cs = NULL;
28

29 30 31
	while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
					     cs, type))) {
		if (validator(cs, id))
32 33 34 35 36 37
			return cs;
	}

	return NULL;
}

38
static bool validate_clock_source_v2(void *p, int id)
39
{
40
	struct uac_clock_source_descriptor *cs = p;
41
	return cs->bClockID == id;
42 43
}

44
static bool validate_clock_source_v3(void *p, int id)
45
{
46
	struct uac3_clock_source_descriptor *cs = p;
47
	return cs->bClockID == id;
48 49
}

50
static bool validate_clock_selector_v2(void *p, int id)
51
{
52
	struct uac_clock_selector_descriptor *cs = p;
53
	return cs->bClockID == id;
54 55
}

56
static bool validate_clock_selector_v3(void *p, int id)
57
{
58
	struct uac3_clock_selector_descriptor *cs = p;
59
	return cs->bClockID == id;
60 61
}

62
static bool validate_clock_multiplier_v2(void *p, int id)
63
{
64
	struct uac_clock_multiplier_descriptor *cs = p;
65
	return cs->bClockID == id;
66
}
67

68 69 70
static bool validate_clock_multiplier_v3(void *p, int id)
{
	struct uac3_clock_multiplier_descriptor *cs = p;
71
	return cs->bClockID == id;
72
}
73

74 75 76 77
#define DEFINE_FIND_HELPER(name, obj, validator, type)		\
static obj *name(struct usb_host_interface *iface, int id)	\
{								\
	return find_uac_clock_desc(iface, id, validator, type);	\
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
DEFINE_FIND_HELPER(snd_usb_find_clock_source,
		   struct uac_clock_source_descriptor,
		   validate_clock_source_v2, UAC2_CLOCK_SOURCE);
DEFINE_FIND_HELPER(snd_usb_find_clock_source_v3,
		   struct uac3_clock_source_descriptor,
		   validate_clock_source_v3, UAC3_CLOCK_SOURCE);

DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
		   struct uac_clock_selector_descriptor,
		   validate_clock_selector_v2, UAC2_CLOCK_SELECTOR);
DEFINE_FIND_HELPER(snd_usb_find_clock_selector_v3,
		   struct uac3_clock_selector_descriptor,
		   validate_clock_selector_v3, UAC3_CLOCK_SELECTOR);

DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
		   struct uac_clock_multiplier_descriptor,
		   validate_clock_multiplier_v2, UAC2_CLOCK_MULTIPLIER);
DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier_v3,
		   struct uac3_clock_multiplier_descriptor,
		   validate_clock_multiplier_v3, UAC3_CLOCK_MULTIPLIER);

101 102 103 104 105 106 107 108
static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
{
	unsigned char buf;
	int ret;

	ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
			      UAC2_CS_CUR,
			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
109 110
			      UAC2_CX_CLOCK_SELECTOR << 8,
			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
111
			      &buf, sizeof(buf));
112 113 114 115 116 117 118

	if (ret < 0)
		return ret;

	return buf;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
					unsigned char pin)
{
	int ret;

	ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
			      UAC2_CS_CUR,
			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
			      UAC2_CX_CLOCK_SELECTOR << 8,
			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
			      &pin, sizeof(pin));
	if (ret < 0)
		return ret;

	if (ret != sizeof(pin)) {
134 135 136
		usb_audio_err(chip,
			"setting selector (id %d) unexpected length %d\n",
			selector_id, ret);
137 138 139 140 141 142 143 144
		return -EINVAL;
	}

	ret = uac_clock_selector_get_val(chip, selector_id);
	if (ret < 0)
		return ret;

	if (ret != pin) {
145 146 147
		usb_audio_err(chip,
			"setting selector (id %d) to %x failed (current: %d)\n",
			selector_id, pin, ret);
148 149 150 151 152 153
		return -EINVAL;
	}

	return ret;
}

154
static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
155
					    const struct audioformat *fmt,
156 157
					    int source_id)
{
158 159 160 161 162
	bool ret = false;
	int count;
	unsigned char data;
	struct usb_device *dev = chip->dev;

163 164 165 166 167 168 169
	if (fmt->protocol == UAC_VERSION_2) {
		struct uac_clock_source_descriptor *cs_desc =
			snd_usb_find_clock_source(chip->ctrl_intf, source_id);

		if (!cs_desc)
			return false;

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		/*
		 * Assume the clock is valid if clock source supports only one
		 * single sample rate, the terminal is connected directly to it
		 * (there is no clock selector) and clock type is internal.
		 * This is to deal with some Denon DJ controllers that always
		 * reports that clock is invalid.
		 */
		if (fmt->nr_rates == 1 &&
		    (fmt->clock & 0xff) == cs_desc->bClockID &&
		    (cs_desc->bmAttributes & 0x3) !=
				UAC_CLOCK_SOURCE_TYPE_EXT)
			return true;
	}

	/*
	 * MOTU MicroBook IIc
	 * Sample rate changes takes more than 2 seconds for this device. Clock
	 * validity request returns false during that period.
	 */
	if (chip->usb_id == USB_ID(0x07fd, 0x0004)) {
		count = 0;

		while ((!ret) && (count < 50)) {
			int err;

			msleep(100);

			err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
					      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
					      UAC2_CS_CONTROL_CLOCK_VALID << 8,
					      snd_usb_ctrl_intf(chip) | (source_id << 8),
					      &data, sizeof(data));
			if (err < 0) {
				dev_warn(&dev->dev,
					 "%s(): cannot get clock validity for id %d\n",
					   __func__, source_id);
				return false;
			}

			ret = !!data;
			count++;
		}
212 213
	}

214
	return ret;
215 216
}

217
static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
218
				      const struct audioformat *fmt,
219
				      int source_id)
220 221 222 223
{
	int err;
	unsigned char data;
	struct usb_device *dev = chip->dev;
224 225
	u32 bmControls;

226
	if (fmt->protocol == UAC_VERSION_3) {
227 228 229 230
		struct uac3_clock_source_descriptor *cs_desc =
			snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);

		if (!cs_desc)
231
			return false;
232 233 234 235 236 237
		bmControls = le32_to_cpu(cs_desc->bmControls);
	} else { /* UAC_VERSION_1/2 */
		struct uac_clock_source_descriptor *cs_desc =
			snd_usb_find_clock_source(chip->ctrl_intf, source_id);

		if (!cs_desc)
238
			return false;
239 240
		bmControls = cs_desc->bmControls;
	}
241 242

	/* If a clock source can't tell us whether it's valid, we assume it is */
243
	if (!uac_v2v3_control_is_readable(bmControls,
244
				      UAC2_CS_CONTROL_CLOCK_VALID))
245
		return true;
246 247 248

	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
249 250
			      UAC2_CS_CONTROL_CLOCK_VALID << 8,
			      snd_usb_ctrl_intf(chip) | (source_id << 8),
251
			      &data, sizeof(data));
252 253

	if (err < 0) {
254 255
		dev_warn(&dev->dev,
			 "%s(): cannot get clock validity for id %d\n",
256
			   __func__, source_id);
257
		return false;
258 259
	}

260 261 262 263
	if (data)
		return true;
	else
		return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
264 265
}

266
static int __uac_clock_find_source(struct snd_usb_audio *chip,
267
				   const struct audioformat *fmt, int entity_id,
268
				   unsigned long *visited, bool validate)
269 270 271 272 273 274 275 276
{
	struct uac_clock_source_descriptor *source;
	struct uac_clock_selector_descriptor *selector;
	struct uac_clock_multiplier_descriptor *multiplier;

	entity_id &= 0xff;

	if (test_and_set_bit(entity_id, visited)) {
277 278 279
		usb_audio_warn(chip,
			 "%s(): recursive clock topology detected, id %d.\n",
			 __func__, entity_id);
280 281 282 283
		return -EINVAL;
	}

	/* first, see if the ID we're looking for is a clock source already */
284
	source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
285 286
	if (source) {
		entity_id = source->bClockID;
287
		if (validate && !uac_clock_source_is_valid(chip, fmt,
288
								entity_id)) {
289 290 291
			usb_audio_err(chip,
				"clock source %d is not valid, cannot use\n",
				entity_id);
292 293 294 295
			return -ENXIO;
		}
		return entity_id;
	}
296

297
	selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
298
	if (selector) {
299
		int ret, i, cur;
300 301 302 303 304 305 306

		/* the entity ID we are looking for is a selector.
		 * find out what it currently selects */
		ret = uac_clock_selector_get_val(chip, selector->bClockID);
		if (ret < 0)
			return ret;

307 308
		/* Selector values are one-based */

309
		if (ret > selector->bNrInPins || ret < 1) {
310
			usb_audio_err(chip,
311 312 313 314 315 316
				"%s(): selector reported illegal value, id %d, ret %d\n",
				__func__, selector->bClockID, ret);

			return -EINVAL;
		}

317
		cur = ret;
318 319 320
		ret = __uac_clock_find_source(chip, fmt,
					      selector->baCSourceID[ret - 1],
					      visited, validate);
321
		if (!validate || ret > 0 || !chip->autoclock)
322 323 324 325 326 327 328 329 330
			return ret;

		/* The current clock source is invalid, try others. */
		for (i = 1; i <= selector->bNrInPins; i++) {
			int err;

			if (i == cur)
				continue;

331 332 333
			ret = __uac_clock_find_source(chip, fmt,
						      selector->baCSourceID[i - 1],
						      visited, true);
334 335 336 337 338 339 340
			if (ret < 0)
				continue;

			err = uac_clock_selector_set_val(chip, entity_id, i);
			if (err < 0)
				continue;

341 342 343
			usb_audio_info(chip,
				 "found and selected valid clock source %d\n",
				 ret);
344 345 346 347
			return ret;
		}

		return -ENXIO;
348 349 350
	}

	/* FIXME: multipliers only act as pass-thru element for now */
351
	multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
352
	if (multiplier)
353 354 355
		return __uac_clock_find_source(chip, fmt,
					       multiplier->bCSourceID,
					       visited, validate);
356 357 358 359

	return -EINVAL;
}

360
static int __uac3_clock_find_source(struct snd_usb_audio *chip,
361
				    const struct audioformat *fmt, int entity_id,
362
				    unsigned long *visited, bool validate)
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
	struct uac3_clock_source_descriptor *source;
	struct uac3_clock_selector_descriptor *selector;
	struct uac3_clock_multiplier_descriptor *multiplier;

	entity_id &= 0xff;

	if (test_and_set_bit(entity_id, visited)) {
		usb_audio_warn(chip,
			 "%s(): recursive clock topology detected, id %d.\n",
			 __func__, entity_id);
		return -EINVAL;
	}

	/* first, see if the ID we're looking for is a clock source already */
	source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
	if (source) {
		entity_id = source->bClockID;
381
		if (validate && !uac_clock_source_is_valid(chip, fmt,
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
								entity_id)) {
			usb_audio_err(chip,
				"clock source %d is not valid, cannot use\n",
				entity_id);
			return -ENXIO;
		}
		return entity_id;
	}

	selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
	if (selector) {
		int ret, i, cur;

		/* the entity ID we are looking for is a selector.
		 * find out what it currently selects */
		ret = uac_clock_selector_get_val(chip, selector->bClockID);
		if (ret < 0)
			return ret;

		/* Selector values are one-based */

		if (ret > selector->bNrInPins || ret < 1) {
			usb_audio_err(chip,
				"%s(): selector reported illegal value, id %d, ret %d\n",
				__func__, selector->bClockID, ret);

			return -EINVAL;
		}

		cur = ret;
412 413
		ret = __uac3_clock_find_source(chip, fmt,
					       selector->baCSourceID[ret - 1],
414 415 416 417 418 419 420 421 422 423 424
					       visited, validate);
		if (!validate || ret > 0 || !chip->autoclock)
			return ret;

		/* The current clock source is invalid, try others. */
		for (i = 1; i <= selector->bNrInPins; i++) {
			int err;

			if (i == cur)
				continue;

425 426 427
			ret = __uac3_clock_find_source(chip, fmt,
						       selector->baCSourceID[i - 1],
						       visited, true);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
			if (ret < 0)
				continue;

			err = uac_clock_selector_set_val(chip, entity_id, i);
			if (err < 0)
				continue;

			usb_audio_info(chip,
				 "found and selected valid clock source %d\n",
				 ret);
			return ret;
		}

		return -ENXIO;
	}

	/* FIXME: multipliers only act as pass-thru element for now */
	multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
						      entity_id);
	if (multiplier)
448 449
		return __uac3_clock_find_source(chip, fmt,
						multiplier->bCSourceID,
450 451 452 453 454
						visited, validate);

	return -EINVAL;
}

455 456 457 458 459 460 461 462 463 464 465
/*
 * For all kinds of sample rate settings and other device queries,
 * the clock source (end-leaf) must be used. However, clock selectors,
 * clock multipliers and sample rate converters may be specified as
 * clock source input to terminal. This functions walks the clock path
 * to its end and tries to find the source.
 *
 * The 'visited' bitfield is used internally to detect recursive loops.
 *
 * Returns the clock source UnitID (>=0) on success, or an error.
 */
466
int snd_usb_clock_find_source(struct snd_usb_audio *chip,
467
			      const struct audioformat *fmt, bool validate)
468 469 470
{
	DECLARE_BITMAP(visited, 256);
	memset(visited, 0, sizeof(visited));
471

472
	switch (fmt->protocol) {
473
	case UAC_VERSION_2:
474
		return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
475 476
					       validate);
	case UAC_VERSION_3:
477
		return __uac3_clock_find_source(chip, fmt, fmt->clock, visited,
478 479 480 481
					       validate);
	default:
		return -EINVAL;
	}
482 483
}

484
static int set_sample_rate_v1(struct snd_usb_audio *chip,
485
			      const struct audioformat *fmt, int rate)
486 487
{
	struct usb_device *dev = chip->dev;
488
	struct usb_host_interface *alts;
489 490 491 492
	unsigned int ep;
	unsigned char data[3];
	int err, crate;

493 494 495
	alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
	if (!alts)
		return -EINVAL;
496 497
	if (get_iface_desc(alts)->bNumEndpoints < 1)
		return -EINVAL;
498 499 500
	ep = get_endpoint(alts, 0)->bEndpointAddress;

	/* if endpoint doesn't have sampling rate control, bail out */
501
	if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
502 503 504 505 506
		return 0;

	data[0] = rate;
	data[1] = rate >> 8;
	data[2] = rate >> 16;
507 508 509 510 511
	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
			      UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
			      data, sizeof(data));
	if (err < 0) {
512
		dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
513
			fmt->iface, fmt->altsetting, rate, ep);
514 515 516
		return err;
	}

517 518 519 520
	/* Don't check the sample rate for devices which we know don't
	 * support reading */
	if (snd_usb_get_sample_rate_quirk(chip))
		return 0;
521 522 523
	/* the firmware is likely buggy, don't repeat to fail too many times */
	if (chip->sample_rate_read_error > 2)
		return 0;
524

525 526 527 528 529
	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
			      UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
			      data, sizeof(data));
	if (err < 0) {
530
		dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
531
			fmt->iface, fmt->altsetting, ep);
532
		chip->sample_rate_read_error++;
533 534 535 536 537
		return 0; /* some devices don't support reading */
	}

	crate = data[0] | (data[1] << 8) | (data[2] << 16);
	if (crate != rate) {
538
		dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
539 540 541 542 543 544
		// runtime->rate = crate;
	}

	return 0;
}

545
static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
546 547 548
			      int altsetting, int clock)
{
	struct usb_device *dev = chip->dev;
549
	__le32 data;
550 551 552 553 554 555
	int err;

	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
			      UAC2_CS_CONTROL_SAM_FREQ << 8,
			      snd_usb_ctrl_intf(chip) | (clock << 8),
556
			      &data, sizeof(data));
557
	if (err < 0) {
558
		dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
559
			 iface, altsetting, err);
560 561 562
		return 0;
	}

563
	return le32_to_cpu(data);
564 565
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
/*
 * 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);
}

613
static int set_sample_rate_v2v3(struct snd_usb_audio *chip,
614
				const struct audioformat *fmt, int rate)
615
{
616
	int cur_rate, prev_rate;
617
	int clock;
618

619 620 621 622
	/* First, try to find a valid clock. This may trigger
	 * automatic clock selection if the current clock is not
	 * valid.
	 */
623
	clock = snd_usb_clock_find_source(chip, fmt, true);
624 625 626 627 628 629 630
	if (clock < 0) {
		/* We did not find a valid clock, but that might be
		 * because the current sample rate does not match an
		 * external clock source. Try again without validation
		 * and we will do another validation after setting the
		 * rate.
		 */
631
		clock = snd_usb_clock_find_source(chip, fmt, false);
632 633 634
		if (clock < 0)
			return clock;
	}
635

636
	prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
637
	if (prev_rate == rate)
638
		goto validation;
639

640 641 642 643
	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",
644
			      fmt->iface, fmt->altsetting, rate, cur_rate);
645
		return cur_rate;
646 647
	}

648
	if (!cur_rate)
649
		cur_rate = prev_rate;
650

651
	if (cur_rate != rate) {
652 653 654 655
		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;
656 657
	}

658 659
validation:
	/* validate clock after rate change */
660
	if (!uac_clock_source_is_valid(chip, fmt, clock))
661
		return -ENXIO;
662 663 664
	return 0;
}

665
int snd_usb_init_sample_rate(struct snd_usb_audio *chip,
666
			     const struct audioformat *fmt, int rate)
667
{
668 669 670
	usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n",
		      fmt->iface, fmt->altsetting, rate, fmt->clock);

671
	switch (fmt->protocol) {
672
	case UAC_VERSION_1:
673
	default:
674
		return set_sample_rate_v1(chip, fmt, rate);
675

676
	case UAC_VERSION_3:
677 678 679 680 681 682
		if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
			if (rate != UAC3_BADD_SAMPLING_RATE)
				return -ENXIO;
			else
				return 0;
		}
683
		fallthrough;
684
	case UAC_VERSION_2:
685
		return set_sample_rate_v2v3(chip, fmt, rate);
686 687 688
	}
}