2 * Clock domain and sample rate management functions
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/bitops.h>
21 #include <linux/init.h>
22 #include <linux/string.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 #include <linux/usb/audio-v2.h>
26 #include <linux/usb/audio-v3.h>
28 #include <sound/core.h>
29 #include <sound/info.h>
30 #include <sound/pcm.h>
38 static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
39 bool (*validator)(void *, int), u8 type)
43 while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
45 if (validator(cs, id))
52 static bool validate_clock_source_v2(void *p, int id)
54 struct uac_clock_source_descriptor *cs = p;
55 return cs->bClockID == id;
58 static bool validate_clock_source_v3(void *p, int id)
60 struct uac3_clock_source_descriptor *cs = p;
61 return cs->bClockID == id;
64 static bool validate_clock_selector_v2(void *p, int id)
66 struct uac_clock_selector_descriptor *cs = p;
67 return cs->bClockID == id;
70 static bool validate_clock_selector_v3(void *p, int id)
72 struct uac3_clock_selector_descriptor *cs = p;
73 return cs->bClockID == id;
76 static bool validate_clock_multiplier_v2(void *p, int id)
78 struct uac_clock_multiplier_descriptor *cs = p;
79 return cs->bClockID == id;
82 static bool validate_clock_multiplier_v3(void *p, int id)
84 struct uac3_clock_multiplier_descriptor *cs = p;
85 return cs->bClockID == id;
88 #define DEFINE_FIND_HELPER(name, obj, validator, type) \
89 static obj *name(struct usb_host_interface *iface, int id) \
91 return find_uac_clock_desc(iface, id, validator, type); \
94 DEFINE_FIND_HELPER(snd_usb_find_clock_source,
95 struct uac_clock_source_descriptor,
96 validate_clock_source_v2, UAC2_CLOCK_SOURCE);
97 DEFINE_FIND_HELPER(snd_usb_find_clock_source_v3,
98 struct uac3_clock_source_descriptor,
99 validate_clock_source_v3, UAC3_CLOCK_SOURCE);
101 DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
102 struct uac_clock_selector_descriptor,
103 validate_clock_selector_v2, UAC2_CLOCK_SELECTOR);
104 DEFINE_FIND_HELPER(snd_usb_find_clock_selector_v3,
105 struct uac3_clock_selector_descriptor,
106 validate_clock_selector_v3, UAC3_CLOCK_SELECTOR);
108 DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
109 struct uac_clock_multiplier_descriptor,
110 validate_clock_multiplier_v2, UAC2_CLOCK_MULTIPLIER);
111 DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier_v3,
112 struct uac3_clock_multiplier_descriptor,
113 validate_clock_multiplier_v3, UAC3_CLOCK_MULTIPLIER);
115 static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
120 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
122 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
123 UAC2_CX_CLOCK_SELECTOR << 8,
124 snd_usb_ctrl_intf(chip) | (selector_id << 8),
133 static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
138 ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
140 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
141 UAC2_CX_CLOCK_SELECTOR << 8,
142 snd_usb_ctrl_intf(chip) | (selector_id << 8),
147 if (ret != sizeof(pin)) {
149 "setting selector (id %d) unexpected length %d\n",
154 ret = uac_clock_selector_get_val(chip, selector_id);
160 "setting selector (id %d) to %x failed (current: %d)\n",
161 selector_id, pin, ret);
169 * Assume the clock is valid if clock source supports only one single sample
170 * rate, the terminal is connected directly to it (there is no clock selector)
171 * and clock type is internal. This is to deal with some Denon DJ controllers
172 * that always reports that clock is invalid.
174 static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
175 struct audioformat *fmt,
178 if (fmt->protocol == UAC_VERSION_2) {
179 struct uac_clock_source_descriptor *cs_desc =
180 snd_usb_find_clock_source(chip->ctrl_intf, source_id);
185 return (fmt->nr_rates == 1 &&
186 (fmt->clock & 0xff) == cs_desc->bClockID &&
187 (cs_desc->bmAttributes & 0x3) !=
188 UAC_CLOCK_SOURCE_TYPE_EXT);
194 static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
195 struct audioformat *fmt,
200 struct usb_device *dev = chip->dev;
203 if (fmt->protocol == UAC_VERSION_3) {
204 struct uac3_clock_source_descriptor *cs_desc =
205 snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);
209 bmControls = le32_to_cpu(cs_desc->bmControls);
210 } else { /* UAC_VERSION_1/2 */
211 struct uac_clock_source_descriptor *cs_desc =
212 snd_usb_find_clock_source(chip->ctrl_intf, source_id);
216 bmControls = cs_desc->bmControls;
219 /* If a clock source can't tell us whether it's valid, we assume it is */
220 if (!uac_v2v3_control_is_readable(bmControls,
221 UAC2_CS_CONTROL_CLOCK_VALID))
224 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
225 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
226 UAC2_CS_CONTROL_CLOCK_VALID << 8,
227 snd_usb_ctrl_intf(chip) | (source_id << 8),
228 &data, sizeof(data));
232 "%s(): cannot get clock validity for id %d\n",
233 __func__, source_id);
240 return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
243 static int __uac_clock_find_source(struct snd_usb_audio *chip,
244 struct audioformat *fmt, int entity_id,
245 unsigned long *visited, bool validate)
247 struct uac_clock_source_descriptor *source;
248 struct uac_clock_selector_descriptor *selector;
249 struct uac_clock_multiplier_descriptor *multiplier;
253 if (test_and_set_bit(entity_id, visited)) {
255 "%s(): recursive clock topology detected, id %d.\n",
256 __func__, entity_id);
260 /* first, see if the ID we're looking for is a clock source already */
261 source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
263 entity_id = source->bClockID;
264 if (validate && !uac_clock_source_is_valid(chip, fmt,
267 "clock source %d is not valid, cannot use\n",
274 selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
276 int ret, i, cur, err;
278 /* the entity ID we are looking for is a selector.
279 * find out what it currently selects */
280 ret = uac_clock_selector_get_val(chip, selector->bClockID);
284 /* Selector values are one-based */
286 if (ret > selector->bNrInPins || ret < 1) {
288 "%s(): selector reported illegal value, id %d, ret %d\n",
289 __func__, selector->bClockID, ret);
295 ret = __uac_clock_find_source(chip, fmt,
296 selector->baCSourceID[ret - 1],
300 * For Samsung USBC Headset (AKG), setting clock selector again
301 * will result in incorrect default clock setting problems
303 if (chip->usb_id == USB_ID(0x04e8, 0xa051))
305 err = uac_clock_selector_set_val(chip, entity_id, cur);
310 if (!validate || ret > 0 || !chip->autoclock)
313 /* The current clock source is invalid, try others. */
314 for (i = 1; i <= selector->bNrInPins; i++) {
318 ret = __uac_clock_find_source(chip, fmt,
319 selector->baCSourceID[i - 1],
324 err = uac_clock_selector_set_val(chip, entity_id, i);
329 "found and selected valid clock source %d\n",
337 /* FIXME: multipliers only act as pass-thru element for now */
338 multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
340 return __uac_clock_find_source(chip, fmt,
341 multiplier->bCSourceID,
347 static int __uac3_clock_find_source(struct snd_usb_audio *chip,
348 struct audioformat *fmt, int entity_id,
349 unsigned long *visited, bool validate)
351 struct uac3_clock_source_descriptor *source;
352 struct uac3_clock_selector_descriptor *selector;
353 struct uac3_clock_multiplier_descriptor *multiplier;
357 if (test_and_set_bit(entity_id, visited)) {
359 "%s(): recursive clock topology detected, id %d.\n",
360 __func__, entity_id);
364 /* first, see if the ID we're looking for is a clock source already */
365 source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
367 entity_id = source->bClockID;
368 if (validate && !uac_clock_source_is_valid(chip, fmt,
371 "clock source %d is not valid, cannot use\n",
378 selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
380 int ret, i, cur, err;
382 /* the entity ID we are looking for is a selector.
383 * find out what it currently selects */
384 ret = uac_clock_selector_get_val(chip, selector->bClockID);
388 /* Selector values are one-based */
390 if (ret > selector->bNrInPins || ret < 1) {
392 "%s(): selector reported illegal value, id %d, ret %d\n",
393 __func__, selector->bClockID, ret);
399 ret = __uac3_clock_find_source(chip, fmt,
400 selector->baCSourceID[ret - 1],
403 err = uac_clock_selector_set_val(chip, entity_id, cur);
408 if (!validate || ret > 0 || !chip->autoclock)
411 /* The current clock source is invalid, try others. */
412 for (i = 1; i <= selector->bNrInPins; i++) {
418 ret = __uac3_clock_find_source(chip, fmt,
419 selector->baCSourceID[i - 1],
424 err = uac_clock_selector_set_val(chip, entity_id, i);
429 "found and selected valid clock source %d\n",
437 /* FIXME: multipliers only act as pass-thru element for now */
438 multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
441 return __uac3_clock_find_source(chip, fmt,
442 multiplier->bCSourceID,
449 * For all kinds of sample rate settings and other device queries,
450 * the clock source (end-leaf) must be used. However, clock selectors,
451 * clock multipliers and sample rate converters may be specified as
452 * clock source input to terminal. This functions walks the clock path
453 * to its end and tries to find the source.
455 * The 'visited' bitfield is used internally to detect recursive loops.
457 * Returns the clock source UnitID (>=0) on success, or an error.
459 int snd_usb_clock_find_source(struct snd_usb_audio *chip,
460 struct audioformat *fmt, bool validate)
462 DECLARE_BITMAP(visited, 256);
463 memset(visited, 0, sizeof(visited));
465 switch (fmt->protocol) {
467 return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
470 return __uac3_clock_find_source(chip, fmt, fmt->clock, visited,
477 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
478 struct usb_host_interface *alts,
479 struct audioformat *fmt, int rate)
481 struct usb_device *dev = chip->dev;
483 unsigned char data[3];
486 if (get_iface_desc(alts)->bNumEndpoints < 1)
488 ep = get_endpoint(alts, 0)->bEndpointAddress;
490 /* if endpoint doesn't have sampling rate control, bail out */
491 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
496 data[2] = rate >> 16;
497 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
498 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
499 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
502 dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
503 iface, fmt->altsetting, rate, ep);
507 /* Don't check the sample rate for devices which we know don't
509 if (snd_usb_get_sample_rate_quirk(chip))
511 /* the firmware is likely buggy, don't repeat to fail too many times */
512 if (chip->sample_rate_read_error > 2)
515 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
516 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
517 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
520 dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
521 iface, fmt->altsetting, ep);
522 chip->sample_rate_read_error++;
523 return 0; /* some devices don't support reading */
526 crate = data[0] | (data[1] << 8) | (data[2] << 16);
528 dev_info(&dev->dev, "failed to read current rate; disabling the check\n");
529 chip->sample_rate_read_error = 3; /* three strikes, see above */
534 dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
535 // runtime->rate = crate;
541 static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
542 int altsetting, int clock)
544 struct usb_device *dev = chip->dev;
548 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
549 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
550 UAC2_CS_CONTROL_SAM_FREQ << 8,
551 snd_usb_ctrl_intf(chip) | (clock << 8),
552 &data, sizeof(data));
554 dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
555 iface, altsetting, err);
559 return le32_to_cpu(data);
562 static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
563 struct usb_host_interface *alts,
564 struct audioformat *fmt, int rate)
566 struct usb_device *dev = chip->dev;
568 int err, cur_rate, prev_rate;
573 /* First, try to find a valid clock. This may trigger
574 * automatic clock selection if the current clock is not
577 clock = snd_usb_clock_find_source(chip, fmt, true);
579 /* We did not find a valid clock, but that might be
580 * because the current sample rate does not match an
581 * external clock source. Try again without validation
582 * and we will do another validation after setting the
585 clock = snd_usb_clock_find_source(chip, fmt, false);
590 prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
591 if (prev_rate == rate)
594 if (fmt->protocol == UAC_VERSION_3) {
595 struct uac3_clock_source_descriptor *cs_desc;
597 cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
598 bmControls = le32_to_cpu(cs_desc->bmControls);
600 struct uac_clock_source_descriptor *cs_desc;
602 cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
603 bmControls = cs_desc->bmControls;
606 writeable = uac_v2v3_control_is_writeable(bmControls,
607 UAC2_CS_CONTROL_SAM_FREQ);
609 data = cpu_to_le32(rate);
610 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
611 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
612 UAC2_CS_CONTROL_SAM_FREQ << 8,
613 snd_usb_ctrl_intf(chip) | (clock << 8),
614 &data, sizeof(data));
617 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
618 iface, fmt->altsetting, rate, err);
622 cur_rate = get_sample_rate_v2v3(chip, iface,
623 fmt->altsetting, clock);
625 cur_rate = prev_rate;
628 if (cur_rate != rate) {
631 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
632 iface, fmt->altsetting, rate, cur_rate);
636 "current rate %d is different from the runtime rate %d\n",
640 /* Some devices doesn't respond to sample rate changes while the
641 * interface is active. */
642 if (rate != prev_rate) {
643 usb_set_interface(dev, iface, 0);
644 snd_usb_set_interface_quirk(dev);
645 usb_set_interface(dev, iface, fmt->altsetting);
646 snd_usb_set_interface_quirk(dev);
650 /* validate clock after rate change */
651 if (!uac_clock_source_is_valid(chip, fmt, clock))
656 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
657 struct usb_host_interface *alts,
658 struct audioformat *fmt, int rate)
660 switch (fmt->protocol) {
663 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
666 if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
667 if (rate != UAC3_BADD_SAMPLING_RATE)
674 return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);