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
);
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],
298 if (!validate
|| ret
> 0 || !chip
->autoclock
)
301 /* The current clock source is invalid, try others. */
302 for (i
= 1; i
<= selector
->bNrInPins
; i
++) {
308 ret
= __uac_clock_find_source(chip
, fmt
,
309 selector
->baCSourceID
[i
- 1],
314 err
= uac_clock_selector_set_val(chip
, entity_id
, i
);
319 "found and selected valid clock source %d\n",
327 /* FIXME: multipliers only act as pass-thru element for now */
328 multiplier
= snd_usb_find_clock_multiplier(chip
->ctrl_intf
, entity_id
);
330 return __uac_clock_find_source(chip
, fmt
,
331 multiplier
->bCSourceID
,
337 static int __uac3_clock_find_source(struct snd_usb_audio
*chip
,
338 struct audioformat
*fmt
, int entity_id
,
339 unsigned long *visited
, bool validate
)
341 struct uac3_clock_source_descriptor
*source
;
342 struct uac3_clock_selector_descriptor
*selector
;
343 struct uac3_clock_multiplier_descriptor
*multiplier
;
347 if (test_and_set_bit(entity_id
, visited
)) {
349 "%s(): recursive clock topology detected, id %d.\n",
350 __func__
, entity_id
);
354 /* first, see if the ID we're looking for is a clock source already */
355 source
= snd_usb_find_clock_source_v3(chip
->ctrl_intf
, entity_id
);
357 entity_id
= source
->bClockID
;
358 if (validate
&& !uac_clock_source_is_valid(chip
, fmt
,
361 "clock source %d is not valid, cannot use\n",
368 selector
= snd_usb_find_clock_selector_v3(chip
->ctrl_intf
, entity_id
);
372 /* the entity ID we are looking for is a selector.
373 * find out what it currently selects */
374 ret
= uac_clock_selector_get_val(chip
, selector
->bClockID
);
378 /* Selector values are one-based */
380 if (ret
> selector
->bNrInPins
|| ret
< 1) {
382 "%s(): selector reported illegal value, id %d, ret %d\n",
383 __func__
, selector
->bClockID
, ret
);
389 ret
= __uac3_clock_find_source(chip
, fmt
,
390 selector
->baCSourceID
[ret
- 1],
392 if (!validate
|| ret
> 0 || !chip
->autoclock
)
395 /* The current clock source is invalid, try others. */
396 for (i
= 1; i
<= selector
->bNrInPins
; i
++) {
402 ret
= __uac3_clock_find_source(chip
, fmt
,
403 selector
->baCSourceID
[i
- 1],
408 err
= uac_clock_selector_set_val(chip
, entity_id
, i
);
413 "found and selected valid clock source %d\n",
421 /* FIXME: multipliers only act as pass-thru element for now */
422 multiplier
= snd_usb_find_clock_multiplier_v3(chip
->ctrl_intf
,
425 return __uac3_clock_find_source(chip
, fmt
,
426 multiplier
->bCSourceID
,
433 * For all kinds of sample rate settings and other device queries,
434 * the clock source (end-leaf) must be used. However, clock selectors,
435 * clock multipliers and sample rate converters may be specified as
436 * clock source input to terminal. This functions walks the clock path
437 * to its end and tries to find the source.
439 * The 'visited' bitfield is used internally to detect recursive loops.
441 * Returns the clock source UnitID (>=0) on success, or an error.
443 int snd_usb_clock_find_source(struct snd_usb_audio
*chip
,
444 struct audioformat
*fmt
, bool validate
)
446 DECLARE_BITMAP(visited
, 256);
447 memset(visited
, 0, sizeof(visited
));
449 switch (fmt
->protocol
) {
451 return __uac_clock_find_source(chip
, fmt
, fmt
->clock
, visited
,
454 return __uac3_clock_find_source(chip
, fmt
, fmt
->clock
, visited
,
461 static int set_sample_rate_v1(struct snd_usb_audio
*chip
, int iface
,
462 struct usb_host_interface
*alts
,
463 struct audioformat
*fmt
, int rate
)
465 struct usb_device
*dev
= chip
->dev
;
467 unsigned char data
[3];
470 if (get_iface_desc(alts
)->bNumEndpoints
< 1)
472 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
474 /* if endpoint doesn't have sampling rate control, bail out */
475 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_SAMPLE_RATE
))
480 data
[2] = rate
>> 16;
481 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
482 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
483 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
,
486 dev_err(&dev
->dev
, "%d:%d: cannot set freq %d to ep %#x\n",
487 iface
, fmt
->altsetting
, rate
, ep
);
491 /* Don't check the sample rate for devices which we know don't
493 if (snd_usb_get_sample_rate_quirk(chip
))
495 /* the firmware is likely buggy, don't repeat to fail too many times */
496 if (chip
->sample_rate_read_error
> 2)
499 err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC_GET_CUR
,
500 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_IN
,
501 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
,
504 dev_err(&dev
->dev
, "%d:%d: cannot get freq at ep %#x\n",
505 iface
, fmt
->altsetting
, ep
);
506 chip
->sample_rate_read_error
++;
507 return 0; /* some devices don't support reading */
510 crate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
512 dev_warn(&dev
->dev
, "current rate %d is different from the runtime rate %d\n", crate
, rate
);
513 // runtime->rate = crate;
519 static int get_sample_rate_v2v3(struct snd_usb_audio
*chip
, int iface
,
520 int altsetting
, int clock
)
522 struct usb_device
*dev
= chip
->dev
;
526 err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC2_CS_CUR
,
527 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
528 UAC2_CS_CONTROL_SAM_FREQ
<< 8,
529 snd_usb_ctrl_intf(chip
) | (clock
<< 8),
530 &data
, sizeof(data
));
532 dev_warn(&dev
->dev
, "%d:%d: cannot get freq (v2/v3): err %d\n",
533 iface
, altsetting
, err
);
537 return le32_to_cpu(data
);
540 static int set_sample_rate_v2v3(struct snd_usb_audio
*chip
, int iface
,
541 struct usb_host_interface
*alts
,
542 struct audioformat
*fmt
, int rate
)
544 struct usb_device
*dev
= chip
->dev
;
546 int err
, cur_rate
, prev_rate
;
551 /* First, try to find a valid clock. This may trigger
552 * automatic clock selection if the current clock is not
555 clock
= snd_usb_clock_find_source(chip
, fmt
, true);
557 /* We did not find a valid clock, but that might be
558 * because the current sample rate does not match an
559 * external clock source. Try again without validation
560 * and we will do another validation after setting the
563 clock
= snd_usb_clock_find_source(chip
, fmt
, false);
568 prev_rate
= get_sample_rate_v2v3(chip
, iface
, fmt
->altsetting
, clock
);
569 if (prev_rate
== rate
)
572 if (fmt
->protocol
== UAC_VERSION_3
) {
573 struct uac3_clock_source_descriptor
*cs_desc
;
575 cs_desc
= snd_usb_find_clock_source_v3(chip
->ctrl_intf
, clock
);
576 bmControls
= le32_to_cpu(cs_desc
->bmControls
);
578 struct uac_clock_source_descriptor
*cs_desc
;
580 cs_desc
= snd_usb_find_clock_source(chip
->ctrl_intf
, clock
);
581 bmControls
= cs_desc
->bmControls
;
584 writeable
= uac_v2v3_control_is_writeable(bmControls
,
585 UAC2_CS_CONTROL_SAM_FREQ
);
587 data
= cpu_to_le32(rate
);
588 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC2_CS_CUR
,
589 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
590 UAC2_CS_CONTROL_SAM_FREQ
<< 8,
591 snd_usb_ctrl_intf(chip
) | (clock
<< 8),
592 &data
, sizeof(data
));
595 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
596 iface
, fmt
->altsetting
, rate
, err
);
600 cur_rate
= get_sample_rate_v2v3(chip
, iface
,
601 fmt
->altsetting
, clock
);
603 cur_rate
= prev_rate
;
606 if (cur_rate
!= rate
) {
609 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
610 iface
, fmt
->altsetting
, rate
, cur_rate
);
614 "current rate %d is different from the runtime rate %d\n",
618 /* Some devices doesn't respond to sample rate changes while the
619 * interface is active. */
620 if (rate
!= prev_rate
) {
621 usb_set_interface(dev
, iface
, 0);
622 snd_usb_set_interface_quirk(dev
);
623 usb_set_interface(dev
, iface
, fmt
->altsetting
);
624 snd_usb_set_interface_quirk(dev
);
628 /* validate clock after rate change */
629 if (!uac_clock_source_is_valid(chip
, fmt
, clock
))
634 int snd_usb_init_sample_rate(struct snd_usb_audio
*chip
, int iface
,
635 struct usb_host_interface
*alts
,
636 struct audioformat
*fmt
, int rate
)
638 switch (fmt
->protocol
) {
641 return set_sample_rate_v1(chip
, iface
, alts
, fmt
, rate
);
644 if (chip
->badd_profile
>= UAC3_FUNCTION_SUBCLASS_GENERIC_IO
) {
645 if (rate
!= UAC3_BADD_SAMPLING_RATE
)
652 return set_sample_rate_v2v3(chip
, iface
, alts
, fmt
, rate
);