1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Clock domain and sample rate management functions
6 #include <linux/bitops.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
12 #include <linux/usb/audio-v3.h>
14 #include <sound/core.h>
15 #include <sound/info.h>
16 #include <sound/pcm.h>
24 static void *find_uac_clock_desc(struct usb_host_interface
*iface
, int id
,
25 bool (*validator
)(void *, int), u8 type
)
29 while ((cs
= snd_usb_find_csint_desc(iface
->extra
, iface
->extralen
,
31 if (validator(cs
, id
))
38 static bool validate_clock_source_v2(void *p
, int id
)
40 struct uac_clock_source_descriptor
*cs
= p
;
41 return cs
->bLength
== sizeof(*cs
) && cs
->bClockID
== id
;
44 static bool validate_clock_source_v3(void *p
, int id
)
46 struct uac3_clock_source_descriptor
*cs
= p
;
47 return cs
->bLength
== sizeof(*cs
) && cs
->bClockID
== id
;
50 static bool validate_clock_selector_v2(void *p
, int id
)
52 struct uac_clock_selector_descriptor
*cs
= p
;
53 return cs
->bLength
>= sizeof(*cs
) && cs
->bClockID
== id
&&
54 cs
->bLength
== 7 + cs
->bNrInPins
;
57 static bool validate_clock_selector_v3(void *p
, int id
)
59 struct uac3_clock_selector_descriptor
*cs
= p
;
60 return cs
->bLength
>= sizeof(*cs
) && cs
->bClockID
== id
&&
61 cs
->bLength
== 11 + cs
->bNrInPins
;
64 static bool validate_clock_multiplier_v2(void *p
, int id
)
66 struct uac_clock_multiplier_descriptor
*cs
= p
;
67 return cs
->bLength
== sizeof(*cs
) && cs
->bClockID
== id
;
70 static bool validate_clock_multiplier_v3(void *p
, int id
)
72 struct uac3_clock_multiplier_descriptor
*cs
= p
;
73 return cs
->bLength
== sizeof(*cs
) && cs
->bClockID
== id
;
76 #define DEFINE_FIND_HELPER(name, obj, validator, type) \
77 static obj *name(struct usb_host_interface *iface, int id) \
79 return find_uac_clock_desc(iface, id, validator, type); \
82 DEFINE_FIND_HELPER(snd_usb_find_clock_source
,
83 struct uac_clock_source_descriptor
,
84 validate_clock_source_v2
, UAC2_CLOCK_SOURCE
);
85 DEFINE_FIND_HELPER(snd_usb_find_clock_source_v3
,
86 struct uac3_clock_source_descriptor
,
87 validate_clock_source_v3
, UAC3_CLOCK_SOURCE
);
89 DEFINE_FIND_HELPER(snd_usb_find_clock_selector
,
90 struct uac_clock_selector_descriptor
,
91 validate_clock_selector_v2
, UAC2_CLOCK_SELECTOR
);
92 DEFINE_FIND_HELPER(snd_usb_find_clock_selector_v3
,
93 struct uac3_clock_selector_descriptor
,
94 validate_clock_selector_v3
, UAC3_CLOCK_SELECTOR
);
96 DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier
,
97 struct uac_clock_multiplier_descriptor
,
98 validate_clock_multiplier_v2
, UAC2_CLOCK_MULTIPLIER
);
99 DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier_v3
,
100 struct uac3_clock_multiplier_descriptor
,
101 validate_clock_multiplier_v3
, UAC3_CLOCK_MULTIPLIER
);
103 static int uac_clock_selector_get_val(struct snd_usb_audio
*chip
, int selector_id
)
108 ret
= snd_usb_ctl_msg(chip
->dev
, usb_rcvctrlpipe(chip
->dev
, 0),
110 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
111 UAC2_CX_CLOCK_SELECTOR
<< 8,
112 snd_usb_ctrl_intf(chip
) | (selector_id
<< 8),
121 static int uac_clock_selector_set_val(struct snd_usb_audio
*chip
, int selector_id
,
126 ret
= snd_usb_ctl_msg(chip
->dev
, usb_sndctrlpipe(chip
->dev
, 0),
128 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
129 UAC2_CX_CLOCK_SELECTOR
<< 8,
130 snd_usb_ctrl_intf(chip
) | (selector_id
<< 8),
135 if (ret
!= sizeof(pin
)) {
137 "setting selector (id %d) unexpected length %d\n",
142 ret
= uac_clock_selector_get_val(chip
, selector_id
);
148 "setting selector (id %d) to %x failed (current: %d)\n",
149 selector_id
, pin
, ret
);
156 static bool uac_clock_source_is_valid(struct snd_usb_audio
*chip
,
162 struct usb_device
*dev
= chip
->dev
;
165 if (protocol
== UAC_VERSION_3
) {
166 struct uac3_clock_source_descriptor
*cs_desc
=
167 snd_usb_find_clock_source_v3(chip
->ctrl_intf
, source_id
);
171 bmControls
= le32_to_cpu(cs_desc
->bmControls
);
172 } else { /* UAC_VERSION_1/2 */
173 struct uac_clock_source_descriptor
*cs_desc
=
174 snd_usb_find_clock_source(chip
->ctrl_intf
, source_id
);
178 bmControls
= cs_desc
->bmControls
;
181 /* If a clock source can't tell us whether it's valid, we assume it is */
182 if (!uac_v2v3_control_is_readable(bmControls
,
183 UAC2_CS_CONTROL_CLOCK_VALID
))
186 err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC2_CS_CUR
,
187 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
188 UAC2_CS_CONTROL_CLOCK_VALID
<< 8,
189 snd_usb_ctrl_intf(chip
) | (source_id
<< 8),
190 &data
, sizeof(data
));
194 "%s(): cannot get clock validity for id %d\n",
195 __func__
, source_id
);
202 static int __uac_clock_find_source(struct snd_usb_audio
*chip
, int entity_id
,
203 unsigned long *visited
, bool validate
)
205 struct uac_clock_source_descriptor
*source
;
206 struct uac_clock_selector_descriptor
*selector
;
207 struct uac_clock_multiplier_descriptor
*multiplier
;
211 if (test_and_set_bit(entity_id
, visited
)) {
213 "%s(): recursive clock topology detected, id %d.\n",
214 __func__
, entity_id
);
218 /* first, see if the ID we're looking for is a clock source already */
219 source
= snd_usb_find_clock_source(chip
->ctrl_intf
, entity_id
);
221 entity_id
= source
->bClockID
;
222 if (validate
&& !uac_clock_source_is_valid(chip
, UAC_VERSION_2
,
225 "clock source %d is not valid, cannot use\n",
232 selector
= snd_usb_find_clock_selector(chip
->ctrl_intf
, entity_id
);
236 /* the entity ID we are looking for is a selector.
237 * find out what it currently selects */
238 ret
= uac_clock_selector_get_val(chip
, selector
->bClockID
);
242 /* Selector values are one-based */
244 if (ret
> selector
->bNrInPins
|| ret
< 1) {
246 "%s(): selector reported illegal value, id %d, ret %d\n",
247 __func__
, selector
->bClockID
, ret
);
253 ret
= __uac_clock_find_source(chip
, selector
->baCSourceID
[ret
- 1],
255 if (!validate
|| ret
> 0 || !chip
->autoclock
)
258 /* The current clock source is invalid, try others. */
259 for (i
= 1; i
<= selector
->bNrInPins
; i
++) {
265 ret
= __uac_clock_find_source(chip
, selector
->baCSourceID
[i
- 1],
270 err
= uac_clock_selector_set_val(chip
, entity_id
, i
);
275 "found and selected valid clock source %d\n",
283 /* FIXME: multipliers only act as pass-thru element for now */
284 multiplier
= snd_usb_find_clock_multiplier(chip
->ctrl_intf
, entity_id
);
286 return __uac_clock_find_source(chip
, multiplier
->bCSourceID
,
292 static int __uac3_clock_find_source(struct snd_usb_audio
*chip
, int entity_id
,
293 unsigned long *visited
, bool validate
)
295 struct uac3_clock_source_descriptor
*source
;
296 struct uac3_clock_selector_descriptor
*selector
;
297 struct uac3_clock_multiplier_descriptor
*multiplier
;
301 if (test_and_set_bit(entity_id
, visited
)) {
303 "%s(): recursive clock topology detected, id %d.\n",
304 __func__
, entity_id
);
308 /* first, see if the ID we're looking for is a clock source already */
309 source
= snd_usb_find_clock_source_v3(chip
->ctrl_intf
, entity_id
);
311 entity_id
= source
->bClockID
;
312 if (validate
&& !uac_clock_source_is_valid(chip
, UAC_VERSION_3
,
315 "clock source %d is not valid, cannot use\n",
322 selector
= snd_usb_find_clock_selector_v3(chip
->ctrl_intf
, entity_id
);
326 /* the entity ID we are looking for is a selector.
327 * find out what it currently selects */
328 ret
= uac_clock_selector_get_val(chip
, selector
->bClockID
);
332 /* Selector values are one-based */
334 if (ret
> selector
->bNrInPins
|| ret
< 1) {
336 "%s(): selector reported illegal value, id %d, ret %d\n",
337 __func__
, selector
->bClockID
, ret
);
343 ret
= __uac3_clock_find_source(chip
, selector
->baCSourceID
[ret
- 1],
345 if (!validate
|| ret
> 0 || !chip
->autoclock
)
348 /* The current clock source is invalid, try others. */
349 for (i
= 1; i
<= selector
->bNrInPins
; i
++) {
355 ret
= __uac3_clock_find_source(chip
, selector
->baCSourceID
[i
- 1],
360 err
= uac_clock_selector_set_val(chip
, entity_id
, i
);
365 "found and selected valid clock source %d\n",
373 /* FIXME: multipliers only act as pass-thru element for now */
374 multiplier
= snd_usb_find_clock_multiplier_v3(chip
->ctrl_intf
,
377 return __uac3_clock_find_source(chip
, multiplier
->bCSourceID
,
384 * For all kinds of sample rate settings and other device queries,
385 * the clock source (end-leaf) must be used. However, clock selectors,
386 * clock multipliers and sample rate converters may be specified as
387 * clock source input to terminal. This functions walks the clock path
388 * to its end and tries to find the source.
390 * The 'visited' bitfield is used internally to detect recursive loops.
392 * Returns the clock source UnitID (>=0) on success, or an error.
394 int snd_usb_clock_find_source(struct snd_usb_audio
*chip
, int protocol
,
395 int entity_id
, bool validate
)
397 DECLARE_BITMAP(visited
, 256);
398 memset(visited
, 0, sizeof(visited
));
402 return __uac_clock_find_source(chip
, entity_id
, visited
,
405 return __uac3_clock_find_source(chip
, entity_id
, visited
,
412 static int set_sample_rate_v1(struct snd_usb_audio
*chip
, int iface
,
413 struct usb_host_interface
*alts
,
414 struct audioformat
*fmt
, int rate
)
416 struct usb_device
*dev
= chip
->dev
;
418 unsigned char data
[3];
421 if (get_iface_desc(alts
)->bNumEndpoints
< 1)
423 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
425 /* if endpoint doesn't have sampling rate control, bail out */
426 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_SAMPLE_RATE
))
431 data
[2] = rate
>> 16;
432 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
433 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
434 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
,
437 dev_err(&dev
->dev
, "%d:%d: cannot set freq %d to ep %#x\n",
438 iface
, fmt
->altsetting
, rate
, ep
);
442 /* Don't check the sample rate for devices which we know don't
444 if (snd_usb_get_sample_rate_quirk(chip
))
446 /* the firmware is likely buggy, don't repeat to fail too many times */
447 if (chip
->sample_rate_read_error
> 2)
450 err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC_GET_CUR
,
451 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_IN
,
452 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
,
455 dev_err(&dev
->dev
, "%d:%d: cannot get freq at ep %#x\n",
456 iface
, fmt
->altsetting
, ep
);
457 chip
->sample_rate_read_error
++;
458 return 0; /* some devices don't support reading */
461 crate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
463 dev_warn(&dev
->dev
, "current rate %d is different from the runtime rate %d\n", crate
, rate
);
464 // runtime->rate = crate;
470 static int get_sample_rate_v2v3(struct snd_usb_audio
*chip
, int iface
,
471 int altsetting
, int clock
)
473 struct usb_device
*dev
= chip
->dev
;
477 err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC2_CS_CUR
,
478 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
479 UAC2_CS_CONTROL_SAM_FREQ
<< 8,
480 snd_usb_ctrl_intf(chip
) | (clock
<< 8),
481 &data
, sizeof(data
));
483 dev_warn(&dev
->dev
, "%d:%d: cannot get freq (v2/v3): err %d\n",
484 iface
, altsetting
, err
);
488 return le32_to_cpu(data
);
491 static int set_sample_rate_v2v3(struct snd_usb_audio
*chip
, int iface
,
492 struct usb_host_interface
*alts
,
493 struct audioformat
*fmt
, int rate
)
495 struct usb_device
*dev
= chip
->dev
;
497 int err
, cur_rate
, prev_rate
;
502 /* First, try to find a valid clock. This may trigger
503 * automatic clock selection if the current clock is not
506 clock
= snd_usb_clock_find_source(chip
, fmt
->protocol
,
509 /* We did not find a valid clock, but that might be
510 * because the current sample rate does not match an
511 * external clock source. Try again without validation
512 * and we will do another validation after setting the
515 clock
= snd_usb_clock_find_source(chip
, fmt
->protocol
,
521 prev_rate
= get_sample_rate_v2v3(chip
, iface
, fmt
->altsetting
, clock
);
522 if (prev_rate
== rate
)
525 if (fmt
->protocol
== UAC_VERSION_3
) {
526 struct uac3_clock_source_descriptor
*cs_desc
;
528 cs_desc
= snd_usb_find_clock_source_v3(chip
->ctrl_intf
, clock
);
529 bmControls
= le32_to_cpu(cs_desc
->bmControls
);
531 struct uac_clock_source_descriptor
*cs_desc
;
533 cs_desc
= snd_usb_find_clock_source(chip
->ctrl_intf
, clock
);
534 bmControls
= cs_desc
->bmControls
;
537 writeable
= uac_v2v3_control_is_writeable(bmControls
,
538 UAC2_CS_CONTROL_SAM_FREQ
);
540 data
= cpu_to_le32(rate
);
541 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC2_CS_CUR
,
542 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
543 UAC2_CS_CONTROL_SAM_FREQ
<< 8,
544 snd_usb_ctrl_intf(chip
) | (clock
<< 8),
545 &data
, sizeof(data
));
548 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
549 iface
, fmt
->altsetting
, rate
, err
);
553 cur_rate
= get_sample_rate_v2v3(chip
, iface
,
554 fmt
->altsetting
, clock
);
556 cur_rate
= prev_rate
;
559 if (cur_rate
!= rate
) {
562 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
563 iface
, fmt
->altsetting
, rate
, cur_rate
);
567 "current rate %d is different from the runtime rate %d\n",
571 /* Some devices doesn't respond to sample rate changes while the
572 * interface is active. */
573 if (rate
!= prev_rate
) {
574 usb_set_interface(dev
, iface
, 0);
575 snd_usb_set_interface_quirk(dev
);
576 usb_set_interface(dev
, iface
, fmt
->altsetting
);
577 snd_usb_set_interface_quirk(dev
);
581 /* validate clock after rate change */
582 if (!uac_clock_source_is_valid(chip
, fmt
->protocol
, clock
))
587 int snd_usb_init_sample_rate(struct snd_usb_audio
*chip
, int iface
,
588 struct usb_host_interface
*alts
,
589 struct audioformat
*fmt
, int rate
)
591 switch (fmt
->protocol
) {
594 return set_sample_rate_v1(chip
, iface
, alts
, fmt
, rate
);
597 if (chip
->badd_profile
>= UAC3_FUNCTION_SUBCLASS_GENERIC_IO
) {
598 if (rate
!= UAC3_BADD_SAMPLING_RATE
)
605 return set_sample_rate_v2v3(chip
, iface
, alts
, fmt
, rate
);