1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Clock domain and sample rate management functions
4 */
5
6 #include <linux/bitops.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9 #include <linux/usb.h>
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
12 #include <linux/usb/audio-v3.h>
13
14 #include <sound/core.h>
15 #include <sound/info.h>
16 #include <sound/pcm.h>
17
18 #include "usbaudio.h"
19 #include "card.h"
20 #include "helper.h"
21 #include "clock.h"
22 #include "quirks.h"
23
24 union uac23_clock_source_desc {
25 struct uac_clock_source_descriptor v2;
26 struct uac3_clock_source_descriptor v3;
27 };
28
29 union uac23_clock_selector_desc {
30 struct uac_clock_selector_descriptor v2;
31 struct uac3_clock_selector_descriptor v3;
32 };
33
34 union uac23_clock_multiplier_desc {
35 struct uac_clock_multiplier_descriptor v2;
36 struct uac_clock_multiplier_descriptor v3;
37 };
38
39 /* check whether the descriptor bLength has the minimal length */
40 #define DESC_LENGTH_CHECK(p, proto) \
41 ((proto) == UAC_VERSION_3 ? \
42 ((p)->v3.bLength >= sizeof((p)->v3)) : \
43 ((p)->v2.bLength >= sizeof((p)->v2)))
44
45 #define GET_VAL(p, proto, field) \
46 ((proto) == UAC_VERSION_3 ? (p)->v3.field : (p)->v2.field)
47
find_uac_clock_desc(struct usb_host_interface * iface,int id,bool (* validator)(void *,int,int),u8 type,int proto)48 static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
49 bool (*validator)(void *, int, int),
50 u8 type, int proto)
51 {
52 void *cs = NULL;
53
54 while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
55 cs, type))) {
56 if (validator(cs, id, proto))
57 return cs;
58 }
59
60 return NULL;
61 }
62
validate_clock_source(void * p,int id,int proto)63 static bool validate_clock_source(void *p, int id, int proto)
64 {
65 union uac23_clock_source_desc *cs = p;
66
67 if (!DESC_LENGTH_CHECK(cs, proto))
68 return false;
69 return GET_VAL(cs, proto, bClockID) == id;
70 }
71
validate_clock_selector(void * p,int id,int proto)72 static bool validate_clock_selector(void *p, int id, int proto)
73 {
74 union uac23_clock_selector_desc *cs = p;
75
76 if (!DESC_LENGTH_CHECK(cs, proto))
77 return false;
78 if (GET_VAL(cs, proto, bClockID) != id)
79 return false;
80 /* additional length check for baCSourceID array (in bNrInPins size)
81 * and two more fields (which sizes depend on the protocol)
82 */
83 if (proto == UAC_VERSION_3)
84 return cs->v3.bLength >= sizeof(cs->v3) + cs->v3.bNrInPins +
85 4 /* bmControls */ + 2 /* wCSelectorDescrStr */;
86 else
87 return cs->v2.bLength >= sizeof(cs->v2) + cs->v2.bNrInPins +
88 1 /* bmControls */ + 1 /* iClockSelector */;
89 }
90
validate_clock_multiplier(void * p,int id,int proto)91 static bool validate_clock_multiplier(void *p, int id, int proto)
92 {
93 union uac23_clock_multiplier_desc *cs = p;
94
95 if (!DESC_LENGTH_CHECK(cs, proto))
96 return false;
97 return GET_VAL(cs, proto, bClockID) == id;
98 }
99
100 #define DEFINE_FIND_HELPER(name, obj, validator, type2, type3) \
101 static obj *name(struct snd_usb_audio *chip, int id, int proto) \
102 { \
103 return find_uac_clock_desc(chip->ctrl_intf, id, validator, \
104 proto == UAC_VERSION_3 ? (type3) : (type2), \
105 proto); \
106 }
107
108 DEFINE_FIND_HELPER(snd_usb_find_clock_source,
109 union uac23_clock_source_desc, validate_clock_source,
110 UAC2_CLOCK_SOURCE, UAC3_CLOCK_SOURCE);
111 DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
112 union uac23_clock_selector_desc, validate_clock_selector,
113 UAC2_CLOCK_SELECTOR, UAC3_CLOCK_SELECTOR);
114 DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
115 union uac23_clock_multiplier_desc, validate_clock_multiplier,
116 UAC2_CLOCK_MULTIPLIER, UAC3_CLOCK_MULTIPLIER);
117
uac_clock_selector_get_val(struct snd_usb_audio * chip,int selector_id)118 static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
119 {
120 unsigned char buf;
121 int ret;
122
123 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
124 UAC2_CS_CUR,
125 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
126 UAC2_CX_CLOCK_SELECTOR << 8,
127 snd_usb_ctrl_intf(chip) | (selector_id << 8),
128 &buf, sizeof(buf));
129
130 if (ret < 0)
131 return ret;
132
133 return buf;
134 }
135
uac_clock_selector_set_val(struct snd_usb_audio * chip,int selector_id,unsigned char pin)136 static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
137 unsigned char pin)
138 {
139 int ret;
140
141 ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
142 UAC2_CS_CUR,
143 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
144 UAC2_CX_CLOCK_SELECTOR << 8,
145 snd_usb_ctrl_intf(chip) | (selector_id << 8),
146 &pin, sizeof(pin));
147 if (ret < 0)
148 return ret;
149
150 if (ret != sizeof(pin)) {
151 usb_audio_err(chip,
152 "setting selector (id %d) unexpected length %d\n",
153 selector_id, ret);
154 return -EINVAL;
155 }
156
157 ret = uac_clock_selector_get_val(chip, selector_id);
158 if (ret < 0)
159 return ret;
160
161 if (ret != pin) {
162 usb_audio_err(chip,
163 "setting selector (id %d) to %x failed (current: %d)\n",
164 selector_id, pin, ret);
165 return -EINVAL;
166 }
167
168 return ret;
169 }
170
uac_clock_source_is_valid_quirk(struct snd_usb_audio * chip,const struct audioformat * fmt,int source_id)171 static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
172 const struct audioformat *fmt,
173 int source_id)
174 {
175 bool ret = false;
176 int count;
177 unsigned char data;
178 struct usb_device *dev = chip->dev;
179 union uac23_clock_source_desc *cs_desc;
180
181 cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol);
182 if (!cs_desc)
183 return false;
184
185 if (fmt->protocol == UAC_VERSION_2) {
186 /*
187 * Assume the clock is valid if clock source supports only one
188 * single sample rate, the terminal is connected directly to it
189 * (there is no clock selector) and clock type is internal.
190 * This is to deal with some Denon DJ controllers that always
191 * reports that clock is invalid.
192 */
193 if (fmt->nr_rates == 1 &&
194 (fmt->clock & 0xff) == cs_desc->v2.bClockID &&
195 (cs_desc->v2.bmAttributes & 0x3) !=
196 UAC_CLOCK_SOURCE_TYPE_EXT)
197 return true;
198 }
199
200 /*
201 * MOTU MicroBook IIc
202 * Sample rate changes takes more than 2 seconds for this device. Clock
203 * validity request returns false during that period.
204 */
205 if (chip->usb_id == USB_ID(0x07fd, 0x0004)) {
206 count = 0;
207
208 while ((!ret) && (count < 50)) {
209 int err;
210
211 msleep(100);
212
213 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
214 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
215 UAC2_CS_CONTROL_CLOCK_VALID << 8,
216 snd_usb_ctrl_intf(chip) | (source_id << 8),
217 &data, sizeof(data));
218 if (err < 0) {
219 dev_warn(&dev->dev,
220 "%s(): cannot get clock validity for id %d\n",
221 __func__, source_id);
222 return false;
223 }
224
225 ret = !!data;
226 count++;
227 }
228 }
229
230 return ret;
231 }
232
uac_clock_source_is_valid(struct snd_usb_audio * chip,const struct audioformat * fmt,int source_id)233 static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
234 const struct audioformat *fmt,
235 int source_id)
236 {
237 int err;
238 unsigned char data;
239 struct usb_device *dev = chip->dev;
240 u32 bmControls;
241 union uac23_clock_source_desc *cs_desc;
242
243 cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol);
244 if (!cs_desc)
245 return false;
246
247 if (fmt->protocol == UAC_VERSION_3)
248 bmControls = le32_to_cpu(cs_desc->v3.bmControls);
249 else
250 bmControls = cs_desc->v2.bmControls;
251
252 /* If a clock source can't tell us whether it's valid, we assume it is */
253 if (!uac_v2v3_control_is_readable(bmControls,
254 UAC2_CS_CONTROL_CLOCK_VALID))
255 return true;
256
257 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
258 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
259 UAC2_CS_CONTROL_CLOCK_VALID << 8,
260 snd_usb_ctrl_intf(chip) | (source_id << 8),
261 &data, sizeof(data));
262
263 if (err < 0) {
264 dev_warn(&dev->dev,
265 "%s(): cannot get clock validity for id %d\n",
266 __func__, source_id);
267 return false;
268 }
269
270 if (data)
271 return true;
272 else
273 return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
274 }
275
__uac_clock_find_source(struct snd_usb_audio * chip,const struct audioformat * fmt,int entity_id,unsigned long * visited,bool validate)276 static int __uac_clock_find_source(struct snd_usb_audio *chip,
277 const struct audioformat *fmt, int entity_id,
278 unsigned long *visited, bool validate)
279 {
280 union uac23_clock_source_desc *source;
281 union uac23_clock_selector_desc *selector;
282 union uac23_clock_multiplier_desc *multiplier;
283 int ret, i, cur, err, pins, clock_id;
284 const u8 *sources;
285 int proto = fmt->protocol;
286
287 entity_id &= 0xff;
288
289 if (test_and_set_bit(entity_id, visited)) {
290 usb_audio_warn(chip,
291 "%s(): recursive clock topology detected, id %d.\n",
292 __func__, entity_id);
293 return -EINVAL;
294 }
295
296 /* first, see if the ID we're looking at is a clock source already */
297 source = snd_usb_find_clock_source(chip, entity_id, proto);
298 if (source) {
299 entity_id = GET_VAL(source, proto, bClockID);
300 if (validate && !uac_clock_source_is_valid(chip, fmt,
301 entity_id)) {
302 usb_audio_err(chip,
303 "clock source %d is not valid, cannot use\n",
304 entity_id);
305 return -ENXIO;
306 }
307 return entity_id;
308 }
309
310 selector = snd_usb_find_clock_selector(chip, entity_id, proto);
311 if (selector) {
312 pins = GET_VAL(selector, proto, bNrInPins);
313 clock_id = GET_VAL(selector, proto, bClockID);
314 sources = GET_VAL(selector, proto, baCSourceID);
315 cur = 0;
316
317 if (pins == 1) {
318 ret = 1;
319 goto find_source;
320 }
321
322 /* the entity ID we are looking at is a selector.
323 * find out what it currently selects */
324 ret = uac_clock_selector_get_val(chip, clock_id);
325 if (ret < 0) {
326 if (!chip->autoclock)
327 return ret;
328 goto find_others;
329 }
330
331 /* Selector values are one-based */
332
333 if (ret > pins || ret < 1) {
334 usb_audio_err(chip,
335 "%s(): selector reported illegal value, id %d, ret %d\n",
336 __func__, clock_id, ret);
337
338 if (!chip->autoclock)
339 return -EINVAL;
340 goto find_others;
341 }
342
343 find_source:
344 cur = ret;
345 ret = __uac_clock_find_source(chip, fmt,
346 sources[ret - 1],
347 visited, validate);
348 if (ret > 0) {
349 /* Skip setting clock selector again for some devices */
350 if (chip->quirk_flags & QUIRK_FLAG_SKIP_CLOCK_SELECTOR)
351 return ret;
352 err = uac_clock_selector_set_val(chip, entity_id, cur);
353 if (err < 0) {
354 if (pins == 1) {
355 usb_audio_dbg(chip,
356 "%s(): selector returned an error, "
357 "assuming a firmware bug, id %d, ret %d\n",
358 __func__, clock_id, err);
359 return ret;
360 }
361 return err;
362 }
363 }
364
365 if (!validate || ret > 0 || !chip->autoclock)
366 return ret;
367
368 find_others:
369 /* The current clock source is invalid, try others. */
370 for (i = 1; i <= pins; i++) {
371 if (i == cur)
372 continue;
373
374 ret = __uac_clock_find_source(chip, fmt,
375 sources[i - 1],
376 visited, true);
377 if (ret < 0)
378 continue;
379
380 err = uac_clock_selector_set_val(chip, entity_id, i);
381 if (err < 0)
382 continue;
383
384 usb_audio_info(chip,
385 "found and selected valid clock source %d\n",
386 ret);
387 return ret;
388 }
389
390 return -ENXIO;
391 }
392
393 /* FIXME: multipliers only act as pass-thru element for now */
394 multiplier = snd_usb_find_clock_multiplier(chip, entity_id, proto);
395 if (multiplier)
396 return __uac_clock_find_source(chip, fmt,
397 GET_VAL(multiplier, proto, bCSourceID),
398 visited, validate);
399
400 return -EINVAL;
401 }
402
403 /*
404 * For all kinds of sample rate settings and other device queries,
405 * the clock source (end-leaf) must be used. However, clock selectors,
406 * clock multipliers and sample rate converters may be specified as
407 * clock source input to terminal. This functions walks the clock path
408 * to its end and tries to find the source.
409 *
410 * The 'visited' bitfield is used internally to detect recursive loops.
411 *
412 * Returns the clock source UnitID (>=0) on success, or an error.
413 */
snd_usb_clock_find_source(struct snd_usb_audio * chip,const struct audioformat * fmt,bool validate)414 int snd_usb_clock_find_source(struct snd_usb_audio *chip,
415 const struct audioformat *fmt, bool validate)
416 {
417 DECLARE_BITMAP(visited, 256);
418 memset(visited, 0, sizeof(visited));
419
420 switch (fmt->protocol) {
421 case UAC_VERSION_2:
422 case UAC_VERSION_3:
423 return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
424 validate);
425 default:
426 return -EINVAL;
427 }
428 }
429
set_sample_rate_v1(struct snd_usb_audio * chip,const struct audioformat * fmt,int rate)430 static int set_sample_rate_v1(struct snd_usb_audio *chip,
431 const struct audioformat *fmt, int rate)
432 {
433 struct usb_device *dev = chip->dev;
434 unsigned char data[3];
435 int err, crate;
436
437 /* if endpoint doesn't have sampling rate control, bail out */
438 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
439 return 0;
440
441 data[0] = rate;
442 data[1] = rate >> 8;
443 data[2] = rate >> 16;
444 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
445 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
446 UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
447 fmt->endpoint, data, sizeof(data));
448 if (err < 0) {
449 dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
450 fmt->iface, fmt->altsetting, rate, fmt->endpoint);
451 return err;
452 }
453
454 /* Don't check the sample rate for devices which we know don't
455 * support reading */
456 if (chip->quirk_flags & QUIRK_FLAG_GET_SAMPLE_RATE)
457 return 0;
458 /* the firmware is likely buggy, don't repeat to fail too many times */
459 if (chip->sample_rate_read_error > 2)
460 return 0;
461
462 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
463 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
464 UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
465 fmt->endpoint, data, sizeof(data));
466 if (err < 0) {
467 dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
468 fmt->iface, fmt->altsetting, fmt->endpoint);
469 chip->sample_rate_read_error++;
470 return 0; /* some devices don't support reading */
471 }
472
473 crate = data[0] | (data[1] << 8) | (data[2] << 16);
474 if (!crate) {
475 dev_info(&dev->dev, "failed to read current rate; disabling the check\n");
476 chip->sample_rate_read_error = 3; /* three strikes, see above */
477 return 0;
478 }
479
480 if (crate != rate) {
481 dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
482 // runtime->rate = crate;
483 }
484
485 return 0;
486 }
487
get_sample_rate_v2v3(struct snd_usb_audio * chip,int iface,int altsetting,int clock)488 static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
489 int altsetting, int clock)
490 {
491 struct usb_device *dev = chip->dev;
492 __le32 data;
493 int err;
494
495 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
496 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
497 UAC2_CS_CONTROL_SAM_FREQ << 8,
498 snd_usb_ctrl_intf(chip) | (clock << 8),
499 &data, sizeof(data));
500 if (err < 0) {
501 dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
502 iface, altsetting, err);
503 return 0;
504 }
505
506 return le32_to_cpu(data);
507 }
508
509 /*
510 * Try to set the given sample rate:
511 *
512 * Return 0 if the clock source is read-only, the actual rate on success,
513 * or a negative error code.
514 *
515 * This function gets called from format.c to validate each sample rate, too.
516 * Hence no message is shown upon error
517 */
snd_usb_set_sample_rate_v2v3(struct snd_usb_audio * chip,const struct audioformat * fmt,int clock,int rate)518 int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
519 const struct audioformat *fmt,
520 int clock, int rate)
521 {
522 bool writeable;
523 u32 bmControls;
524 __le32 data;
525 int err;
526 union uac23_clock_source_desc *cs_desc;
527
528 cs_desc = snd_usb_find_clock_source(chip, clock, fmt->protocol);
529
530 if (!cs_desc)
531 return 0;
532
533 if (fmt->protocol == UAC_VERSION_3)
534 bmControls = le32_to_cpu(cs_desc->v3.bmControls);
535 else
536 bmControls = cs_desc->v2.bmControls;
537
538 writeable = uac_v2v3_control_is_writeable(bmControls,
539 UAC2_CS_CONTROL_SAM_FREQ);
540 if (!writeable)
541 return 0;
542
543 data = cpu_to_le32(rate);
544 err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR,
545 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
546 UAC2_CS_CONTROL_SAM_FREQ << 8,
547 snd_usb_ctrl_intf(chip) | (clock << 8),
548 &data, sizeof(data));
549 if (err < 0)
550 return err;
551
552 return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
553 }
554
set_sample_rate_v2v3(struct snd_usb_audio * chip,const struct audioformat * fmt,int rate)555 static int set_sample_rate_v2v3(struct snd_usb_audio *chip,
556 const struct audioformat *fmt, int rate)
557 {
558 int cur_rate, prev_rate;
559 int clock;
560
561 /* First, try to find a valid clock. This may trigger
562 * automatic clock selection if the current clock is not
563 * valid.
564 */
565 clock = snd_usb_clock_find_source(chip, fmt, true);
566 if (clock < 0) {
567 /* We did not find a valid clock, but that might be
568 * because the current sample rate does not match an
569 * external clock source. Try again without validation
570 * and we will do another validation after setting the
571 * rate.
572 */
573 clock = snd_usb_clock_find_source(chip, fmt, false);
574
575 /* Hardcoded sample rates */
576 if (chip->quirk_flags & QUIRK_FLAG_IGNORE_CLOCK_SOURCE)
577 return 0;
578
579 if (clock < 0)
580 return clock;
581 }
582
583 prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
584 if (prev_rate == rate)
585 goto validation;
586
587 cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate);
588 if (cur_rate < 0) {
589 usb_audio_err(chip,
590 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
591 fmt->iface, fmt->altsetting, rate, cur_rate);
592 return cur_rate;
593 }
594
595 if (!cur_rate)
596 cur_rate = prev_rate;
597
598 if (cur_rate != rate) {
599 usb_audio_dbg(chip,
600 "%d:%d: freq mismatch: req %d, clock runs @%d\n",
601 fmt->iface, fmt->altsetting, rate, cur_rate);
602 /* continue processing */
603 }
604
605 /* FIXME - TEAC devices require the immediate interface setup */
606 if (USB_ID_VENDOR(chip->usb_id) == 0x0644) {
607 bool cur_base_48k = (rate % 48000 == 0);
608 bool prev_base_48k = (prev_rate % 48000 == 0);
609 if (cur_base_48k != prev_base_48k) {
610 usb_set_interface(chip->dev, fmt->iface, fmt->altsetting);
611 if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
612 msleep(50);
613 }
614 }
615
616 validation:
617 /* validate clock after rate change */
618 if (!uac_clock_source_is_valid(chip, fmt, clock))
619 return -ENXIO;
620 return 0;
621 }
622
snd_usb_init_sample_rate(struct snd_usb_audio * chip,const struct audioformat * fmt,int rate)623 int snd_usb_init_sample_rate(struct snd_usb_audio *chip,
624 const struct audioformat *fmt, int rate)
625 {
626 usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n",
627 fmt->iface, fmt->altsetting, rate, fmt->clock);
628
629 switch (fmt->protocol) {
630 case UAC_VERSION_1:
631 default:
632 return set_sample_rate_v1(chip, fmt, rate);
633
634 case UAC_VERSION_3:
635 if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
636 if (rate != UAC3_BADD_SAMPLING_RATE)
637 return -ENXIO;
638 else
639 return 0;
640 }
641 fallthrough;
642 case UAC_VERSION_2:
643 return set_sample_rate_v2v3(chip, fmt, rate);
644 }
645 }
646
647