Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *   Clock domain and sample rate management functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/usb/audio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/usb/audio-v2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/usb/audio-v3.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sound/info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "usbaudio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "card.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "helper.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "clock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "quirks.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 				 bool (*validator)(void *, int), u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	void *cs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 					     cs, type))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		if (validator(cs, id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 			return cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static bool validate_clock_source_v2(void *p, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct uac_clock_source_descriptor *cs = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	return cs->bClockID == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static bool validate_clock_source_v3(void *p, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct uac3_clock_source_descriptor *cs = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return cs->bClockID == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static bool validate_clock_selector_v2(void *p, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct uac_clock_selector_descriptor *cs = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return cs->bClockID == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static bool validate_clock_selector_v3(void *p, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct uac3_clock_selector_descriptor *cs = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return cs->bClockID == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static bool validate_clock_multiplier_v2(void *p, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct uac_clock_multiplier_descriptor *cs = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return cs->bClockID == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static bool validate_clock_multiplier_v3(void *p, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct uac3_clock_multiplier_descriptor *cs = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return cs->bClockID == id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define DEFINE_FIND_HELPER(name, obj, validator, type)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static obj *name(struct usb_host_interface *iface, int id)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return find_uac_clock_desc(iface, id, validator, type);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) DEFINE_FIND_HELPER(snd_usb_find_clock_source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		   struct uac_clock_source_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		   validate_clock_source_v2, UAC2_CLOCK_SOURCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) DEFINE_FIND_HELPER(snd_usb_find_clock_source_v3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		   struct uac3_clock_source_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		   validate_clock_source_v3, UAC3_CLOCK_SOURCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		   struct uac_clock_selector_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		   validate_clock_selector_v2, UAC2_CLOCK_SELECTOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) DEFINE_FIND_HELPER(snd_usb_find_clock_selector_v3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		   struct uac3_clock_selector_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		   validate_clock_selector_v3, UAC3_CLOCK_SELECTOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		   struct uac_clock_multiplier_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		   validate_clock_multiplier_v2, UAC2_CLOCK_MULTIPLIER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier_v3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		   struct uac3_clock_multiplier_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		   validate_clock_multiplier_v3, UAC3_CLOCK_MULTIPLIER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	unsigned char buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			      UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			      UAC2_CX_CLOCK_SELECTOR << 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			      &buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					unsigned char pin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			      UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			      UAC2_CX_CLOCK_SELECTOR << 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			      &pin, sizeof(pin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (ret != sizeof(pin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			"setting selector (id %d) unexpected length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			selector_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	ret = uac_clock_selector_get_val(chip, selector_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (ret != pin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			"setting selector (id %d) to %x failed (current: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			selector_id, pin, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 					    struct audioformat *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 					    int source_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	unsigned char data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (fmt->protocol == UAC_VERSION_2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		struct uac_clock_source_descriptor *cs_desc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			snd_usb_find_clock_source(chip->ctrl_intf, source_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (!cs_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		 * Assume the clock is valid if clock source supports only one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		 * single sample rate, the terminal is connected directly to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		 * (there is no clock selector) and clock type is internal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		 * This is to deal with some Denon DJ controllers that always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		 * reports that clock is invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (fmt->nr_rates == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		    (fmt->clock & 0xff) == cs_desc->bClockID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		    (cs_desc->bmAttributes & 0x3) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				UAC_CLOCK_SOURCE_TYPE_EXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 * MOTU MicroBook IIc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * Sample rate changes takes more than 2 seconds for this device. Clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 * validity request returns false during that period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (chip->usb_id == USB_ID(0x07fd, 0x0004)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		while ((!ret) && (count < 50)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 					      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 					      UAC2_CS_CONTROL_CLOCK_VALID << 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 					      snd_usb_ctrl_intf(chip) | (source_id << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 					      &data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				dev_warn(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 					 "%s(): cannot get clock validity for id %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					   __func__, source_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			ret = !!data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				      struct audioformat *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				      int source_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	unsigned char data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	u32 bmControls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (fmt->protocol == UAC_VERSION_3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		struct uac3_clock_source_descriptor *cs_desc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		if (!cs_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		bmControls = le32_to_cpu(cs_desc->bmControls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	} else { /* UAC_VERSION_1/2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		struct uac_clock_source_descriptor *cs_desc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			snd_usb_find_clock_source(chip->ctrl_intf, source_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		if (!cs_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		bmControls = cs_desc->bmControls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	/* If a clock source can't tell us whether it's valid, we assume it is */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (!uac_v2v3_control_is_readable(bmControls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				      UAC2_CS_CONTROL_CLOCK_VALID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			      UAC2_CS_CONTROL_CLOCK_VALID << 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			      snd_usb_ctrl_intf(chip) | (source_id << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			      &data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		dev_warn(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			 "%s(): cannot get clock validity for id %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			   __func__, source_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static int __uac_clock_find_source(struct snd_usb_audio *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				   struct audioformat *fmt, int entity_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				   unsigned long *visited, bool validate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct uac_clock_source_descriptor *source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct uac_clock_selector_descriptor *selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct uac_clock_multiplier_descriptor *multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	entity_id &= 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (test_and_set_bit(entity_id, visited)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		usb_audio_warn(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			 "%s(): recursive clock topology detected, id %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			 __func__, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/* first, see if the ID we're looking for is a clock source already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (source) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		entity_id = source->bClockID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		if (validate && !uac_clock_source_is_valid(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 								entity_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				"clock source %d is not valid, cannot use\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return entity_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (selector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		int ret, i, cur, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		/* the entity ID we are looking for is a selector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		 * find out what it currently selects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		ret = uac_clock_selector_get_val(chip, selector->bClockID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		/* Selector values are one-based */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if (ret > selector->bNrInPins || ret < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				"%s(): selector reported illegal value, id %d, ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 				__func__, selector->bClockID, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		cur = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		ret = __uac_clock_find_source(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 					      selector->baCSourceID[ret - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 					      visited, validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			 * For Samsung USBC Headset (AKG), setting clock selector again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			 * will result in incorrect default clock setting problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			if (chip->usb_id == USB_ID(0x04e8, 0xa051))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			err = uac_clock_selector_set_val(chip, entity_id, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		if (!validate || ret > 0 || !chip->autoclock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		/* The current clock source is invalid, try others. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		for (i = 1; i <= selector->bNrInPins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			if (i == cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			ret = __uac_clock_find_source(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 						      selector->baCSourceID[i - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 						      visited, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			err = uac_clock_selector_set_val(chip, entity_id, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			usb_audio_info(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				 "found and selected valid clock source %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 				 ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	/* FIXME: multipliers only act as pass-thru element for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (multiplier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return __uac_clock_find_source(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 					       multiplier->bCSourceID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 					       visited, validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static int __uac3_clock_find_source(struct snd_usb_audio *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				    struct audioformat *fmt, int entity_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 				    unsigned long *visited, bool validate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct uac3_clock_source_descriptor *source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	struct uac3_clock_selector_descriptor *selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	struct uac3_clock_multiplier_descriptor *multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	entity_id &= 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (test_and_set_bit(entity_id, visited)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		usb_audio_warn(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			 "%s(): recursive clock topology detected, id %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			 __func__, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	/* first, see if the ID we're looking for is a clock source already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (source) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		entity_id = source->bClockID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		if (validate && !uac_clock_source_is_valid(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 								entity_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				"clock source %d is not valid, cannot use\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return entity_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (selector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		int ret, i, cur, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		/* the entity ID we are looking for is a selector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		 * find out what it currently selects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		ret = uac_clock_selector_get_val(chip, selector->bClockID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		/* Selector values are one-based */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		if (ret > selector->bNrInPins || ret < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				"%s(): selector reported illegal value, id %d, ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 				__func__, selector->bClockID, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		cur = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		ret = __uac3_clock_find_source(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 					       selector->baCSourceID[ret - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 					       visited, validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			err = uac_clock_selector_set_val(chip, entity_id, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		if (!validate || ret > 0 || !chip->autoclock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		/* The current clock source is invalid, try others. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		for (i = 1; i <= selector->bNrInPins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			if (i == cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			ret = __uac3_clock_find_source(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 						       selector->baCSourceID[i - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 						       visited, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			err = uac_clock_selector_set_val(chip, entity_id, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			usb_audio_info(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 				 "found and selected valid clock source %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 				 ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	/* FIXME: multipliers only act as pass-thru element for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 						      entity_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (multiplier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return __uac3_clock_find_source(chip, fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 						multiplier->bCSourceID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 						visited, validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)  * For all kinds of sample rate settings and other device queries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)  * the clock source (end-leaf) must be used. However, clock selectors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)  * clock multipliers and sample rate converters may be specified as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)  * clock source input to terminal. This functions walks the clock path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)  * to its end and tries to find the source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)  * The 'visited' bitfield is used internally to detect recursive loops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)  * Returns the clock source UnitID (>=0) on success, or an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) int snd_usb_clock_find_source(struct snd_usb_audio *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			      struct audioformat *fmt, bool validate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	DECLARE_BITMAP(visited, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	memset(visited, 0, sizeof(visited));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	switch (fmt->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	case UAC_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 					       validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	case UAC_VERSION_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		return __uac3_clock_find_source(chip, fmt, fmt->clock, visited,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 					       validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			      struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			      struct audioformat *fmt, int rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	unsigned int ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	unsigned char data[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	int err, crate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (get_iface_desc(alts)->bNumEndpoints < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	ep = get_endpoint(alts, 0)->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	/* if endpoint doesn't have sampling rate control, bail out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	data[0] = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	data[1] = rate >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	data[2] = rate >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			      UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			      data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			iface, fmt->altsetting, rate, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	/* Don't check the sample rate for devices which we know don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	 * support reading */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (snd_usb_get_sample_rate_quirk(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	/* the firmware is likely buggy, don't repeat to fail too many times */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (chip->sample_rate_read_error > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			      UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			      data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			iface, fmt->altsetting, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		chip->sample_rate_read_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		return 0; /* some devices don't support reading */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	crate = data[0] | (data[1] << 8) | (data[2] << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (!crate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		dev_info(&dev->dev, "failed to read current rate; disabling the check\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		chip->sample_rate_read_error = 3; /* three strikes, see above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (crate != rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		// runtime->rate = crate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			      int altsetting, int clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	__le32 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			      snd_usb_ctrl_intf(chip) | (clock << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			      &data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			 iface, altsetting, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	return le32_to_cpu(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			      struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			      struct audioformat *fmt, int rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	__le32 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	int err, cur_rate, prev_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	int clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	bool writeable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	u32 bmControls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	/* First, try to find a valid clock. This may trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	 * automatic clock selection if the current clock is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	 * valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	clock = snd_usb_clock_find_source(chip, fmt, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (clock < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		/* We did not find a valid clock, but that might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		 * because the current sample rate does not match an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		 * external clock source. Try again without validation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		 * and we will do another validation after setting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		 * rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		clock = snd_usb_clock_find_source(chip, fmt, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		if (clock < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			return clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (prev_rate == rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		goto validation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	if (fmt->protocol == UAC_VERSION_3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		struct uac3_clock_source_descriptor *cs_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		bmControls = le32_to_cpu(cs_desc->bmControls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		struct uac_clock_source_descriptor *cs_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		bmControls = cs_desc->bmControls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	writeable = uac_v2v3_control_is_writeable(bmControls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 						  UAC2_CS_CONTROL_SAM_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (writeable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		data = cpu_to_le32(rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 				      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 				      UAC2_CS_CONTROL_SAM_FREQ << 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 				      snd_usb_ctrl_intf(chip) | (clock << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 				      &data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			usb_audio_err(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 				"%d:%d: cannot set freq %d (v2/v3): err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 				iface, fmt->altsetting, rate, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		cur_rate = get_sample_rate_v2v3(chip, iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 						fmt->altsetting, clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		cur_rate = prev_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (cur_rate != rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		if (!writeable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			usb_audio_warn(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 				 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 				 iface, fmt->altsetting, rate, cur_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		usb_audio_dbg(chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			"current rate %d is different from the runtime rate %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			cur_rate, rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	/* Some devices doesn't respond to sample rate changes while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	 * interface is active. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	if (rate != prev_rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		usb_set_interface(dev, iface, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		snd_vendor_set_interface(dev, alts, iface, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		snd_usb_set_interface_quirk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		usb_set_interface(dev, iface, fmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		snd_vendor_set_interface(dev, alts, iface, fmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		snd_usb_set_interface_quirk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) validation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	/* validate clock after rate change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	if (!uac_clock_source_is_valid(chip, fmt, clock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 			     struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 			     struct audioformat *fmt, int rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	switch (fmt->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	case UAC_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		return set_sample_rate_v1(chip, iface, alts, fmt, rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	case UAC_VERSION_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 			if (rate != UAC3_BADD_SAMPLING_RATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 				return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	case UAC_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)