Linux 4.19.140
[linux/fpc-iii.git] / sound / usb / clock.c
blobbfe5540030b8066f0ff0a4cf0f0afb4f16b28e95
1 /*
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>
32 #include "usbaudio.h"
33 #include "card.h"
34 #include "helper.h"
35 #include "clock.h"
36 #include "quirks.h"
38 static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
39 bool (*validator)(void *, int), u8 type)
41 void *cs = NULL;
43 while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
44 cs, type))) {
45 if (validator(cs, id))
46 return cs;
49 return NULL;
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) \
90 { \
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)
117 unsigned char buf;
118 int ret;
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));
127 if (ret < 0)
128 return ret;
130 return buf;
133 static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
134 unsigned char pin)
136 int ret;
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;
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;
154 ret = uac_clock_selector_get_val(chip, selector_id);
155 if (ret < 0)
156 return ret;
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;
165 return 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,
176 int source_id)
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);
182 if (!cs_desc)
183 return false;
185 return (fmt->nr_rates == 1 &&
186 (fmt->clock & 0xff) == cs_desc->bClockID &&
187 (cs_desc->bmAttributes & 0x3) !=
188 UAC_CLOCK_SOURCE_TYPE_EXT);
191 return false;
194 static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
195 struct audioformat *fmt,
196 int source_id)
198 int err;
199 unsigned char data;
200 struct usb_device *dev = chip->dev;
201 u32 bmControls;
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);
207 if (!cs_desc)
208 return false;
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);
214 if (!cs_desc)
215 return false;
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))
222 return true;
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));
230 if (err < 0) {
231 dev_warn(&dev->dev,
232 "%s(): cannot get clock validity for id %d\n",
233 __func__, source_id);
234 return false;
237 if (data)
238 return true;
239 else
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;
251 entity_id &= 0xff;
253 if (test_and_set_bit(entity_id, visited)) {
254 usb_audio_warn(chip,
255 "%s(): recursive clock topology detected, id %d.\n",
256 __func__, entity_id);
257 return -EINVAL;
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);
262 if (source) {
263 entity_id = source->bClockID;
264 if (validate && !uac_clock_source_is_valid(chip, fmt,
265 entity_id)) {
266 usb_audio_err(chip,
267 "clock source %d is not valid, cannot use\n",
268 entity_id);
269 return -ENXIO;
271 return entity_id;
274 selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
275 if (selector) {
276 int ret, i, cur;
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);
281 if (ret < 0)
282 return ret;
284 /* Selector values are one-based */
286 if (ret > selector->bNrInPins || ret < 1) {
287 usb_audio_err(chip,
288 "%s(): selector reported illegal value, id %d, ret %d\n",
289 __func__, selector->bClockID, ret);
291 return -EINVAL;
294 cur = ret;
295 ret = __uac_clock_find_source(chip, fmt,
296 selector->baCSourceID[ret - 1],
297 visited, validate);
298 if (!validate || ret > 0 || !chip->autoclock)
299 return ret;
301 /* The current clock source is invalid, try others. */
302 for (i = 1; i <= selector->bNrInPins; i++) {
303 int err;
305 if (i == cur)
306 continue;
308 ret = __uac_clock_find_source(chip, fmt,
309 selector->baCSourceID[i - 1],
310 visited, true);
311 if (ret < 0)
312 continue;
314 err = uac_clock_selector_set_val(chip, entity_id, i);
315 if (err < 0)
316 continue;
318 usb_audio_info(chip,
319 "found and selected valid clock source %d\n",
320 ret);
321 return ret;
324 return -ENXIO;
327 /* FIXME: multipliers only act as pass-thru element for now */
328 multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
329 if (multiplier)
330 return __uac_clock_find_source(chip, fmt,
331 multiplier->bCSourceID,
332 visited, validate);
334 return -EINVAL;
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;
345 entity_id &= 0xff;
347 if (test_and_set_bit(entity_id, visited)) {
348 usb_audio_warn(chip,
349 "%s(): recursive clock topology detected, id %d.\n",
350 __func__, entity_id);
351 return -EINVAL;
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);
356 if (source) {
357 entity_id = source->bClockID;
358 if (validate && !uac_clock_source_is_valid(chip, fmt,
359 entity_id)) {
360 usb_audio_err(chip,
361 "clock source %d is not valid, cannot use\n",
362 entity_id);
363 return -ENXIO;
365 return entity_id;
368 selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
369 if (selector) {
370 int ret, i, cur;
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);
375 if (ret < 0)
376 return ret;
378 /* Selector values are one-based */
380 if (ret > selector->bNrInPins || ret < 1) {
381 usb_audio_err(chip,
382 "%s(): selector reported illegal value, id %d, ret %d\n",
383 __func__, selector->bClockID, ret);
385 return -EINVAL;
388 cur = ret;
389 ret = __uac3_clock_find_source(chip, fmt,
390 selector->baCSourceID[ret - 1],
391 visited, validate);
392 if (!validate || ret > 0 || !chip->autoclock)
393 return ret;
395 /* The current clock source is invalid, try others. */
396 for (i = 1; i <= selector->bNrInPins; i++) {
397 int err;
399 if (i == cur)
400 continue;
402 ret = __uac3_clock_find_source(chip, fmt,
403 selector->baCSourceID[i - 1],
404 visited, true);
405 if (ret < 0)
406 continue;
408 err = uac_clock_selector_set_val(chip, entity_id, i);
409 if (err < 0)
410 continue;
412 usb_audio_info(chip,
413 "found and selected valid clock source %d\n",
414 ret);
415 return ret;
418 return -ENXIO;
421 /* FIXME: multipliers only act as pass-thru element for now */
422 multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
423 entity_id);
424 if (multiplier)
425 return __uac3_clock_find_source(chip, fmt,
426 multiplier->bCSourceID,
427 visited, validate);
429 return -EINVAL;
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) {
450 case UAC_VERSION_2:
451 return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
452 validate);
453 case UAC_VERSION_3:
454 return __uac3_clock_find_source(chip, fmt, fmt->clock, visited,
455 validate);
456 default:
457 return -EINVAL;
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;
466 unsigned int ep;
467 unsigned char data[3];
468 int err, crate;
470 if (get_iface_desc(alts)->bNumEndpoints < 1)
471 return -EINVAL;
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))
476 return 0;
478 data[0] = rate;
479 data[1] = rate >> 8;
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,
484 data, sizeof(data));
485 if (err < 0) {
486 dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
487 iface, fmt->altsetting, rate, ep);
488 return err;
491 /* Don't check the sample rate for devices which we know don't
492 * support reading */
493 if (snd_usb_get_sample_rate_quirk(chip))
494 return 0;
495 /* the firmware is likely buggy, don't repeat to fail too many times */
496 if (chip->sample_rate_read_error > 2)
497 return 0;
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,
502 data, sizeof(data));
503 if (err < 0) {
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);
511 if (crate != rate) {
512 dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
513 // runtime->rate = crate;
516 return 0;
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;
523 __le32 data;
524 int err;
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));
531 if (err < 0) {
532 dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
533 iface, altsetting, err);
534 return 0;
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;
545 __le32 data;
546 int err, cur_rate, prev_rate;
547 int clock;
548 bool writeable;
549 u32 bmControls;
551 /* First, try to find a valid clock. This may trigger
552 * automatic clock selection if the current clock is not
553 * valid.
555 clock = snd_usb_clock_find_source(chip, fmt, true);
556 if (clock < 0) {
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
561 * rate.
563 clock = snd_usb_clock_find_source(chip, fmt, false);
564 if (clock < 0)
565 return clock;
568 prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
569 if (prev_rate == rate)
570 goto validation;
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);
577 } else {
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);
586 if (writeable) {
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));
593 if (err < 0) {
594 usb_audio_err(chip,
595 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
596 iface, fmt->altsetting, rate, err);
597 return err;
600 cur_rate = get_sample_rate_v2v3(chip, iface,
601 fmt->altsetting, clock);
602 } else {
603 cur_rate = prev_rate;
606 if (cur_rate != rate) {
607 if (!writeable) {
608 usb_audio_warn(chip,
609 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
610 iface, fmt->altsetting, rate, cur_rate);
611 return -ENXIO;
613 usb_audio_dbg(chip,
614 "current rate %d is different from the runtime rate %d\n",
615 cur_rate, rate);
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);
627 validation:
628 /* validate clock after rate change */
629 if (!uac_clock_source_is_valid(chip, fmt, clock))
630 return -ENXIO;
631 return 0;
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) {
639 case UAC_VERSION_1:
640 default:
641 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
643 case UAC_VERSION_3:
644 if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
645 if (rate != UAC3_BADD_SAMPLING_RATE)
646 return -ENXIO;
647 else
648 return 0;
650 /* fall through */
651 case UAC_VERSION_2:
652 return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);