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 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 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 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 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) \ 799ec73005STakashi Iwai static obj *name(struct snd_usb_audio *chip, int id, int proto) \ 80f7645bd6STakashi Iwai { \ 819ec73005STakashi Iwai return find_uac_clock_desc(chip->ctrl_intf, id, validator, \ 829ec73005STakashi Iwai proto == UAC_VERSION_3 ? (type3) : (type2), \ 839ec73005STakashi Iwai proto); \ 84f7645bd6STakashi Iwai } 85f7645bd6STakashi Iwai 86f7645bd6STakashi Iwai DEFINE_FIND_HELPER(snd_usb_find_clock_source, 879ec73005STakashi Iwai union uac23_clock_source_desc, validate_clock_source, 889ec73005STakashi Iwai UAC2_CLOCK_SOURCE, UAC3_CLOCK_SOURCE); 89f7645bd6STakashi Iwai DEFINE_FIND_HELPER(snd_usb_find_clock_selector, 909ec73005STakashi Iwai union uac23_clock_selector_desc, validate_clock_selector, 919ec73005STakashi Iwai UAC2_CLOCK_SELECTOR, UAC3_CLOCK_SELECTOR); 92f7645bd6STakashi Iwai DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier, 939ec73005STakashi Iwai union uac23_clock_multiplier_desc, validate_clock_multiplier, 949ec73005STakashi Iwai UAC2_CLOCK_MULTIPLIER, UAC3_CLOCK_MULTIPLIER); 95f7645bd6STakashi Iwai 9679f920fbSDaniel Mack static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id) 9779f920fbSDaniel Mack { 9879f920fbSDaniel Mack unsigned char buf; 9979f920fbSDaniel Mack int ret; 10079f920fbSDaniel Mack 10179f920fbSDaniel Mack ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), 10279f920fbSDaniel Mack UAC2_CS_CUR, 10379f920fbSDaniel Mack USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 10411bcbc44SDaniel Mack UAC2_CX_CLOCK_SELECTOR << 8, 10511bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (selector_id << 8), 10617d900c4SClemens Ladisch &buf, sizeof(buf)); 10779f920fbSDaniel Mack 10879f920fbSDaniel Mack if (ret < 0) 10979f920fbSDaniel Mack return ret; 11079f920fbSDaniel Mack 11179f920fbSDaniel Mack return buf; 11279f920fbSDaniel Mack } 11379f920fbSDaniel Mack 1148c55af3fSEldad Zack static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id, 1158c55af3fSEldad Zack unsigned char pin) 1168c55af3fSEldad Zack { 1178c55af3fSEldad Zack int ret; 1188c55af3fSEldad Zack 1198c55af3fSEldad Zack ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), 1208c55af3fSEldad Zack UAC2_CS_CUR, 1218c55af3fSEldad Zack USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, 1228c55af3fSEldad Zack UAC2_CX_CLOCK_SELECTOR << 8, 1238c55af3fSEldad Zack snd_usb_ctrl_intf(chip) | (selector_id << 8), 1248c55af3fSEldad Zack &pin, sizeof(pin)); 1258c55af3fSEldad Zack if (ret < 0) 1268c55af3fSEldad Zack return ret; 1278c55af3fSEldad Zack 1288c55af3fSEldad Zack if (ret != sizeof(pin)) { 1290ba41d91STakashi Iwai usb_audio_err(chip, 1300ba41d91STakashi Iwai "setting selector (id %d) unexpected length %d\n", 1310ba41d91STakashi Iwai selector_id, ret); 1328c55af3fSEldad Zack return -EINVAL; 1338c55af3fSEldad Zack } 1348c55af3fSEldad Zack 1358c55af3fSEldad Zack ret = uac_clock_selector_get_val(chip, selector_id); 1368c55af3fSEldad Zack if (ret < 0) 1378c55af3fSEldad Zack return ret; 1388c55af3fSEldad Zack 1398c55af3fSEldad Zack if (ret != pin) { 1400ba41d91STakashi Iwai usb_audio_err(chip, 1410ba41d91STakashi Iwai "setting selector (id %d) to %x failed (current: %d)\n", 1420ba41d91STakashi Iwai selector_id, pin, ret); 1438c55af3fSEldad Zack return -EINVAL; 1448c55af3fSEldad Zack } 1458c55af3fSEldad Zack 1468c55af3fSEldad Zack return ret; 1478c55af3fSEldad Zack } 1488c55af3fSEldad Zack 1499f35a312SAlexander Tsoy static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip, 150cab941b7STakashi Iwai const struct audioformat *fmt, 1519f35a312SAlexander Tsoy int source_id) 1529f35a312SAlexander Tsoy { 1532edb84e3SAlexander Tsoy bool ret = false; 1542edb84e3SAlexander Tsoy int count; 1552edb84e3SAlexander Tsoy unsigned char data; 1562edb84e3SAlexander Tsoy struct usb_device *dev = chip->dev; 1579ec73005STakashi Iwai union uac23_clock_source_desc *cs_desc; 1582edb84e3SAlexander Tsoy 1599ec73005STakashi Iwai cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol); 1609f35a312SAlexander Tsoy if (!cs_desc) 1619f35a312SAlexander Tsoy return false; 1629f35a312SAlexander Tsoy 1639ec73005STakashi Iwai if (fmt->protocol == UAC_VERSION_2) { 1642edb84e3SAlexander Tsoy /* 1652edb84e3SAlexander Tsoy * Assume the clock is valid if clock source supports only one 1662edb84e3SAlexander Tsoy * single sample rate, the terminal is connected directly to it 1672edb84e3SAlexander Tsoy * (there is no clock selector) and clock type is internal. 1682edb84e3SAlexander Tsoy * This is to deal with some Denon DJ controllers that always 1692edb84e3SAlexander Tsoy * reports that clock is invalid. 1702edb84e3SAlexander Tsoy */ 1712edb84e3SAlexander Tsoy if (fmt->nr_rates == 1 && 1729ec73005STakashi Iwai (fmt->clock & 0xff) == cs_desc->v2.bClockID && 1739ec73005STakashi Iwai (cs_desc->v2.bmAttributes & 0x3) != 1742edb84e3SAlexander Tsoy UAC_CLOCK_SOURCE_TYPE_EXT) 1752edb84e3SAlexander Tsoy return true; 1769f35a312SAlexander Tsoy } 1779f35a312SAlexander Tsoy 1782edb84e3SAlexander Tsoy /* 1792edb84e3SAlexander Tsoy * MOTU MicroBook IIc 1802edb84e3SAlexander Tsoy * Sample rate changes takes more than 2 seconds for this device. Clock 1812edb84e3SAlexander Tsoy * validity request returns false during that period. 1822edb84e3SAlexander Tsoy */ 1832edb84e3SAlexander Tsoy if (chip->usb_id == USB_ID(0x07fd, 0x0004)) { 1842edb84e3SAlexander Tsoy count = 0; 1852edb84e3SAlexander Tsoy 1862edb84e3SAlexander Tsoy while ((!ret) && (count < 50)) { 1872edb84e3SAlexander Tsoy int err; 1882edb84e3SAlexander Tsoy 1892edb84e3SAlexander Tsoy msleep(100); 1902edb84e3SAlexander Tsoy 1912edb84e3SAlexander Tsoy err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, 1922edb84e3SAlexander Tsoy USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 1932edb84e3SAlexander Tsoy UAC2_CS_CONTROL_CLOCK_VALID << 8, 1942edb84e3SAlexander Tsoy snd_usb_ctrl_intf(chip) | (source_id << 8), 1952edb84e3SAlexander Tsoy &data, sizeof(data)); 1962edb84e3SAlexander Tsoy if (err < 0) { 1972edb84e3SAlexander Tsoy dev_warn(&dev->dev, 1982edb84e3SAlexander Tsoy "%s(): cannot get clock validity for id %d\n", 1992edb84e3SAlexander Tsoy __func__, source_id); 2009f35a312SAlexander Tsoy return false; 2019f35a312SAlexander Tsoy } 2029f35a312SAlexander Tsoy 2032edb84e3SAlexander Tsoy ret = !!data; 2042edb84e3SAlexander Tsoy count++; 2052edb84e3SAlexander Tsoy } 2062edb84e3SAlexander Tsoy } 2072edb84e3SAlexander Tsoy 2082edb84e3SAlexander Tsoy return ret; 2092edb84e3SAlexander Tsoy } 2102edb84e3SAlexander Tsoy 2119a2fe9b8SRuslan Bilovol static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, 212cab941b7STakashi Iwai const struct audioformat *fmt, 2139a2fe9b8SRuslan Bilovol int source_id) 21479f920fbSDaniel Mack { 21579f920fbSDaniel Mack int err; 21679f920fbSDaniel Mack unsigned char data; 21779f920fbSDaniel Mack struct usb_device *dev = chip->dev; 2189a2fe9b8SRuslan Bilovol u32 bmControls; 2199ec73005STakashi Iwai union uac23_clock_source_desc *cs_desc; 2209a2fe9b8SRuslan Bilovol 2219ec73005STakashi Iwai cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol); 2229a2fe9b8SRuslan Bilovol if (!cs_desc) 2231d4961d9SSaurav Girepunje return false; 2243bc6fbc7SDaniel Mack 2259ec73005STakashi Iwai if (fmt->protocol == UAC_VERSION_3) 2269ec73005STakashi Iwai bmControls = le32_to_cpu(cs_desc->v3.bmControls); 2279ec73005STakashi Iwai else 2289ec73005STakashi Iwai bmControls = cs_desc->v2.bmControls; 2293bc6fbc7SDaniel Mack 2303bc6fbc7SDaniel Mack /* If a clock source can't tell us whether it's valid, we assume it is */ 2319a2fe9b8SRuslan Bilovol if (!uac_v2v3_control_is_readable(bmControls, 23221e9b3e9SAndrew Chant UAC2_CS_CONTROL_CLOCK_VALID)) 2331d4961d9SSaurav Girepunje return true; 23479f920fbSDaniel Mack 23579f920fbSDaniel Mack err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, 23679f920fbSDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 23711bcbc44SDaniel Mack UAC2_CS_CONTROL_CLOCK_VALID << 8, 23811bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (source_id << 8), 23917d900c4SClemens Ladisch &data, sizeof(data)); 24079f920fbSDaniel Mack 24179f920fbSDaniel Mack if (err < 0) { 2420ba41d91STakashi Iwai dev_warn(&dev->dev, 2430ba41d91STakashi Iwai "%s(): cannot get clock validity for id %d\n", 24479f920fbSDaniel Mack __func__, source_id); 2451d4961d9SSaurav Girepunje return false; 24679f920fbSDaniel Mack } 24779f920fbSDaniel Mack 2489f35a312SAlexander Tsoy if (data) 2499f35a312SAlexander Tsoy return true; 2509f35a312SAlexander Tsoy else 2519f35a312SAlexander Tsoy return uac_clock_source_is_valid_quirk(chip, fmt, source_id); 25279f920fbSDaniel Mack } 25379f920fbSDaniel Mack 2549f35a312SAlexander Tsoy static int __uac_clock_find_source(struct snd_usb_audio *chip, 255cab941b7STakashi Iwai const struct audioformat *fmt, int entity_id, 2569a2fe9b8SRuslan Bilovol unsigned long *visited, bool validate) 25779f920fbSDaniel Mack { 2589ec73005STakashi Iwai union uac23_clock_source_desc *source; 2599ec73005STakashi Iwai union uac23_clock_selector_desc *selector; 2609ec73005STakashi Iwai union uac23_clock_multiplier_desc *multiplier; 2619ec73005STakashi Iwai int ret, i, cur, err, pins, clock_id; 2629ec73005STakashi Iwai const u8 *sources; 2639ec73005STakashi Iwai int proto = fmt->protocol; 26479f920fbSDaniel Mack 26579f920fbSDaniel Mack entity_id &= 0xff; 26679f920fbSDaniel Mack 26779f920fbSDaniel Mack if (test_and_set_bit(entity_id, visited)) { 2680ba41d91STakashi Iwai usb_audio_warn(chip, 26979f920fbSDaniel Mack "%s(): recursive clock topology detected, id %d.\n", 27079f920fbSDaniel Mack __func__, entity_id); 27179f920fbSDaniel Mack return -EINVAL; 27279f920fbSDaniel Mack } 27379f920fbSDaniel Mack 27479f920fbSDaniel Mack /* first, see if the ID we're looking for is a clock source already */ 2759ec73005STakashi Iwai source = snd_usb_find_clock_source(chip, entity_id, proto); 27606ffc1ebSEldad Zack if (source) { 2779ec73005STakashi Iwai entity_id = GET_VAL(source, proto, bClockID); 2789f35a312SAlexander Tsoy if (validate && !uac_clock_source_is_valid(chip, fmt, 2799a2fe9b8SRuslan Bilovol entity_id)) { 2800ba41d91STakashi Iwai usb_audio_err(chip, 2810ba41d91STakashi Iwai "clock source %d is not valid, cannot use\n", 2820ba41d91STakashi Iwai entity_id); 28306ffc1ebSEldad Zack return -ENXIO; 28406ffc1ebSEldad Zack } 28506ffc1ebSEldad Zack return entity_id; 28606ffc1ebSEldad Zack } 28779f920fbSDaniel Mack 2889ec73005STakashi Iwai selector = snd_usb_find_clock_selector(chip, entity_id, proto); 28979f920fbSDaniel Mack if (selector) { 2909ec73005STakashi Iwai pins = GET_VAL(selector, proto, bNrInPins); 2919ec73005STakashi Iwai clock_id = GET_VAL(selector, proto, bClockID); 2929ec73005STakashi Iwai sources = GET_VAL(selector, proto, baCSourceID); 293*be22db77STakashi Iwai cur = 0; 29479f920fbSDaniel Mack 2959ec73005STakashi Iwai if (pins == 1) { 296086b957cSTakashi Iwai ret = 1; 297086b957cSTakashi Iwai goto find_source; 298086b957cSTakashi Iwai } 299086b957cSTakashi Iwai 30079f920fbSDaniel Mack /* the entity ID we are looking for is a selector. 30179f920fbSDaniel Mack * find out what it currently selects */ 3029ec73005STakashi Iwai ret = uac_clock_selector_get_val(chip, clock_id); 303481f17c4STakashi Iwai if (ret < 0) { 304481f17c4STakashi Iwai if (!chip->autoclock) 30579f920fbSDaniel Mack return ret; 306481f17c4STakashi Iwai goto find_others; 307481f17c4STakashi Iwai } 30879f920fbSDaniel Mack 309157a57b6SDaniel Mack /* Selector values are one-based */ 310157a57b6SDaniel Mack 3119ec73005STakashi Iwai if (ret > pins || ret < 1) { 3120ba41d91STakashi Iwai usb_audio_err(chip, 31379f920fbSDaniel Mack "%s(): selector reported illegal value, id %d, ret %d\n", 3149ec73005STakashi Iwai __func__, clock_id, ret); 31579f920fbSDaniel Mack 316481f17c4STakashi Iwai if (!chip->autoclock) 31779f920fbSDaniel Mack return -EINVAL; 318481f17c4STakashi Iwai goto find_others; 31979f920fbSDaniel Mack } 32079f920fbSDaniel Mack 321086b957cSTakashi Iwai find_source: 3228c55af3fSEldad Zack cur = ret; 3239f35a312SAlexander Tsoy ret = __uac_clock_find_source(chip, fmt, 3249ec73005STakashi Iwai sources[ret - 1], 32506ffc1ebSEldad Zack visited, validate); 326d2e8f641STakashi Iwai if (ret > 0) { 327d2e8f641STakashi Iwai err = uac_clock_selector_set_val(chip, entity_id, cur); 328d2e8f641STakashi Iwai if (err < 0) 329d2e8f641STakashi Iwai return err; 330d2e8f641STakashi Iwai } 331d2e8f641STakashi Iwai 332ef02e29bSEldad Zack if (!validate || ret > 0 || !chip->autoclock) 3338c55af3fSEldad Zack return ret; 3348c55af3fSEldad Zack 335481f17c4STakashi Iwai find_others: 3368c55af3fSEldad Zack /* The current clock source is invalid, try others. */ 3379ec73005STakashi Iwai for (i = 1; i <= pins; i++) { 3388c55af3fSEldad Zack if (i == cur) 3398c55af3fSEldad Zack continue; 3408c55af3fSEldad Zack 3419f35a312SAlexander Tsoy ret = __uac_clock_find_source(chip, fmt, 3429ec73005STakashi Iwai sources[i - 1], 3438c55af3fSEldad Zack visited, true); 3448c55af3fSEldad Zack if (ret < 0) 3458c55af3fSEldad Zack continue; 3468c55af3fSEldad Zack 3478c55af3fSEldad Zack err = uac_clock_selector_set_val(chip, entity_id, i); 3488c55af3fSEldad Zack if (err < 0) 3498c55af3fSEldad Zack continue; 3508c55af3fSEldad Zack 3510ba41d91STakashi Iwai usb_audio_info(chip, 3520ba41d91STakashi Iwai "found and selected valid clock source %d\n", 3530ba41d91STakashi Iwai ret); 3548c55af3fSEldad Zack return ret; 3558c55af3fSEldad Zack } 3568c55af3fSEldad Zack 3578c55af3fSEldad Zack return -ENXIO; 35879f920fbSDaniel Mack } 35979f920fbSDaniel Mack 36079f920fbSDaniel Mack /* FIXME: multipliers only act as pass-thru element for now */ 3619ec73005STakashi Iwai multiplier = snd_usb_find_clock_multiplier(chip, entity_id, proto); 36279f920fbSDaniel Mack if (multiplier) 3639f35a312SAlexander Tsoy return __uac_clock_find_source(chip, fmt, 3649ec73005STakashi Iwai GET_VAL(multiplier, proto, bCSourceID), 3659a2fe9b8SRuslan Bilovol visited, validate); 3669a2fe9b8SRuslan Bilovol 3679a2fe9b8SRuslan Bilovol return -EINVAL; 3689a2fe9b8SRuslan Bilovol } 3699a2fe9b8SRuslan Bilovol 370157a57b6SDaniel Mack /* 371157a57b6SDaniel Mack * For all kinds of sample rate settings and other device queries, 372157a57b6SDaniel Mack * the clock source (end-leaf) must be used. However, clock selectors, 373157a57b6SDaniel Mack * clock multipliers and sample rate converters may be specified as 374157a57b6SDaniel Mack * clock source input to terminal. This functions walks the clock path 375157a57b6SDaniel Mack * to its end and tries to find the source. 376157a57b6SDaniel Mack * 377157a57b6SDaniel Mack * The 'visited' bitfield is used internally to detect recursive loops. 378157a57b6SDaniel Mack * 379157a57b6SDaniel Mack * Returns the clock source UnitID (>=0) on success, or an error. 380157a57b6SDaniel Mack */ 3819f35a312SAlexander Tsoy int snd_usb_clock_find_source(struct snd_usb_audio *chip, 382cab941b7STakashi Iwai const struct audioformat *fmt, bool validate) 38379f920fbSDaniel Mack { 38479f920fbSDaniel Mack DECLARE_BITMAP(visited, 256); 38579f920fbSDaniel Mack memset(visited, 0, sizeof(visited)); 3869a2fe9b8SRuslan Bilovol 3879f35a312SAlexander Tsoy switch (fmt->protocol) { 3889a2fe9b8SRuslan Bilovol case UAC_VERSION_2: 3899a2fe9b8SRuslan Bilovol case UAC_VERSION_3: 3909ec73005STakashi Iwai return __uac_clock_find_source(chip, fmt, fmt->clock, visited, 3919a2fe9b8SRuslan Bilovol validate); 3929a2fe9b8SRuslan Bilovol default: 3939a2fe9b8SRuslan Bilovol return -EINVAL; 3949a2fe9b8SRuslan Bilovol } 39579f920fbSDaniel Mack } 39679f920fbSDaniel Mack 397953a446bSTakashi Iwai static int set_sample_rate_v1(struct snd_usb_audio *chip, 398cab941b7STakashi Iwai const struct audioformat *fmt, int rate) 39979f920fbSDaniel Mack { 40079f920fbSDaniel Mack struct usb_device *dev = chip->dev; 40179f920fbSDaniel Mack unsigned char data[3]; 40279f920fbSDaniel Mack int err, crate; 40379f920fbSDaniel Mack 40479f920fbSDaniel Mack /* if endpoint doesn't have sampling rate control, bail out */ 405d32d552eSClemens Ladisch if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) 40679f920fbSDaniel Mack return 0; 40779f920fbSDaniel Mack 40879f920fbSDaniel Mack data[0] = rate; 40979f920fbSDaniel Mack data[1] = rate >> 8; 41079f920fbSDaniel Mack data[2] = rate >> 16; 411f25ecf8fSTakashi Iwai err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, 41279f920fbSDaniel Mack USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, 41387cb9af9STakashi Iwai UAC_EP_CS_ATTR_SAMPLE_RATE << 8, 41487cb9af9STakashi Iwai fmt->endpoint, data, sizeof(data)); 415f25ecf8fSTakashi Iwai if (err < 0) { 4160ba41d91STakashi Iwai dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n", 41787cb9af9STakashi Iwai fmt->iface, fmt->altsetting, rate, fmt->endpoint); 41879f920fbSDaniel Mack return err; 41979f920fbSDaniel Mack } 42079f920fbSDaniel Mack 421b62b9980SJoe Turner /* Don't check the sample rate for devices which we know don't 422b62b9980SJoe Turner * support reading */ 423b62b9980SJoe Turner if (snd_usb_get_sample_rate_quirk(chip)) 424b62b9980SJoe Turner return 0; 42557dd5414STakashi Iwai /* the firmware is likely buggy, don't repeat to fail too many times */ 42657dd5414STakashi Iwai if (chip->sample_rate_read_error > 2) 42757dd5414STakashi Iwai return 0; 428b62b9980SJoe Turner 429f25ecf8fSTakashi Iwai err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR, 43079f920fbSDaniel Mack USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN, 43187cb9af9STakashi Iwai UAC_EP_CS_ATTR_SAMPLE_RATE << 8, 43287cb9af9STakashi Iwai fmt->endpoint, data, sizeof(data)); 433f25ecf8fSTakashi Iwai if (err < 0) { 4340ba41d91STakashi Iwai dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n", 43587cb9af9STakashi Iwai fmt->iface, fmt->altsetting, fmt->endpoint); 43657dd5414STakashi Iwai chip->sample_rate_read_error++; 43779f920fbSDaniel Mack return 0; /* some devices don't support reading */ 43879f920fbSDaniel Mack } 43979f920fbSDaniel Mack 44079f920fbSDaniel Mack crate = data[0] | (data[1] << 8) | (data[2] << 16); 4419df28edcSTakashi Iwai if (!crate) { 4429df28edcSTakashi Iwai dev_info(&dev->dev, "failed to read current rate; disabling the check\n"); 4439df28edcSTakashi Iwai chip->sample_rate_read_error = 3; /* three strikes, see above */ 4449df28edcSTakashi Iwai return 0; 4459df28edcSTakashi Iwai } 4469df28edcSTakashi Iwai 44779f920fbSDaniel Mack if (crate != rate) { 4480ba41d91STakashi Iwai dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate); 44979f920fbSDaniel Mack // runtime->rate = crate; 45079f920fbSDaniel Mack } 45179f920fbSDaniel Mack 45279f920fbSDaniel Mack return 0; 45379f920fbSDaniel Mack } 45479f920fbSDaniel Mack 4559a2fe9b8SRuslan Bilovol static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface, 4567c517465STakashi Iwai int altsetting, int clock) 4577c517465STakashi Iwai { 4587c517465STakashi Iwai struct usb_device *dev = chip->dev; 459f6a8bc70SEldad Zack __le32 data; 4607c517465STakashi Iwai int err; 4617c517465STakashi Iwai 4627c517465STakashi Iwai err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, 4637c517465STakashi Iwai USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 4647c517465STakashi Iwai UAC2_CS_CONTROL_SAM_FREQ << 8, 4657c517465STakashi Iwai snd_usb_ctrl_intf(chip) | (clock << 8), 466f6a8bc70SEldad Zack &data, sizeof(data)); 4677c517465STakashi Iwai if (err < 0) { 4689a2fe9b8SRuslan Bilovol dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n", 4690ba41d91STakashi Iwai iface, altsetting, err); 4707c517465STakashi Iwai return 0; 4717c517465STakashi Iwai } 4727c517465STakashi Iwai 473f6a8bc70SEldad Zack return le32_to_cpu(data); 4747c517465STakashi Iwai } 4757c517465STakashi Iwai 47693db51d0STakashi Iwai /* 47793db51d0STakashi Iwai * Try to set the given sample rate: 47893db51d0STakashi Iwai * 47993db51d0STakashi Iwai * Return 0 if the clock source is read-only, the actual rate on success, 48093db51d0STakashi Iwai * or a negative error code. 48193db51d0STakashi Iwai * 48293db51d0STakashi Iwai * This function gets called from format.c to validate each sample rate, too. 48393db51d0STakashi Iwai * Hence no message is shown upon error 48493db51d0STakashi Iwai */ 48593db51d0STakashi Iwai int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip, 48693db51d0STakashi Iwai const struct audioformat *fmt, 48793db51d0STakashi Iwai int clock, int rate) 48893db51d0STakashi Iwai { 48993db51d0STakashi Iwai bool writeable; 49093db51d0STakashi Iwai u32 bmControls; 49193db51d0STakashi Iwai __le32 data; 49293db51d0STakashi Iwai int err; 4939ec73005STakashi Iwai union uac23_clock_source_desc *cs_desc; 49493db51d0STakashi Iwai 4959ec73005STakashi Iwai cs_desc = snd_usb_find_clock_source(chip, clock, fmt->protocol); 4969ec73005STakashi Iwai if (fmt->protocol == UAC_VERSION_3) 4979ec73005STakashi Iwai bmControls = le32_to_cpu(cs_desc->v3.bmControls); 4989ec73005STakashi Iwai else 4999ec73005STakashi Iwai bmControls = cs_desc->v2.bmControls; 50093db51d0STakashi Iwai 50193db51d0STakashi Iwai writeable = uac_v2v3_control_is_writeable(bmControls, 50293db51d0STakashi Iwai UAC2_CS_CONTROL_SAM_FREQ); 50393db51d0STakashi Iwai if (!writeable) 50493db51d0STakashi Iwai return 0; 50593db51d0STakashi Iwai 50693db51d0STakashi Iwai data = cpu_to_le32(rate); 50793db51d0STakashi Iwai err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR, 50893db51d0STakashi Iwai USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, 50993db51d0STakashi Iwai UAC2_CS_CONTROL_SAM_FREQ << 8, 51093db51d0STakashi Iwai snd_usb_ctrl_intf(chip) | (clock << 8), 51193db51d0STakashi Iwai &data, sizeof(data)); 51293db51d0STakashi Iwai if (err < 0) 51393db51d0STakashi Iwai return err; 51493db51d0STakashi Iwai 51593db51d0STakashi Iwai return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock); 51693db51d0STakashi Iwai } 51793db51d0STakashi Iwai 518953a446bSTakashi Iwai static int set_sample_rate_v2v3(struct snd_usb_audio *chip, 519cab941b7STakashi Iwai const struct audioformat *fmt, int rate) 52079f920fbSDaniel Mack { 52193db51d0STakashi Iwai int cur_rate, prev_rate; 5228c55af3fSEldad Zack int clock; 52379f920fbSDaniel Mack 52458cabe87SAdam Goode /* First, try to find a valid clock. This may trigger 52558cabe87SAdam Goode * automatic clock selection if the current clock is not 52658cabe87SAdam Goode * valid. 52758cabe87SAdam Goode */ 5289f35a312SAlexander Tsoy clock = snd_usb_clock_find_source(chip, fmt, true); 52958cabe87SAdam Goode if (clock < 0) { 53058cabe87SAdam Goode /* We did not find a valid clock, but that might be 53158cabe87SAdam Goode * because the current sample rate does not match an 53258cabe87SAdam Goode * external clock source. Try again without validation 53358cabe87SAdam Goode * and we will do another validation after setting the 53458cabe87SAdam Goode * rate. 53558cabe87SAdam Goode */ 5369f35a312SAlexander Tsoy clock = snd_usb_clock_find_source(chip, fmt, false); 53779f920fbSDaniel Mack if (clock < 0) 53879f920fbSDaniel Mack return clock; 53958cabe87SAdam Goode } 54079f920fbSDaniel Mack 541953a446bSTakashi Iwai prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock); 542fa92dd77SDavid Henningsson if (prev_rate == rate) 54358cabe87SAdam Goode goto validation; 544690a863fSTorstein Hegge 54593db51d0STakashi Iwai cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate); 54693db51d0STakashi Iwai if (cur_rate < 0) { 5470ba41d91STakashi Iwai usb_audio_err(chip, 5489a2fe9b8SRuslan Bilovol "%d:%d: cannot set freq %d (v2/v3): err %d\n", 549953a446bSTakashi Iwai fmt->iface, fmt->altsetting, rate, cur_rate); 55093db51d0STakashi Iwai return cur_rate; 55179f920fbSDaniel Mack } 55279f920fbSDaniel Mack 55393db51d0STakashi Iwai if (!cur_rate) 5541dc669feSEldad Zack cur_rate = prev_rate; 55579f920fbSDaniel Mack 556690a863fSTorstein Hegge if (cur_rate != rate) { 557dcf269b3STakashi Iwai usb_audio_dbg(chip, 558dcf269b3STakashi Iwai "%d:%d: freq mismatch: req %d, clock runs @%d\n", 55993db51d0STakashi Iwai fmt->iface, fmt->altsetting, rate, cur_rate); 560dcf269b3STakashi Iwai /* continue processing */ 5611dc669feSEldad Zack } 562690a863fSTorstein Hegge 56358cabe87SAdam Goode validation: 56458cabe87SAdam Goode /* validate clock after rate change */ 5659f35a312SAlexander Tsoy if (!uac_clock_source_is_valid(chip, fmt, clock)) 56658cabe87SAdam Goode return -ENXIO; 56779f920fbSDaniel Mack return 0; 56879f920fbSDaniel Mack } 56979f920fbSDaniel Mack 570953a446bSTakashi Iwai int snd_usb_init_sample_rate(struct snd_usb_audio *chip, 571cab941b7STakashi Iwai const struct audioformat *fmt, int rate) 57279f920fbSDaniel Mack { 573bf6313a0STakashi Iwai usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n", 574bf6313a0STakashi Iwai fmt->iface, fmt->altsetting, rate, fmt->clock); 575bf6313a0STakashi Iwai 5768f898e92SClemens Ladisch switch (fmt->protocol) { 57779f920fbSDaniel Mack case UAC_VERSION_1: 578a2acad82SClemens Ladisch default: 579953a446bSTakashi Iwai return set_sample_rate_v1(chip, fmt, rate); 58079f920fbSDaniel Mack 5819a2fe9b8SRuslan Bilovol case UAC_VERSION_3: 58217156f23SRuslan Bilovol if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { 58317156f23SRuslan Bilovol if (rate != UAC3_BADD_SAMPLING_RATE) 58417156f23SRuslan Bilovol return -ENXIO; 58517156f23SRuslan Bilovol else 58617156f23SRuslan Bilovol return 0; 58717156f23SRuslan Bilovol } 588c0dbbdadSGustavo A. R. Silva fallthrough; 58917156f23SRuslan Bilovol case UAC_VERSION_2: 590953a446bSTakashi Iwai return set_sample_rate_v2v3(chip, fmt, rate); 59179f920fbSDaniel Mack } 59279f920fbSDaniel Mack } 59379f920fbSDaniel Mack 594