blob: e31349865f202d0367e69ee00a5cac2d7789dcb0 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Clock domain and sample rate management functions
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
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>
27
28#include <sound/core.h>
29#include <sound/info.h>
30#include <sound/pcm.h>
31
32#include "usbaudio.h"
33#include "card.h"
34#include "helper.h"
35#include "clock.h"
36#include "quirks.h"
37
38static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
39 bool (*validator)(void *, int), u8 type)
40{
41 void *cs = NULL;
42
43 while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
44 cs, type))) {
45 if (validator(cs, id))
46 return cs;
47 }
48
49 return NULL;
50}
51
52static bool validate_clock_source_v2(void *p, int id)
53{
54 struct uac_clock_source_descriptor *cs = p;
55 return cs->bClockID == id;
56}
57
58static bool validate_clock_source_v3(void *p, int id)
59{
60 struct uac3_clock_source_descriptor *cs = p;
61 return cs->bClockID == id;
62}
63
64static bool validate_clock_selector_v2(void *p, int id)
65{
66 struct uac_clock_selector_descriptor *cs = p;
67 return cs->bClockID == id;
68}
69
70static bool validate_clock_selector_v3(void *p, int id)
71{
72 struct uac3_clock_selector_descriptor *cs = p;
73 return cs->bClockID == id;
74}
75
76static bool validate_clock_multiplier_v2(void *p, int id)
77{
78 struct uac_clock_multiplier_descriptor *cs = p;
79 return cs->bClockID == id;
80}
81
82static bool validate_clock_multiplier_v3(void *p, int id)
83{
84 struct uac3_clock_multiplier_descriptor *cs = p;
85 return cs->bClockID == id;
86}
87
88#define DEFINE_FIND_HELPER(name, obj, validator, type) \
89static obj *name(struct usb_host_interface *iface, int id) \
90{ \
91 return find_uac_clock_desc(iface, id, validator, type); \
92}
93
94DEFINE_FIND_HELPER(snd_usb_find_clock_source,
95 struct uac_clock_source_descriptor,
96 validate_clock_source_v2, UAC2_CLOCK_SOURCE);
97DEFINE_FIND_HELPER(snd_usb_find_clock_source_v3,
98 struct uac3_clock_source_descriptor,
99 validate_clock_source_v3, UAC3_CLOCK_SOURCE);
100
101DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
102 struct uac_clock_selector_descriptor,
103 validate_clock_selector_v2, UAC2_CLOCK_SELECTOR);
104DEFINE_FIND_HELPER(snd_usb_find_clock_selector_v3,
105 struct uac3_clock_selector_descriptor,
106 validate_clock_selector_v3, UAC3_CLOCK_SELECTOR);
107
108DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
109 struct uac_clock_multiplier_descriptor,
110 validate_clock_multiplier_v2, UAC2_CLOCK_MULTIPLIER);
111DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier_v3,
112 struct uac3_clock_multiplier_descriptor,
113 validate_clock_multiplier_v3, UAC3_CLOCK_MULTIPLIER);
114
115static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
116{
117 unsigned char buf;
118 int ret;
119
120 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
121 UAC2_CS_CUR,
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),
125 &buf, sizeof(buf));
126
127 if (ret < 0)
128 return ret;
129
130 return buf;
131}
132
133static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
134 unsigned char pin)
135{
136 int ret;
137
138 ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
139 UAC2_CS_CUR,
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),
143 &pin, sizeof(pin));
144 if (ret < 0)
145 return ret;
146
147 if (ret != sizeof(pin)) {
148 usb_audio_err(chip,
149 "setting selector (id %d) unexpected length %d\n",
150 selector_id, ret);
151 return -EINVAL;
152 }
153
154 ret = uac_clock_selector_get_val(chip, selector_id);
155 if (ret < 0)
156 return ret;
157
158 if (ret != pin) {
159 usb_audio_err(chip,
160 "setting selector (id %d) to %x failed (current: %d)\n",
161 selector_id, pin, ret);
162 return -EINVAL;
163 }
164
165 return ret;
166}
167
168static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
169 int protocol,
170 int source_id)
171{
172 int err;
173 unsigned char data;
174 struct usb_device *dev = chip->dev;
175 u32 bmControls;
176
177 if (protocol == UAC_VERSION_3) {
178 struct uac3_clock_source_descriptor *cs_desc =
179 snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);
180
181 if (!cs_desc)
182 return 0;
183 bmControls = le32_to_cpu(cs_desc->bmControls);
184 } else { /* UAC_VERSION_1/2 */
185 struct uac_clock_source_descriptor *cs_desc =
186 snd_usb_find_clock_source(chip->ctrl_intf, source_id);
187
188 if (!cs_desc)
189 return 0;
190 bmControls = cs_desc->bmControls;
191 }
192
193 /* If a clock source can't tell us whether it's valid, we assume it is */
194 if (!uac_v2v3_control_is_readable(bmControls,
195 UAC2_CS_CONTROL_CLOCK_VALID))
196 return 1;
197
198 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
199 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
200 UAC2_CS_CONTROL_CLOCK_VALID << 8,
201 snd_usb_ctrl_intf(chip) | (source_id << 8),
202 &data, sizeof(data));
203
204 if (err < 0) {
205 dev_warn(&dev->dev,
206 "%s(): cannot get clock validity for id %d\n",
207 __func__, source_id);
208 return 0;
209 }
210
211 return !!data;
212}
213
214static int __uac_clock_find_source(struct snd_usb_audio *chip, int entity_id,
215 unsigned long *visited, bool validate)
216{
217 struct uac_clock_source_descriptor *source;
218 struct uac_clock_selector_descriptor *selector;
219 struct uac_clock_multiplier_descriptor *multiplier;
220
221 entity_id &= 0xff;
222
223 if (test_and_set_bit(entity_id, visited)) {
224 usb_audio_warn(chip,
225 "%s(): recursive clock topology detected, id %d.\n",
226 __func__, entity_id);
227 return -EINVAL;
228 }
229
230 /* first, see if the ID we're looking for is a clock source already */
231 source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
232 if (source) {
233 entity_id = source->bClockID;
234 if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_2,
235 entity_id)) {
236 usb_audio_err(chip,
237 "clock source %d is not valid, cannot use\n",
238 entity_id);
239 return -ENXIO;
240 }
241 return entity_id;
242 }
243
244 selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
245 if (selector) {
246 int ret, i, cur;
247
248 /* the entity ID we are looking for is a selector.
249 * find out what it currently selects */
250 ret = uac_clock_selector_get_val(chip, selector->bClockID);
251 if (ret < 0)
252 return ret;
253
254 /* Selector values are one-based */
255
256 if (ret > selector->bNrInPins || ret < 1) {
257 usb_audio_err(chip,
258 "%s(): selector reported illegal value, id %d, ret %d\n",
259 __func__, selector->bClockID, ret);
260
261 return -EINVAL;
262 }
263
264 cur = ret;
265 ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
266 visited, validate);
267 if (!validate || ret > 0 || !chip->autoclock)
268 return ret;
269
270 /* The current clock source is invalid, try others. */
271 for (i = 1; i <= selector->bNrInPins; i++) {
272 int err;
273
274 if (i == cur)
275 continue;
276
277 ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
278 visited, true);
279 if (ret < 0)
280 continue;
281
282 err = uac_clock_selector_set_val(chip, entity_id, i);
283 if (err < 0)
284 continue;
285
286 usb_audio_info(chip,
287 "found and selected valid clock source %d\n",
288 ret);
289 return ret;
290 }
291
292 return -ENXIO;
293 }
294
295 /* FIXME: multipliers only act as pass-thru element for now */
296 multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
297 if (multiplier)
298 return __uac_clock_find_source(chip, multiplier->bCSourceID,
299 visited, validate);
300
301 return -EINVAL;
302}
303
304static int __uac3_clock_find_source(struct snd_usb_audio *chip, int entity_id,
305 unsigned long *visited, bool validate)
306{
307 struct uac3_clock_source_descriptor *source;
308 struct uac3_clock_selector_descriptor *selector;
309 struct uac3_clock_multiplier_descriptor *multiplier;
310
311 entity_id &= 0xff;
312
313 if (test_and_set_bit(entity_id, visited)) {
314 usb_audio_warn(chip,
315 "%s(): recursive clock topology detected, id %d.\n",
316 __func__, entity_id);
317 return -EINVAL;
318 }
319
320 /* first, see if the ID we're looking for is a clock source already */
321 source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
322 if (source) {
323 entity_id = source->bClockID;
324 if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_3,
325 entity_id)) {
326 usb_audio_err(chip,
327 "clock source %d is not valid, cannot use\n",
328 entity_id);
329 return -ENXIO;
330 }
331 return entity_id;
332 }
333
334 selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
335 if (selector) {
336 int ret, i, cur;
337
338 /* the entity ID we are looking for is a selector.
339 * find out what it currently selects */
340 ret = uac_clock_selector_get_val(chip, selector->bClockID);
341 if (ret < 0)
342 return ret;
343
344 /* Selector values are one-based */
345
346 if (ret > selector->bNrInPins || ret < 1) {
347 usb_audio_err(chip,
348 "%s(): selector reported illegal value, id %d, ret %d\n",
349 __func__, selector->bClockID, ret);
350
351 return -EINVAL;
352 }
353
354 cur = ret;
355 ret = __uac3_clock_find_source(chip, selector->baCSourceID[ret - 1],
356 visited, validate);
357 if (!validate || ret > 0 || !chip->autoclock)
358 return ret;
359
360 /* The current clock source is invalid, try others. */
361 for (i = 1; i <= selector->bNrInPins; i++) {
362 int err;
363
364 if (i == cur)
365 continue;
366
367 ret = __uac3_clock_find_source(chip, selector->baCSourceID[i - 1],
368 visited, true);
369 if (ret < 0)
370 continue;
371
372 err = uac_clock_selector_set_val(chip, entity_id, i);
373 if (err < 0)
374 continue;
375
376 usb_audio_info(chip,
377 "found and selected valid clock source %d\n",
378 ret);
379 return ret;
380 }
381
382 return -ENXIO;
383 }
384
385 /* FIXME: multipliers only act as pass-thru element for now */
386 multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
387 entity_id);
388 if (multiplier)
389 return __uac3_clock_find_source(chip, multiplier->bCSourceID,
390 visited, validate);
391
392 return -EINVAL;
393}
394
395/*
396 * For all kinds of sample rate settings and other device queries,
397 * the clock source (end-leaf) must be used. However, clock selectors,
398 * clock multipliers and sample rate converters may be specified as
399 * clock source input to terminal. This functions walks the clock path
400 * to its end and tries to find the source.
401 *
402 * The 'visited' bitfield is used internally to detect recursive loops.
403 *
404 * Returns the clock source UnitID (>=0) on success, or an error.
405 */
406int snd_usb_clock_find_source(struct snd_usb_audio *chip, int protocol,
407 int entity_id, bool validate)
408{
409 DECLARE_BITMAP(visited, 256);
410 memset(visited, 0, sizeof(visited));
411
412 switch (protocol) {
413 case UAC_VERSION_2:
414 return __uac_clock_find_source(chip, entity_id, visited,
415 validate);
416 case UAC_VERSION_3:
417 return __uac3_clock_find_source(chip, entity_id, visited,
418 validate);
419 default:
420 return -EINVAL;
421 }
422}
423
424static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
425 struct usb_host_interface *alts,
426 struct audioformat *fmt, int rate)
427{
428 struct usb_device *dev = chip->dev;
429 unsigned int ep;
430 unsigned char data[3];
431 int err, crate;
432
433 if (get_iface_desc(alts)->bNumEndpoints < 1)
434 return -EINVAL;
435 ep = get_endpoint(alts, 0)->bEndpointAddress;
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, ep,
447 data, sizeof(data));
448 if (err < 0) {
449 dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
450 iface, fmt->altsetting, rate, ep);
451 return err;
452 }
453
454 /* Don't check the sample rate for devices which we know don't
455 * support reading */
456 if (snd_usb_get_sample_rate_quirk(chip))
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, ep,
465 data, sizeof(data));
466 if (err < 0) {
467 dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
468 iface, fmt->altsetting, ep);
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 != rate) {
475 dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
476 // runtime->rate = crate;
477 }
478
479 return 0;
480}
481
482static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
483 int altsetting, int clock)
484{
485 struct usb_device *dev = chip->dev;
486 __le32 data;
487 int err;
488
489 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
490 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
491 UAC2_CS_CONTROL_SAM_FREQ << 8,
492 snd_usb_ctrl_intf(chip) | (clock << 8),
493 &data, sizeof(data));
494 if (err < 0) {
495 dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
496 iface, altsetting, err);
497 return 0;
498 }
499
500 return le32_to_cpu(data);
501}
502
503static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
504 struct usb_host_interface *alts,
505 struct audioformat *fmt, int rate)
506{
507 struct usb_device *dev = chip->dev;
508 __le32 data;
509 int err, cur_rate, prev_rate;
510 int clock;
511 bool writeable;
512 u32 bmControls;
513
514 /* First, try to find a valid clock. This may trigger
515 * automatic clock selection if the current clock is not
516 * valid.
517 */
518 clock = snd_usb_clock_find_source(chip, fmt->protocol,
519 fmt->clock, true);
520 if (clock < 0) {
521 /* We did not find a valid clock, but that might be
522 * because the current sample rate does not match an
523 * external clock source. Try again without validation
524 * and we will do another validation after setting the
525 * rate.
526 */
527 clock = snd_usb_clock_find_source(chip, fmt->protocol,
528 fmt->clock, false);
529 if (clock < 0)
530 return clock;
531 }
532
533 prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
534 if (prev_rate == rate)
535 goto validation;
536
537 if (fmt->protocol == UAC_VERSION_3) {
538 struct uac3_clock_source_descriptor *cs_desc;
539
540 cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
541 bmControls = le32_to_cpu(cs_desc->bmControls);
542 } else {
543 struct uac_clock_source_descriptor *cs_desc;
544
545 cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
546 bmControls = cs_desc->bmControls;
547 }
548
549 writeable = uac_v2v3_control_is_writeable(bmControls,
550 UAC2_CS_CONTROL_SAM_FREQ);
551 if (writeable) {
552 data = cpu_to_le32(rate);
553 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
554 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
555 UAC2_CS_CONTROL_SAM_FREQ << 8,
556 snd_usb_ctrl_intf(chip) | (clock << 8),
557 &data, sizeof(data));
558 if (err < 0) {
559 usb_audio_err(chip,
560 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
561 iface, fmt->altsetting, rate, err);
562 return err;
563 }
564
565 cur_rate = get_sample_rate_v2v3(chip, iface,
566 fmt->altsetting, clock);
567 } else {
568 cur_rate = prev_rate;
569 }
570
571 if (cur_rate != rate) {
572 if (!writeable) {
573 usb_audio_warn(chip,
574 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
575 iface, fmt->altsetting, rate, cur_rate);
576 return -ENXIO;
577 }
578 usb_audio_dbg(chip,
579 "current rate %d is different from the runtime rate %d\n",
580 cur_rate, rate);
581 }
582
583 /* Some devices doesn't respond to sample rate changes while the
584 * interface is active. */
585 if (rate != prev_rate) {
586 usb_set_interface(dev, iface, 0);
587 snd_usb_set_interface_quirk(dev);
588 usb_set_interface(dev, iface, fmt->altsetting);
589 snd_usb_set_interface_quirk(dev);
590 }
591
592validation:
593 /* validate clock after rate change */
594 if (!uac_clock_source_is_valid(chip, fmt->protocol, clock))
595 return -ENXIO;
596 return 0;
597}
598
599int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
600 struct usb_host_interface *alts,
601 struct audioformat *fmt, int rate)
602{
603 switch (fmt->protocol) {
604 case UAC_VERSION_1:
605 default:
606 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
607
608 case UAC_VERSION_3:
609 if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
610 if (rate != UAC3_BADD_SAMPLING_RATE)
611 return -ENXIO;
612 else
613 return 0;
614 }
615 /* fall through */
616 case UAC_VERSION_2:
617 return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);
618 }
619}
620