11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
279f920fbSDaniel Mack /*
379f920fbSDaniel Mack * Clock domain and sample rate management functions
479f920fbSDaniel Mack */
579f920fbSDaniel Mack
679f920fbSDaniel Mack #include <linux/bitops.h>
779f920fbSDaniel Mack #include <linux/init.h>
879f920fbSDaniel Mack #include <linux/string.h>
979f920fbSDaniel Mack #include <linux/usb.h>
1079f920fbSDaniel Mack #include <linux/usb/audio.h>
1179f920fbSDaniel Mack #include <linux/usb/audio-v2.h>
129a2fe9b8SRuslan Bilovol #include <linux/usb/audio-v3.h>
1379f920fbSDaniel Mack
1479f920fbSDaniel Mack #include <sound/core.h>
1579f920fbSDaniel Mack #include <sound/info.h>
1679f920fbSDaniel Mack #include <sound/pcm.h>
1779f920fbSDaniel Mack
1879f920fbSDaniel Mack #include "usbaudio.h"
1979f920fbSDaniel Mack #include "card.h"
2079f920fbSDaniel Mack #include "helper.h"
21f22aa949SDaniel Mack #include "clock.h"
2221bb5aafSDaniel Mack #include "quirks.h"
2379f920fbSDaniel Mack
249ec73005STakashi Iwai union uac23_clock_source_desc {
259ec73005STakashi Iwai struct uac_clock_source_descriptor v2;
269ec73005STakashi Iwai struct uac3_clock_source_descriptor v3;
279ec73005STakashi Iwai };
289ec73005STakashi Iwai
299ec73005STakashi Iwai union uac23_clock_selector_desc {
309ec73005STakashi Iwai struct uac_clock_selector_descriptor v2;
319ec73005STakashi Iwai struct uac3_clock_selector_descriptor v3;
329ec73005STakashi Iwai };
339ec73005STakashi Iwai
349ec73005STakashi Iwai union uac23_clock_multiplier_desc {
359ec73005STakashi Iwai struct uac_clock_multiplier_descriptor v2;
369ec73005STakashi Iwai struct uac_clock_multiplier_descriptor v3;
379ec73005STakashi Iwai };
389ec73005STakashi Iwai
399ec73005STakashi Iwai #define GET_VAL(p, proto, field) \
409ec73005STakashi Iwai ((proto) == UAC_VERSION_3 ? (p)->v3.field : (p)->v2.field)
419ec73005STakashi Iwai
find_uac_clock_desc(struct usb_host_interface * iface,int id,bool (* validator)(void *,int,int),u8 type,int proto)42f7645bd6STakashi Iwai static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
439ec73005STakashi Iwai bool (*validator)(void *, int, int),
449ec73005STakashi Iwai u8 type, int proto)
4579f920fbSDaniel Mack {
46f7645bd6STakashi Iwai void *cs = NULL;
4779f920fbSDaniel Mack
48f7645bd6STakashi Iwai while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
49f7645bd6STakashi Iwai cs, type))) {
509ec73005STakashi Iwai if (validator(cs, id, proto))
5179f920fbSDaniel Mack return cs;
5279f920fbSDaniel Mack }
5379f920fbSDaniel Mack
5479f920fbSDaniel Mack return NULL;
5579f920fbSDaniel Mack }
5679f920fbSDaniel Mack
validate_clock_source(void * p,int id,int proto)579ec73005STakashi Iwai static bool validate_clock_source(void *p, int id, int proto)
589a2fe9b8SRuslan Bilovol {
599ec73005STakashi Iwai union uac23_clock_source_desc *cs = p;
609ec73005STakashi Iwai
619ec73005STakashi Iwai return GET_VAL(cs, proto, bClockID) == id;
629a2fe9b8SRuslan Bilovol }
639a2fe9b8SRuslan Bilovol
validate_clock_selector(void * p,int id,int proto)649ec73005STakashi Iwai static bool validate_clock_selector(void *p, int id, int proto)
6579f920fbSDaniel Mack {
669ec73005STakashi Iwai union uac23_clock_selector_desc *cs = p;
679ec73005STakashi Iwai
689ec73005STakashi Iwai return GET_VAL(cs, proto, bClockID) == id;
690a62d6c9STakashi Iwai }
7079f920fbSDaniel Mack
validate_clock_multiplier(void * p,int id,int proto)719ec73005STakashi Iwai static bool validate_clock_multiplier(void *p, int id, int proto)
729a2fe9b8SRuslan Bilovol {
739ec73005STakashi Iwai union uac23_clock_multiplier_desc *cs = p;
749ec73005STakashi Iwai
759ec73005STakashi Iwai return GET_VAL(cs, proto, bClockID) == id;
769a2fe9b8SRuslan Bilovol }
779a2fe9b8SRuslan Bilovol
789ec73005STakashi Iwai #define DEFINE_FIND_HELPER(name, obj, validator, type2, type3) \
79*6aa87001SKarol Kosik static obj *name(struct snd_usb_audio *chip, int id, \
80*6aa87001SKarol Kosik const struct audioformat *fmt) \
81f7645bd6STakashi Iwai { \
82*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf = \
83*6aa87001SKarol Kosik snd_usb_find_ctrl_interface(chip, fmt->iface); \
84*6aa87001SKarol Kosik return find_uac_clock_desc(ctrl_intf, id, validator, \
85*6aa87001SKarol Kosik fmt->protocol == UAC_VERSION_3 ? (type3) : (type2), \
86*6aa87001SKarol Kosik fmt->protocol); \
87f7645bd6STakashi Iwai }
88f7645bd6STakashi Iwai
89f7645bd6STakashi Iwai DEFINE_FIND_HELPER(snd_usb_find_clock_source,
909ec73005STakashi Iwai union uac23_clock_source_desc, validate_clock_source,
919ec73005STakashi Iwai UAC2_CLOCK_SOURCE, UAC3_CLOCK_SOURCE);
92f7645bd6STakashi Iwai DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
939ec73005STakashi Iwai union uac23_clock_selector_desc, validate_clock_selector,
949ec73005STakashi Iwai UAC2_CLOCK_SELECTOR, UAC3_CLOCK_SELECTOR);
95f7645bd6STakashi Iwai DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
969ec73005STakashi Iwai union uac23_clock_multiplier_desc, validate_clock_multiplier,
979ec73005STakashi Iwai UAC2_CLOCK_MULTIPLIER, UAC3_CLOCK_MULTIPLIER);
98f7645bd6STakashi Iwai
uac_clock_selector_get_val(struct snd_usb_audio * chip,int selector_id,int iface_no)99*6aa87001SKarol Kosik static int uac_clock_selector_get_val(struct snd_usb_audio *chip,
100*6aa87001SKarol Kosik int selector_id, int iface_no)
10179f920fbSDaniel Mack {
102*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf;
10379f920fbSDaniel Mack unsigned char buf;
10479f920fbSDaniel Mack int ret;
10579f920fbSDaniel Mack
106*6aa87001SKarol Kosik ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
10779f920fbSDaniel Mack ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
10879f920fbSDaniel Mack UAC2_CS_CUR,
10979f920fbSDaniel Mack USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
11011bcbc44SDaniel Mack UAC2_CX_CLOCK_SELECTOR << 8,
111*6aa87001SKarol Kosik snd_usb_ctrl_intf(ctrl_intf) | (selector_id << 8),
11217d900c4SClemens Ladisch &buf, sizeof(buf));
11379f920fbSDaniel Mack
11479f920fbSDaniel Mack if (ret < 0)
11579f920fbSDaniel Mack return ret;
11679f920fbSDaniel Mack
11779f920fbSDaniel Mack return buf;
11879f920fbSDaniel Mack }
11979f920fbSDaniel Mack
uac_clock_selector_set_val(struct snd_usb_audio * chip,int selector_id,unsigned char pin,int iface_no)120*6aa87001SKarol Kosik static int uac_clock_selector_set_val(struct snd_usb_audio *chip,
121*6aa87001SKarol Kosik int selector_id, unsigned char pin, int iface_no)
1228c55af3fSEldad Zack {
123*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf;
1248c55af3fSEldad Zack int ret;
1258c55af3fSEldad Zack
126*6aa87001SKarol Kosik ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
1278c55af3fSEldad Zack ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
1288c55af3fSEldad Zack UAC2_CS_CUR,
1298c55af3fSEldad Zack USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1308c55af3fSEldad Zack UAC2_CX_CLOCK_SELECTOR << 8,
131*6aa87001SKarol Kosik snd_usb_ctrl_intf(ctrl_intf) | (selector_id << 8),
1328c55af3fSEldad Zack &pin, sizeof(pin));
1338c55af3fSEldad Zack if (ret < 0)
1348c55af3fSEldad Zack return ret;
1358c55af3fSEldad Zack
1368c55af3fSEldad Zack if (ret != sizeof(pin)) {
1370ba41d91STakashi Iwai usb_audio_err(chip,
1380ba41d91STakashi Iwai "setting selector (id %d) unexpected length %d\n",
1390ba41d91STakashi Iwai selector_id, ret);
1408c55af3fSEldad Zack return -EINVAL;
1418c55af3fSEldad Zack }
1428c55af3fSEldad Zack
143*6aa87001SKarol Kosik ret = uac_clock_selector_get_val(chip, selector_id, iface_no);
1448c55af3fSEldad Zack if (ret < 0)
1458c55af3fSEldad Zack return ret;
1468c55af3fSEldad Zack
1478c55af3fSEldad Zack if (ret != pin) {
1480ba41d91STakashi Iwai usb_audio_err(chip,
1490ba41d91STakashi Iwai "setting selector (id %d) to %x failed (current: %d)\n",
1500ba41d91STakashi Iwai selector_id, pin, ret);
1518c55af3fSEldad Zack return -EINVAL;
1528c55af3fSEldad Zack }
1538c55af3fSEldad Zack
1548c55af3fSEldad Zack return ret;
1558c55af3fSEldad Zack }
1568c55af3fSEldad Zack
uac_clock_source_is_valid_quirk(struct snd_usb_audio * chip,const struct audioformat * fmt,int source_id)1579f35a312SAlexander Tsoy static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
158cab941b7STakashi Iwai const struct audioformat *fmt,
1599f35a312SAlexander Tsoy int source_id)
1609f35a312SAlexander Tsoy {
1612edb84e3SAlexander Tsoy bool ret = false;
1622edb84e3SAlexander Tsoy int count;
1632edb84e3SAlexander Tsoy unsigned char data;
1642edb84e3SAlexander Tsoy struct usb_device *dev = chip->dev;
1659ec73005STakashi Iwai union uac23_clock_source_desc *cs_desc;
166*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf;
1672edb84e3SAlexander Tsoy
168*6aa87001SKarol Kosik ctrl_intf = snd_usb_find_ctrl_interface(chip, fmt->iface);
169*6aa87001SKarol Kosik cs_desc = snd_usb_find_clock_source(chip, source_id, fmt);
1709f35a312SAlexander Tsoy if (!cs_desc)
1719f35a312SAlexander Tsoy return false;
1729f35a312SAlexander Tsoy
1739ec73005STakashi Iwai if (fmt->protocol == UAC_VERSION_2) {
1742edb84e3SAlexander Tsoy /*
1752edb84e3SAlexander Tsoy * Assume the clock is valid if clock source supports only one
1762edb84e3SAlexander Tsoy * single sample rate, the terminal is connected directly to it
1772edb84e3SAlexander Tsoy * (there is no clock selector) and clock type is internal.
1782edb84e3SAlexander Tsoy * This is to deal with some Denon DJ controllers that always
1792edb84e3SAlexander Tsoy * reports that clock is invalid.
1802edb84e3SAlexander Tsoy */
1812edb84e3SAlexander Tsoy if (fmt->nr_rates == 1 &&
1829ec73005STakashi Iwai (fmt->clock & 0xff) == cs_desc->v2.bClockID &&
1839ec73005STakashi Iwai (cs_desc->v2.bmAttributes & 0x3) !=
1842edb84e3SAlexander Tsoy UAC_CLOCK_SOURCE_TYPE_EXT)
1852edb84e3SAlexander Tsoy return true;
1869f35a312SAlexander Tsoy }
1879f35a312SAlexander Tsoy
1882edb84e3SAlexander Tsoy /*
1892edb84e3SAlexander Tsoy * MOTU MicroBook IIc
1902edb84e3SAlexander Tsoy * Sample rate changes takes more than 2 seconds for this device. Clock
1912edb84e3SAlexander Tsoy * validity request returns false during that period.
1922edb84e3SAlexander Tsoy */
1932edb84e3SAlexander Tsoy if (chip->usb_id == USB_ID(0x07fd, 0x0004)) {
1942edb84e3SAlexander Tsoy count = 0;
1952edb84e3SAlexander Tsoy
1962edb84e3SAlexander Tsoy while ((!ret) && (count < 50)) {
1972edb84e3SAlexander Tsoy int err;
1982edb84e3SAlexander Tsoy
1992edb84e3SAlexander Tsoy msleep(100);
2002edb84e3SAlexander Tsoy
2012edb84e3SAlexander Tsoy err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
2022edb84e3SAlexander Tsoy USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
2032edb84e3SAlexander Tsoy UAC2_CS_CONTROL_CLOCK_VALID << 8,
204*6aa87001SKarol Kosik snd_usb_ctrl_intf(ctrl_intf) | (source_id << 8),
2052edb84e3SAlexander Tsoy &data, sizeof(data));
2062edb84e3SAlexander Tsoy if (err < 0) {
2072edb84e3SAlexander Tsoy dev_warn(&dev->dev,
2082edb84e3SAlexander Tsoy "%s(): cannot get clock validity for id %d\n",
2092edb84e3SAlexander Tsoy __func__, source_id);
2109f35a312SAlexander Tsoy return false;
2119f35a312SAlexander Tsoy }
2129f35a312SAlexander Tsoy
2132edb84e3SAlexander Tsoy ret = !!data;
2142edb84e3SAlexander Tsoy count++;
2152edb84e3SAlexander Tsoy }
2162edb84e3SAlexander Tsoy }
2172edb84e3SAlexander Tsoy
2182edb84e3SAlexander Tsoy return ret;
2192edb84e3SAlexander Tsoy }
2202edb84e3SAlexander Tsoy
uac_clock_source_is_valid(struct snd_usb_audio * chip,const struct audioformat * fmt,int source_id)2219a2fe9b8SRuslan Bilovol static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
222cab941b7STakashi Iwai const struct audioformat *fmt,
2239a2fe9b8SRuslan Bilovol int source_id)
22479f920fbSDaniel Mack {
22579f920fbSDaniel Mack int err;
22679f920fbSDaniel Mack unsigned char data;
22779f920fbSDaniel Mack struct usb_device *dev = chip->dev;
2289a2fe9b8SRuslan Bilovol u32 bmControls;
2299ec73005STakashi Iwai union uac23_clock_source_desc *cs_desc;
230*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf;
2319a2fe9b8SRuslan Bilovol
232*6aa87001SKarol Kosik ctrl_intf = snd_usb_find_ctrl_interface(chip, fmt->iface);
233*6aa87001SKarol Kosik cs_desc = snd_usb_find_clock_source(chip, source_id, fmt);
2349a2fe9b8SRuslan Bilovol if (!cs_desc)
2351d4961d9SSaurav Girepunje return false;
2363bc6fbc7SDaniel Mack
2379ec73005STakashi Iwai if (fmt->protocol == UAC_VERSION_3)
2389ec73005STakashi Iwai bmControls = le32_to_cpu(cs_desc->v3.bmControls);
2399ec73005STakashi Iwai else
2409ec73005STakashi Iwai bmControls = cs_desc->v2.bmControls;
2413bc6fbc7SDaniel Mack
2423bc6fbc7SDaniel Mack /* If a clock source can't tell us whether it's valid, we assume it is */
2439a2fe9b8SRuslan Bilovol if (!uac_v2v3_control_is_readable(bmControls,
24421e9b3e9SAndrew Chant UAC2_CS_CONTROL_CLOCK_VALID))
2451d4961d9SSaurav Girepunje return true;
24679f920fbSDaniel Mack
24779f920fbSDaniel Mack err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
24879f920fbSDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
24911bcbc44SDaniel Mack UAC2_CS_CONTROL_CLOCK_VALID << 8,
250*6aa87001SKarol Kosik snd_usb_ctrl_intf(ctrl_intf) | (source_id << 8),
25117d900c4SClemens Ladisch &data, sizeof(data));
25279f920fbSDaniel Mack
25379f920fbSDaniel Mack if (err < 0) {
2540ba41d91STakashi Iwai dev_warn(&dev->dev,
2550ba41d91STakashi Iwai "%s(): cannot get clock validity for id %d\n",
25679f920fbSDaniel Mack __func__, source_id);
2571d4961d9SSaurav Girepunje return false;
25879f920fbSDaniel Mack }
25979f920fbSDaniel Mack
2609f35a312SAlexander Tsoy if (data)
2619f35a312SAlexander Tsoy return true;
2629f35a312SAlexander Tsoy else
2639f35a312SAlexander Tsoy return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
26479f920fbSDaniel Mack }
26579f920fbSDaniel Mack
__uac_clock_find_source(struct snd_usb_audio * chip,const struct audioformat * fmt,int entity_id,unsigned long * visited,bool validate)2669f35a312SAlexander Tsoy static int __uac_clock_find_source(struct snd_usb_audio *chip,
267cab941b7STakashi Iwai const struct audioformat *fmt, int entity_id,
2689a2fe9b8SRuslan Bilovol unsigned long *visited, bool validate)
26979f920fbSDaniel Mack {
2709ec73005STakashi Iwai union uac23_clock_source_desc *source;
2719ec73005STakashi Iwai union uac23_clock_selector_desc *selector;
2729ec73005STakashi Iwai union uac23_clock_multiplier_desc *multiplier;
2739ec73005STakashi Iwai int ret, i, cur, err, pins, clock_id;
2749ec73005STakashi Iwai const u8 *sources;
2759ec73005STakashi Iwai int proto = fmt->protocol;
276eeca59a6SAlexander Tsoy bool readable, writeable;
277eeca59a6SAlexander Tsoy u32 bmControls;
27879f920fbSDaniel Mack
27979f920fbSDaniel Mack entity_id &= 0xff;
28079f920fbSDaniel Mack
28179f920fbSDaniel Mack if (test_and_set_bit(entity_id, visited)) {
2820ba41d91STakashi Iwai usb_audio_warn(chip,
28379f920fbSDaniel Mack "%s(): recursive clock topology detected, id %d.\n",
28479f920fbSDaniel Mack __func__, entity_id);
28579f920fbSDaniel Mack return -EINVAL;
28679f920fbSDaniel Mack }
28779f920fbSDaniel Mack
288882e013aSGeraldo Nascimento /* first, see if the ID we're looking at is a clock source already */
289*6aa87001SKarol Kosik source = snd_usb_find_clock_source(chip, entity_id, fmt);
29006ffc1ebSEldad Zack if (source) {
2919ec73005STakashi Iwai entity_id = GET_VAL(source, proto, bClockID);
2929f35a312SAlexander Tsoy if (validate && !uac_clock_source_is_valid(chip, fmt,
2939a2fe9b8SRuslan Bilovol entity_id)) {
2940ba41d91STakashi Iwai usb_audio_err(chip,
2950ba41d91STakashi Iwai "clock source %d is not valid, cannot use\n",
2960ba41d91STakashi Iwai entity_id);
29706ffc1ebSEldad Zack return -ENXIO;
29806ffc1ebSEldad Zack }
29906ffc1ebSEldad Zack return entity_id;
30006ffc1ebSEldad Zack }
30179f920fbSDaniel Mack
302*6aa87001SKarol Kosik selector = snd_usb_find_clock_selector(chip, entity_id, fmt);
30379f920fbSDaniel Mack if (selector) {
3049ec73005STakashi Iwai pins = GET_VAL(selector, proto, bNrInPins);
3059ec73005STakashi Iwai clock_id = GET_VAL(selector, proto, bClockID);
3069ec73005STakashi Iwai sources = GET_VAL(selector, proto, baCSourceID);
307be22db77STakashi Iwai cur = 0;
30879f920fbSDaniel Mack
309eeca59a6SAlexander Tsoy if (proto == UAC_VERSION_3)
310eeca59a6SAlexander Tsoy bmControls = le32_to_cpu(*(__le32 *)(&selector->v3.baCSourceID[0] + pins));
311eeca59a6SAlexander Tsoy else
312eeca59a6SAlexander Tsoy bmControls = *(__u8 *)(&selector->v2.baCSourceID[0] + pins);
313eeca59a6SAlexander Tsoy
314eeca59a6SAlexander Tsoy readable = uac_v2v3_control_is_readable(bmControls,
315eeca59a6SAlexander Tsoy UAC2_CX_CLOCK_SELECTOR);
316eeca59a6SAlexander Tsoy writeable = uac_v2v3_control_is_writeable(bmControls,
317eeca59a6SAlexander Tsoy UAC2_CX_CLOCK_SELECTOR);
318eeca59a6SAlexander Tsoy
3199ec73005STakashi Iwai if (pins == 1) {
320086b957cSTakashi Iwai ret = 1;
321086b957cSTakashi Iwai goto find_source;
322086b957cSTakashi Iwai }
323086b957cSTakashi Iwai
324eeca59a6SAlexander Tsoy /* for now just warn about buggy device */
325eeca59a6SAlexander Tsoy if (!readable)
326eeca59a6SAlexander Tsoy usb_audio_warn(chip,
327eeca59a6SAlexander Tsoy "%s(): clock selector control is not readable, id %d\n",
328eeca59a6SAlexander Tsoy __func__, clock_id);
329eeca59a6SAlexander Tsoy
330882e013aSGeraldo Nascimento /* the entity ID we are looking at is a selector.
33179f920fbSDaniel Mack * find out what it currently selects */
332*6aa87001SKarol Kosik ret = uac_clock_selector_get_val(chip, clock_id, fmt->iface);
333481f17c4STakashi Iwai if (ret < 0) {
334481f17c4STakashi Iwai if (!chip->autoclock)
33579f920fbSDaniel Mack return ret;
336481f17c4STakashi Iwai goto find_others;
337481f17c4STakashi Iwai }
33879f920fbSDaniel Mack
339157a57b6SDaniel Mack /* Selector values are one-based */
340157a57b6SDaniel Mack
3419ec73005STakashi Iwai if (ret > pins || ret < 1) {
3420ba41d91STakashi Iwai usb_audio_err(chip,
34379f920fbSDaniel Mack "%s(): selector reported illegal value, id %d, ret %d\n",
3449ec73005STakashi Iwai __func__, clock_id, ret);
34579f920fbSDaniel Mack
346481f17c4STakashi Iwai if (!chip->autoclock)
34779f920fbSDaniel Mack return -EINVAL;
348481f17c4STakashi Iwai goto find_others;
34979f920fbSDaniel Mack }
35079f920fbSDaniel Mack
351086b957cSTakashi Iwai find_source:
3528c55af3fSEldad Zack cur = ret;
3539f35a312SAlexander Tsoy ret = __uac_clock_find_source(chip, fmt,
3549ec73005STakashi Iwai sources[ret - 1],
35506ffc1ebSEldad Zack visited, validate);
356d2e8f641STakashi Iwai if (ret > 0) {
357f21dca85STakashi Iwai /* Skip setting clock selector again for some devices */
35867794f88SAlexander Tsoy if (chip->quirk_flags & QUIRK_FLAG_SKIP_CLOCK_SELECTOR ||
359c0787fcfSAlexander Tsoy !writeable)
3604511781fSchihhao.chen return ret;
361*6aa87001SKarol Kosik err = uac_clock_selector_set_val(chip, entity_id, cur, fmt->iface);
362eaa1b01fSAlexander Tsoy if (err < 0) {
363eaa1b01fSAlexander Tsoy if (pins == 1) {
364eaa1b01fSAlexander Tsoy usb_audio_dbg(chip,
365eaa1b01fSAlexander Tsoy "%s(): selector returned an error, "
366eaa1b01fSAlexander Tsoy "assuming a firmware bug, id %d, ret %d\n",
367eaa1b01fSAlexander Tsoy __func__, clock_id, err);
368eaa1b01fSAlexander Tsoy return ret;
369eaa1b01fSAlexander Tsoy }
370d2e8f641STakashi Iwai return err;
371d2e8f641STakashi Iwai }
372eaa1b01fSAlexander Tsoy }
373d2e8f641STakashi Iwai
374ef02e29bSEldad Zack if (!validate || ret > 0 || !chip->autoclock)
3758c55af3fSEldad Zack return ret;
3768c55af3fSEldad Zack
377481f17c4STakashi Iwai find_others:
378eeca59a6SAlexander Tsoy if (!writeable)
379eeca59a6SAlexander Tsoy return -ENXIO;
380eeca59a6SAlexander Tsoy
3818c55af3fSEldad Zack /* The current clock source is invalid, try others. */
3829ec73005STakashi Iwai for (i = 1; i <= pins; i++) {
3838c55af3fSEldad Zack if (i == cur)
3848c55af3fSEldad Zack continue;
3858c55af3fSEldad Zack
3869f35a312SAlexander Tsoy ret = __uac_clock_find_source(chip, fmt,
3879ec73005STakashi Iwai sources[i - 1],
3888c55af3fSEldad Zack visited, true);
3898c55af3fSEldad Zack if (ret < 0)
3908c55af3fSEldad Zack continue;
3918c55af3fSEldad Zack
392*6aa87001SKarol Kosik err = uac_clock_selector_set_val(chip, entity_id, i, fmt->iface);
3938c55af3fSEldad Zack if (err < 0)
3948c55af3fSEldad Zack continue;
3958c55af3fSEldad Zack
3960ba41d91STakashi Iwai usb_audio_info(chip,
3970ba41d91STakashi Iwai "found and selected valid clock source %d\n",
3980ba41d91STakashi Iwai ret);
3998c55af3fSEldad Zack return ret;
4008c55af3fSEldad Zack }
4018c55af3fSEldad Zack
4028c55af3fSEldad Zack return -ENXIO;
40379f920fbSDaniel Mack }
40479f920fbSDaniel Mack
40579f920fbSDaniel Mack /* FIXME: multipliers only act as pass-thru element for now */
406*6aa87001SKarol Kosik multiplier = snd_usb_find_clock_multiplier(chip, entity_id, fmt);
40779f920fbSDaniel Mack if (multiplier)
4089f35a312SAlexander Tsoy return __uac_clock_find_source(chip, fmt,
4099ec73005STakashi Iwai GET_VAL(multiplier, proto, bCSourceID),
4109a2fe9b8SRuslan Bilovol visited, validate);
4119a2fe9b8SRuslan Bilovol
4129a2fe9b8SRuslan Bilovol return -EINVAL;
4139a2fe9b8SRuslan Bilovol }
4149a2fe9b8SRuslan Bilovol
415157a57b6SDaniel Mack /*
416157a57b6SDaniel Mack * For all kinds of sample rate settings and other device queries,
417157a57b6SDaniel Mack * the clock source (end-leaf) must be used. However, clock selectors,
418157a57b6SDaniel Mack * clock multipliers and sample rate converters may be specified as
419157a57b6SDaniel Mack * clock source input to terminal. This functions walks the clock path
420157a57b6SDaniel Mack * to its end and tries to find the source.
421157a57b6SDaniel Mack *
422157a57b6SDaniel Mack * The 'visited' bitfield is used internally to detect recursive loops.
423157a57b6SDaniel Mack *
424157a57b6SDaniel Mack * Returns the clock source UnitID (>=0) on success, or an error.
425157a57b6SDaniel Mack */
snd_usb_clock_find_source(struct snd_usb_audio * chip,const struct audioformat * fmt,bool validate)4269f35a312SAlexander Tsoy int snd_usb_clock_find_source(struct snd_usb_audio *chip,
427cab941b7STakashi Iwai const struct audioformat *fmt, bool validate)
42879f920fbSDaniel Mack {
42979f920fbSDaniel Mack DECLARE_BITMAP(visited, 256);
43079f920fbSDaniel Mack memset(visited, 0, sizeof(visited));
4319a2fe9b8SRuslan Bilovol
4329f35a312SAlexander Tsoy switch (fmt->protocol) {
4339a2fe9b8SRuslan Bilovol case UAC_VERSION_2:
4349a2fe9b8SRuslan Bilovol case UAC_VERSION_3:
4359ec73005STakashi Iwai return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
4369a2fe9b8SRuslan Bilovol validate);
4379a2fe9b8SRuslan Bilovol default:
4389a2fe9b8SRuslan Bilovol return -EINVAL;
4399a2fe9b8SRuslan Bilovol }
44079f920fbSDaniel Mack }
44179f920fbSDaniel Mack
set_sample_rate_v1(struct snd_usb_audio * chip,const struct audioformat * fmt,int rate)442953a446bSTakashi Iwai static int set_sample_rate_v1(struct snd_usb_audio *chip,
443cab941b7STakashi Iwai const struct audioformat *fmt, int rate)
44479f920fbSDaniel Mack {
44579f920fbSDaniel Mack struct usb_device *dev = chip->dev;
44679f920fbSDaniel Mack unsigned char data[3];
44779f920fbSDaniel Mack int err, crate;
44879f920fbSDaniel Mack
44979f920fbSDaniel Mack /* if endpoint doesn't have sampling rate control, bail out */
450d32d552eSClemens Ladisch if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
45179f920fbSDaniel Mack return 0;
45279f920fbSDaniel Mack
45379f920fbSDaniel Mack data[0] = rate;
45479f920fbSDaniel Mack data[1] = rate >> 8;
45579f920fbSDaniel Mack data[2] = rate >> 16;
456f25ecf8fSTakashi Iwai err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
45779f920fbSDaniel Mack USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
45887cb9af9STakashi Iwai UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
45987cb9af9STakashi Iwai fmt->endpoint, data, sizeof(data));
460f25ecf8fSTakashi Iwai if (err < 0) {
4610ba41d91STakashi Iwai dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
46287cb9af9STakashi Iwai fmt->iface, fmt->altsetting, rate, fmt->endpoint);
46379f920fbSDaniel Mack return err;
46479f920fbSDaniel Mack }
46579f920fbSDaniel Mack
466b62b9980SJoe Turner /* Don't check the sample rate for devices which we know don't
467b62b9980SJoe Turner * support reading */
4684d4dee0aSTakashi Iwai if (chip->quirk_flags & QUIRK_FLAG_GET_SAMPLE_RATE)
469b62b9980SJoe Turner return 0;
47057dd5414STakashi Iwai /* the firmware is likely buggy, don't repeat to fail too many times */
47157dd5414STakashi Iwai if (chip->sample_rate_read_error > 2)
47257dd5414STakashi Iwai return 0;
473b62b9980SJoe Turner
474f25ecf8fSTakashi Iwai err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
47579f920fbSDaniel Mack USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
47687cb9af9STakashi Iwai UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
47787cb9af9STakashi Iwai fmt->endpoint, data, sizeof(data));
478f25ecf8fSTakashi Iwai if (err < 0) {
4790ba41d91STakashi Iwai dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
48087cb9af9STakashi Iwai fmt->iface, fmt->altsetting, fmt->endpoint);
48157dd5414STakashi Iwai chip->sample_rate_read_error++;
48279f920fbSDaniel Mack return 0; /* some devices don't support reading */
48379f920fbSDaniel Mack }
48479f920fbSDaniel Mack
48579f920fbSDaniel Mack crate = data[0] | (data[1] << 8) | (data[2] << 16);
4869df28edcSTakashi Iwai if (!crate) {
4879df28edcSTakashi Iwai dev_info(&dev->dev, "failed to read current rate; disabling the check\n");
4889df28edcSTakashi Iwai chip->sample_rate_read_error = 3; /* three strikes, see above */
4899df28edcSTakashi Iwai return 0;
4909df28edcSTakashi Iwai }
4919df28edcSTakashi Iwai
49279f920fbSDaniel Mack if (crate != rate) {
4930ba41d91STakashi Iwai dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
49479f920fbSDaniel Mack // runtime->rate = crate;
49579f920fbSDaniel Mack }
49679f920fbSDaniel Mack
49779f920fbSDaniel Mack return 0;
49879f920fbSDaniel Mack }
49979f920fbSDaniel Mack
get_sample_rate_v2v3(struct snd_usb_audio * chip,int iface,int altsetting,int clock)5009a2fe9b8SRuslan Bilovol static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
5017c517465STakashi Iwai int altsetting, int clock)
5027c517465STakashi Iwai {
5037c517465STakashi Iwai struct usb_device *dev = chip->dev;
504f6a8bc70SEldad Zack __le32 data;
5057c517465STakashi Iwai int err;
506*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf;
5077c517465STakashi Iwai
508*6aa87001SKarol Kosik ctrl_intf = snd_usb_find_ctrl_interface(chip, iface);
5097c517465STakashi Iwai err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
5107c517465STakashi Iwai USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
5117c517465STakashi Iwai UAC2_CS_CONTROL_SAM_FREQ << 8,
512*6aa87001SKarol Kosik snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
513f6a8bc70SEldad Zack &data, sizeof(data));
5147c517465STakashi Iwai if (err < 0) {
5159a2fe9b8SRuslan Bilovol dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
5160ba41d91STakashi Iwai iface, altsetting, err);
5177c517465STakashi Iwai return 0;
5187c517465STakashi Iwai }
5197c517465STakashi Iwai
520f6a8bc70SEldad Zack return le32_to_cpu(data);
5217c517465STakashi Iwai }
5227c517465STakashi Iwai
52393db51d0STakashi Iwai /*
52493db51d0STakashi Iwai * Try to set the given sample rate:
52593db51d0STakashi Iwai *
52693db51d0STakashi Iwai * Return 0 if the clock source is read-only, the actual rate on success,
52793db51d0STakashi Iwai * or a negative error code.
52893db51d0STakashi Iwai *
52993db51d0STakashi Iwai * This function gets called from format.c to validate each sample rate, too.
53093db51d0STakashi Iwai * Hence no message is shown upon error
53193db51d0STakashi Iwai */
snd_usb_set_sample_rate_v2v3(struct snd_usb_audio * chip,const struct audioformat * fmt,int clock,int rate)53293db51d0STakashi Iwai int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
53393db51d0STakashi Iwai const struct audioformat *fmt,
53493db51d0STakashi Iwai int clock, int rate)
53593db51d0STakashi Iwai {
53693db51d0STakashi Iwai bool writeable;
53793db51d0STakashi Iwai u32 bmControls;
53893db51d0STakashi Iwai __le32 data;
53993db51d0STakashi Iwai int err;
5409ec73005STakashi Iwai union uac23_clock_source_desc *cs_desc;
541*6aa87001SKarol Kosik struct usb_host_interface *ctrl_intf;
54293db51d0STakashi Iwai
543*6aa87001SKarol Kosik ctrl_intf = snd_usb_find_ctrl_interface(chip, fmt->iface);
544*6aa87001SKarol Kosik cs_desc = snd_usb_find_clock_source(chip, clock, fmt);
545b97053dfSChengfeng Ye
546b97053dfSChengfeng Ye if (!cs_desc)
547b97053dfSChengfeng Ye return 0;
548b97053dfSChengfeng Ye
5499ec73005STakashi Iwai if (fmt->protocol == UAC_VERSION_3)
5509ec73005STakashi Iwai bmControls = le32_to_cpu(cs_desc->v3.bmControls);
5519ec73005STakashi Iwai else
5529ec73005STakashi Iwai bmControls = cs_desc->v2.bmControls;
55393db51d0STakashi Iwai
55493db51d0STakashi Iwai writeable = uac_v2v3_control_is_writeable(bmControls,
55593db51d0STakashi Iwai UAC2_CS_CONTROL_SAM_FREQ);
55693db51d0STakashi Iwai if (!writeable)
55793db51d0STakashi Iwai return 0;
55893db51d0STakashi Iwai
55993db51d0STakashi Iwai data = cpu_to_le32(rate);
56093db51d0STakashi Iwai err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR,
56193db51d0STakashi Iwai USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
56293db51d0STakashi Iwai UAC2_CS_CONTROL_SAM_FREQ << 8,
563*6aa87001SKarol Kosik snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
56493db51d0STakashi Iwai &data, sizeof(data));
56593db51d0STakashi Iwai if (err < 0)
56693db51d0STakashi Iwai return err;
56793db51d0STakashi Iwai
56893db51d0STakashi Iwai return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
56993db51d0STakashi Iwai }
57093db51d0STakashi Iwai
set_sample_rate_v2v3(struct snd_usb_audio * chip,const struct audioformat * fmt,int rate)571953a446bSTakashi Iwai static int set_sample_rate_v2v3(struct snd_usb_audio *chip,
572cab941b7STakashi Iwai const struct audioformat *fmt, int rate)
57379f920fbSDaniel Mack {
57493db51d0STakashi Iwai int cur_rate, prev_rate;
5758c55af3fSEldad Zack int clock;
57679f920fbSDaniel Mack
57758cabe87SAdam Goode /* First, try to find a valid clock. This may trigger
57858cabe87SAdam Goode * automatic clock selection if the current clock is not
57958cabe87SAdam Goode * valid.
58058cabe87SAdam Goode */
5819f35a312SAlexander Tsoy clock = snd_usb_clock_find_source(chip, fmt, true);
58258cabe87SAdam Goode if (clock < 0) {
58358cabe87SAdam Goode /* We did not find a valid clock, but that might be
58458cabe87SAdam Goode * because the current sample rate does not match an
58558cabe87SAdam Goode * external clock source. Try again without validation
58658cabe87SAdam Goode * and we will do another validation after setting the
58758cabe87SAdam Goode * rate.
58858cabe87SAdam Goode */
5899f35a312SAlexander Tsoy clock = snd_usb_clock_find_source(chip, fmt, false);
5907fdabab8SDamien Zammit
591f21dca85STakashi Iwai /* Hardcoded sample rates */
592f21dca85STakashi Iwai if (chip->quirk_flags & QUIRK_FLAG_IGNORE_CLOCK_SOURCE)
5937fdabab8SDamien Zammit return 0;
5947fdabab8SDamien Zammit
59579f920fbSDaniel Mack if (clock < 0)
59679f920fbSDaniel Mack return clock;
59758cabe87SAdam Goode }
59879f920fbSDaniel Mack
599953a446bSTakashi Iwai prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
600fa92dd77SDavid Henningsson if (prev_rate == rate)
60158cabe87SAdam Goode goto validation;
602690a863fSTorstein Hegge
60393db51d0STakashi Iwai cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate);
60493db51d0STakashi Iwai if (cur_rate < 0) {
6050ba41d91STakashi Iwai usb_audio_err(chip,
6069a2fe9b8SRuslan Bilovol "%d:%d: cannot set freq %d (v2/v3): err %d\n",
607953a446bSTakashi Iwai fmt->iface, fmt->altsetting, rate, cur_rate);
60893db51d0STakashi Iwai return cur_rate;
60979f920fbSDaniel Mack }
61079f920fbSDaniel Mack
61193db51d0STakashi Iwai if (!cur_rate)
6121dc669feSEldad Zack cur_rate = prev_rate;
61379f920fbSDaniel Mack
614690a863fSTorstein Hegge if (cur_rate != rate) {
615dcf269b3STakashi Iwai usb_audio_dbg(chip,
616dcf269b3STakashi Iwai "%d:%d: freq mismatch: req %d, clock runs @%d\n",
61793db51d0STakashi Iwai fmt->iface, fmt->altsetting, rate, cur_rate);
618dcf269b3STakashi Iwai /* continue processing */
6191dc669feSEldad Zack }
620690a863fSTorstein Hegge
6215ce0b06aSTakashi Iwai /* FIXME - TEAC devices require the immediate interface setup */
6223753fcc2STakashi Iwai if (USB_ID_VENDOR(chip->usb_id) == 0x0644) {
6233753fcc2STakashi Iwai bool cur_base_48k = (rate % 48000 == 0);
6243753fcc2STakashi Iwai bool prev_base_48k = (prev_rate % 48000 == 0);
6253753fcc2STakashi Iwai if (cur_base_48k != prev_base_48k) {
6265ce0b06aSTakashi Iwai usb_set_interface(chip->dev, fmt->iface, fmt->altsetting);
6275ce0b06aSTakashi Iwai if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
6285ce0b06aSTakashi Iwai msleep(50);
6295ce0b06aSTakashi Iwai }
6303753fcc2STakashi Iwai }
6315ce0b06aSTakashi Iwai
63258cabe87SAdam Goode validation:
63358cabe87SAdam Goode /* validate clock after rate change */
6349f35a312SAlexander Tsoy if (!uac_clock_source_is_valid(chip, fmt, clock))
63558cabe87SAdam Goode return -ENXIO;
63679f920fbSDaniel Mack return 0;
63779f920fbSDaniel Mack }
63879f920fbSDaniel Mack
snd_usb_init_sample_rate(struct snd_usb_audio * chip,const struct audioformat * fmt,int rate)639953a446bSTakashi Iwai int snd_usb_init_sample_rate(struct snd_usb_audio *chip,
640cab941b7STakashi Iwai const struct audioformat *fmt, int rate)
64179f920fbSDaniel Mack {
642bf6313a0STakashi Iwai usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n",
643bf6313a0STakashi Iwai fmt->iface, fmt->altsetting, rate, fmt->clock);
644bf6313a0STakashi Iwai
6458f898e92SClemens Ladisch switch (fmt->protocol) {
64679f920fbSDaniel Mack case UAC_VERSION_1:
647a2acad82SClemens Ladisch default:
648953a446bSTakashi Iwai return set_sample_rate_v1(chip, fmt, rate);
64979f920fbSDaniel Mack
6509a2fe9b8SRuslan Bilovol case UAC_VERSION_3:
65117156f23SRuslan Bilovol if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
65217156f23SRuslan Bilovol if (rate != UAC3_BADD_SAMPLING_RATE)
65317156f23SRuslan Bilovol return -ENXIO;
65417156f23SRuslan Bilovol else
65517156f23SRuslan Bilovol return 0;
65617156f23SRuslan Bilovol }
657c0dbbdadSGustavo A. R. Silva fallthrough;
65817156f23SRuslan Bilovol case UAC_VERSION_2:
659953a446bSTakashi Iwai return set_sample_rate_v2v3(chip, fmt, rate);
66079f920fbSDaniel Mack }
66179f920fbSDaniel Mack }
66279f920fbSDaniel Mack
663