Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Clock domain and sample rate management functions
0004  */
0005 
0006 #include <linux/bitops.h>
0007 #include <linux/init.h>
0008 #include <linux/string.h>
0009 #include <linux/usb.h>
0010 #include <linux/usb/audio.h>
0011 #include <linux/usb/audio-v2.h>
0012 #include <linux/usb/audio-v3.h>
0013 
0014 #include <sound/core.h>
0015 #include <sound/info.h>
0016 #include <sound/pcm.h>
0017 
0018 #include "usbaudio.h"
0019 #include "card.h"
0020 #include "helper.h"
0021 #include "clock.h"
0022 #include "quirks.h"
0023 
0024 union uac23_clock_source_desc {
0025     struct uac_clock_source_descriptor v2;
0026     struct uac3_clock_source_descriptor v3;
0027 };
0028 
0029 union uac23_clock_selector_desc {
0030     struct uac_clock_selector_descriptor v2;
0031     struct uac3_clock_selector_descriptor v3;
0032 };
0033 
0034 union uac23_clock_multiplier_desc {
0035     struct uac_clock_multiplier_descriptor v2;
0036     struct uac_clock_multiplier_descriptor v3;
0037 };
0038 
0039 #define GET_VAL(p, proto, field) \
0040     ((proto) == UAC_VERSION_3 ? (p)->v3.field : (p)->v2.field)
0041 
0042 static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
0043                  bool (*validator)(void *, int, int),
0044                  u8 type, int proto)
0045 {
0046     void *cs = NULL;
0047 
0048     while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
0049                          cs, type))) {
0050         if (validator(cs, id, proto))
0051             return cs;
0052     }
0053 
0054     return NULL;
0055 }
0056 
0057 static bool validate_clock_source(void *p, int id, int proto)
0058 {
0059     union uac23_clock_source_desc *cs = p;
0060 
0061     return GET_VAL(cs, proto, bClockID) == id;
0062 }
0063 
0064 static bool validate_clock_selector(void *p, int id, int proto)
0065 {
0066     union uac23_clock_selector_desc *cs = p;
0067 
0068     return GET_VAL(cs, proto, bClockID) == id;
0069 }
0070 
0071 static bool validate_clock_multiplier(void *p, int id, int proto)
0072 {
0073     union uac23_clock_multiplier_desc *cs = p;
0074 
0075     return GET_VAL(cs, proto, bClockID) == id;
0076 }
0077 
0078 #define DEFINE_FIND_HELPER(name, obj, validator, type2, type3)      \
0079 static obj *name(struct snd_usb_audio *chip, int id, int proto) \
0080 {                                   \
0081     return find_uac_clock_desc(chip->ctrl_intf, id, validator,  \
0082                    proto == UAC_VERSION_3 ? (type3) : (type2), \
0083                    proto);              \
0084 }
0085 
0086 DEFINE_FIND_HELPER(snd_usb_find_clock_source,
0087            union uac23_clock_source_desc, validate_clock_source,
0088            UAC2_CLOCK_SOURCE, UAC3_CLOCK_SOURCE);
0089 DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
0090            union uac23_clock_selector_desc, validate_clock_selector,
0091            UAC2_CLOCK_SELECTOR, UAC3_CLOCK_SELECTOR);
0092 DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
0093            union uac23_clock_multiplier_desc, validate_clock_multiplier,
0094            UAC2_CLOCK_MULTIPLIER, UAC3_CLOCK_MULTIPLIER);
0095 
0096 static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
0097 {
0098     unsigned char buf;
0099     int ret;
0100 
0101     ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
0102                   UAC2_CS_CUR,
0103                   USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
0104                   UAC2_CX_CLOCK_SELECTOR << 8,
0105                   snd_usb_ctrl_intf(chip) | (selector_id << 8),
0106                   &buf, sizeof(buf));
0107 
0108     if (ret < 0)
0109         return ret;
0110 
0111     return buf;
0112 }
0113 
0114 static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
0115                     unsigned char pin)
0116 {
0117     int ret;
0118 
0119     ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
0120                   UAC2_CS_CUR,
0121                   USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
0122                   UAC2_CX_CLOCK_SELECTOR << 8,
0123                   snd_usb_ctrl_intf(chip) | (selector_id << 8),
0124                   &pin, sizeof(pin));
0125     if (ret < 0)
0126         return ret;
0127 
0128     if (ret != sizeof(pin)) {
0129         usb_audio_err(chip,
0130             "setting selector (id %d) unexpected length %d\n",
0131             selector_id, ret);
0132         return -EINVAL;
0133     }
0134 
0135     ret = uac_clock_selector_get_val(chip, selector_id);
0136     if (ret < 0)
0137         return ret;
0138 
0139     if (ret != pin) {
0140         usb_audio_err(chip,
0141             "setting selector (id %d) to %x failed (current: %d)\n",
0142             selector_id, pin, ret);
0143         return -EINVAL;
0144     }
0145 
0146     return ret;
0147 }
0148 
0149 static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
0150                         const struct audioformat *fmt,
0151                         int source_id)
0152 {
0153     bool ret = false;
0154     int count;
0155     unsigned char data;
0156     struct usb_device *dev = chip->dev;
0157     union uac23_clock_source_desc *cs_desc;
0158 
0159     cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol);
0160     if (!cs_desc)
0161         return false;
0162 
0163     if (fmt->protocol == UAC_VERSION_2) {
0164         /*
0165          * Assume the clock is valid if clock source supports only one
0166          * single sample rate, the terminal is connected directly to it
0167          * (there is no clock selector) and clock type is internal.
0168          * This is to deal with some Denon DJ controllers that always
0169          * reports that clock is invalid.
0170          */
0171         if (fmt->nr_rates == 1 &&
0172             (fmt->clock & 0xff) == cs_desc->v2.bClockID &&
0173             (cs_desc->v2.bmAttributes & 0x3) !=
0174                 UAC_CLOCK_SOURCE_TYPE_EXT)
0175             return true;
0176     }
0177 
0178     /*
0179      * MOTU MicroBook IIc
0180      * Sample rate changes takes more than 2 seconds for this device. Clock
0181      * validity request returns false during that period.
0182      */
0183     if (chip->usb_id == USB_ID(0x07fd, 0x0004)) {
0184         count = 0;
0185 
0186         while ((!ret) && (count < 50)) {
0187             int err;
0188 
0189             msleep(100);
0190 
0191             err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
0192                           USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0193                           UAC2_CS_CONTROL_CLOCK_VALID << 8,
0194                           snd_usb_ctrl_intf(chip) | (source_id << 8),
0195                           &data, sizeof(data));
0196             if (err < 0) {
0197                 dev_warn(&dev->dev,
0198                      "%s(): cannot get clock validity for id %d\n",
0199                        __func__, source_id);
0200                 return false;
0201             }
0202 
0203             ret = !!data;
0204             count++;
0205         }
0206     }
0207 
0208     return ret;
0209 }
0210 
0211 static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
0212                       const struct audioformat *fmt,
0213                       int source_id)
0214 {
0215     int err;
0216     unsigned char data;
0217     struct usb_device *dev = chip->dev;
0218     u32 bmControls;
0219     union uac23_clock_source_desc *cs_desc;
0220 
0221     cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol);
0222     if (!cs_desc)
0223         return false;
0224 
0225     if (fmt->protocol == UAC_VERSION_3)
0226         bmControls = le32_to_cpu(cs_desc->v3.bmControls);
0227     else
0228         bmControls = cs_desc->v2.bmControls;
0229 
0230     /* If a clock source can't tell us whether it's valid, we assume it is */
0231     if (!uac_v2v3_control_is_readable(bmControls,
0232                       UAC2_CS_CONTROL_CLOCK_VALID))
0233         return true;
0234 
0235     err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
0236                   USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0237                   UAC2_CS_CONTROL_CLOCK_VALID << 8,
0238                   snd_usb_ctrl_intf(chip) | (source_id << 8),
0239                   &data, sizeof(data));
0240 
0241     if (err < 0) {
0242         dev_warn(&dev->dev,
0243              "%s(): cannot get clock validity for id %d\n",
0244                __func__, source_id);
0245         return false;
0246     }
0247 
0248     if (data)
0249         return true;
0250     else
0251         return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
0252 }
0253 
0254 static int __uac_clock_find_source(struct snd_usb_audio *chip,
0255                    const struct audioformat *fmt, int entity_id,
0256                    unsigned long *visited, bool validate)
0257 {
0258     union uac23_clock_source_desc *source;
0259     union uac23_clock_selector_desc *selector;
0260     union uac23_clock_multiplier_desc *multiplier;
0261     int ret, i, cur, err, pins, clock_id;
0262     const u8 *sources;
0263     int proto = fmt->protocol;
0264 
0265     entity_id &= 0xff;
0266 
0267     if (test_and_set_bit(entity_id, visited)) {
0268         usb_audio_warn(chip,
0269              "%s(): recursive clock topology detected, id %d.\n",
0270              __func__, entity_id);
0271         return -EINVAL;
0272     }
0273 
0274     /* first, see if the ID we're looking at is a clock source already */
0275     source = snd_usb_find_clock_source(chip, entity_id, proto);
0276     if (source) {
0277         entity_id = GET_VAL(source, proto, bClockID);
0278         if (validate && !uac_clock_source_is_valid(chip, fmt,
0279                                 entity_id)) {
0280             usb_audio_err(chip,
0281                 "clock source %d is not valid, cannot use\n",
0282                 entity_id);
0283             return -ENXIO;
0284         }
0285         return entity_id;
0286     }
0287 
0288     selector = snd_usb_find_clock_selector(chip, entity_id, proto);
0289     if (selector) {
0290         pins = GET_VAL(selector, proto, bNrInPins);
0291         clock_id = GET_VAL(selector, proto, bClockID);
0292         sources = GET_VAL(selector, proto, baCSourceID);
0293         cur = 0;
0294 
0295         if (pins == 1) {
0296             ret = 1;
0297             goto find_source;
0298         }
0299 
0300         /* the entity ID we are looking at is a selector.
0301          * find out what it currently selects */
0302         ret = uac_clock_selector_get_val(chip, clock_id);
0303         if (ret < 0) {
0304             if (!chip->autoclock)
0305                 return ret;
0306             goto find_others;
0307         }
0308 
0309         /* Selector values are one-based */
0310 
0311         if (ret > pins || ret < 1) {
0312             usb_audio_err(chip,
0313                 "%s(): selector reported illegal value, id %d, ret %d\n",
0314                 __func__, clock_id, ret);
0315 
0316             if (!chip->autoclock)
0317                 return -EINVAL;
0318             goto find_others;
0319         }
0320 
0321     find_source:
0322         cur = ret;
0323         ret = __uac_clock_find_source(chip, fmt,
0324                           sources[ret - 1],
0325                           visited, validate);
0326         if (ret > 0) {
0327             /* Skip setting clock selector again for some devices */
0328             if (chip->quirk_flags & QUIRK_FLAG_SKIP_CLOCK_SELECTOR)
0329                 return ret;
0330             err = uac_clock_selector_set_val(chip, entity_id, cur);
0331             if (err < 0)
0332                 return err;
0333         }
0334 
0335         if (!validate || ret > 0 || !chip->autoclock)
0336             return ret;
0337 
0338     find_others:
0339         /* The current clock source is invalid, try others. */
0340         for (i = 1; i <= pins; i++) {
0341             if (i == cur)
0342                 continue;
0343 
0344             ret = __uac_clock_find_source(chip, fmt,
0345                               sources[i - 1],
0346                               visited, true);
0347             if (ret < 0)
0348                 continue;
0349 
0350             err = uac_clock_selector_set_val(chip, entity_id, i);
0351             if (err < 0)
0352                 continue;
0353 
0354             usb_audio_info(chip,
0355                  "found and selected valid clock source %d\n",
0356                  ret);
0357             return ret;
0358         }
0359 
0360         return -ENXIO;
0361     }
0362 
0363     /* FIXME: multipliers only act as pass-thru element for now */
0364     multiplier = snd_usb_find_clock_multiplier(chip, entity_id, proto);
0365     if (multiplier)
0366         return __uac_clock_find_source(chip, fmt,
0367                            GET_VAL(multiplier, proto, bCSourceID),
0368                            visited, validate);
0369 
0370     return -EINVAL;
0371 }
0372 
0373 /*
0374  * For all kinds of sample rate settings and other device queries,
0375  * the clock source (end-leaf) must be used. However, clock selectors,
0376  * clock multipliers and sample rate converters may be specified as
0377  * clock source input to terminal. This functions walks the clock path
0378  * to its end and tries to find the source.
0379  *
0380  * The 'visited' bitfield is used internally to detect recursive loops.
0381  *
0382  * Returns the clock source UnitID (>=0) on success, or an error.
0383  */
0384 int snd_usb_clock_find_source(struct snd_usb_audio *chip,
0385                   const struct audioformat *fmt, bool validate)
0386 {
0387     DECLARE_BITMAP(visited, 256);
0388     memset(visited, 0, sizeof(visited));
0389 
0390     switch (fmt->protocol) {
0391     case UAC_VERSION_2:
0392     case UAC_VERSION_3:
0393         return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
0394                            validate);
0395     default:
0396         return -EINVAL;
0397     }
0398 }
0399 
0400 static int set_sample_rate_v1(struct snd_usb_audio *chip,
0401                   const struct audioformat *fmt, int rate)
0402 {
0403     struct usb_device *dev = chip->dev;
0404     unsigned char data[3];
0405     int err, crate;
0406 
0407     /* if endpoint doesn't have sampling rate control, bail out */
0408     if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
0409         return 0;
0410 
0411     data[0] = rate;
0412     data[1] = rate >> 8;
0413     data[2] = rate >> 16;
0414     err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
0415                   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0416                   UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
0417                   fmt->endpoint, data, sizeof(data));
0418     if (err < 0) {
0419         dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
0420             fmt->iface, fmt->altsetting, rate, fmt->endpoint);
0421         return err;
0422     }
0423 
0424     /* Don't check the sample rate for devices which we know don't
0425      * support reading */
0426     if (chip->quirk_flags & QUIRK_FLAG_GET_SAMPLE_RATE)
0427         return 0;
0428     /* the firmware is likely buggy, don't repeat to fail too many times */
0429     if (chip->sample_rate_read_error > 2)
0430         return 0;
0431 
0432     err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
0433                   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
0434                   UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
0435                   fmt->endpoint, data, sizeof(data));
0436     if (err < 0) {
0437         dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
0438             fmt->iface, fmt->altsetting, fmt->endpoint);
0439         chip->sample_rate_read_error++;
0440         return 0; /* some devices don't support reading */
0441     }
0442 
0443     crate = data[0] | (data[1] << 8) | (data[2] << 16);
0444     if (!crate) {
0445         dev_info(&dev->dev, "failed to read current rate; disabling the check\n");
0446         chip->sample_rate_read_error = 3; /* three strikes, see above */
0447         return 0;
0448     }
0449 
0450     if (crate != rate) {
0451         dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
0452         // runtime->rate = crate;
0453     }
0454 
0455     return 0;
0456 }
0457 
0458 static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
0459                   int altsetting, int clock)
0460 {
0461     struct usb_device *dev = chip->dev;
0462     __le32 data;
0463     int err;
0464 
0465     err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
0466                   USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0467                   UAC2_CS_CONTROL_SAM_FREQ << 8,
0468                   snd_usb_ctrl_intf(chip) | (clock << 8),
0469                   &data, sizeof(data));
0470     if (err < 0) {
0471         dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
0472              iface, altsetting, err);
0473         return 0;
0474     }
0475 
0476     return le32_to_cpu(data);
0477 }
0478 
0479 /*
0480  * Try to set the given sample rate:
0481  *
0482  * Return 0 if the clock source is read-only, the actual rate on success,
0483  * or a negative error code.
0484  *
0485  * This function gets called from format.c to validate each sample rate, too.
0486  * Hence no message is shown upon error
0487  */
0488 int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
0489                  const struct audioformat *fmt,
0490                  int clock, int rate)
0491 {
0492     bool writeable;
0493     u32 bmControls;
0494     __le32 data;
0495     int err;
0496     union uac23_clock_source_desc *cs_desc;
0497 
0498     cs_desc = snd_usb_find_clock_source(chip, clock, fmt->protocol);
0499 
0500     if (!cs_desc)
0501         return 0;
0502 
0503     if (fmt->protocol == UAC_VERSION_3)
0504         bmControls = le32_to_cpu(cs_desc->v3.bmControls);
0505     else
0506         bmControls = cs_desc->v2.bmControls;
0507 
0508     writeable = uac_v2v3_control_is_writeable(bmControls,
0509                           UAC2_CS_CONTROL_SAM_FREQ);
0510     if (!writeable)
0511         return 0;
0512 
0513     data = cpu_to_le32(rate);
0514     err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR,
0515                   USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0516                   UAC2_CS_CONTROL_SAM_FREQ << 8,
0517                   snd_usb_ctrl_intf(chip) | (clock << 8),
0518                   &data, sizeof(data));
0519     if (err < 0)
0520         return err;
0521 
0522     return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
0523 }
0524 
0525 static int set_sample_rate_v2v3(struct snd_usb_audio *chip,
0526                 const struct audioformat *fmt, int rate)
0527 {
0528     int cur_rate, prev_rate;
0529     int clock;
0530 
0531     /* First, try to find a valid clock. This may trigger
0532      * automatic clock selection if the current clock is not
0533      * valid.
0534      */
0535     clock = snd_usb_clock_find_source(chip, fmt, true);
0536     if (clock < 0) {
0537         /* We did not find a valid clock, but that might be
0538          * because the current sample rate does not match an
0539          * external clock source. Try again without validation
0540          * and we will do another validation after setting the
0541          * rate.
0542          */
0543         clock = snd_usb_clock_find_source(chip, fmt, false);
0544 
0545         /* Hardcoded sample rates */
0546         if (chip->quirk_flags & QUIRK_FLAG_IGNORE_CLOCK_SOURCE)
0547             return 0;
0548 
0549         if (clock < 0)
0550             return clock;
0551     }
0552 
0553     prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
0554     if (prev_rate == rate)
0555         goto validation;
0556 
0557     cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate);
0558     if (cur_rate < 0) {
0559         usb_audio_err(chip,
0560                   "%d:%d: cannot set freq %d (v2/v3): err %d\n",
0561                   fmt->iface, fmt->altsetting, rate, cur_rate);
0562         return cur_rate;
0563     }
0564 
0565     if (!cur_rate)
0566         cur_rate = prev_rate;
0567 
0568     if (cur_rate != rate) {
0569         usb_audio_dbg(chip,
0570                   "%d:%d: freq mismatch: req %d, clock runs @%d\n",
0571                   fmt->iface, fmt->altsetting, rate, cur_rate);
0572         /* continue processing */
0573     }
0574 
0575     /* FIXME - TEAC devices require the immediate interface setup */
0576     if (USB_ID_VENDOR(chip->usb_id) == 0x0644) {
0577         bool cur_base_48k = (rate % 48000 == 0);
0578         bool prev_base_48k = (prev_rate % 48000 == 0);
0579         if (cur_base_48k != prev_base_48k) {
0580             usb_set_interface(chip->dev, fmt->iface, fmt->altsetting);
0581             if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
0582                 msleep(50);
0583         }
0584     }
0585 
0586 validation:
0587     /* validate clock after rate change */
0588     if (!uac_clock_source_is_valid(chip, fmt, clock))
0589         return -ENXIO;
0590     return 0;
0591 }
0592 
0593 int snd_usb_init_sample_rate(struct snd_usb_audio *chip,
0594                  const struct audioformat *fmt, int rate)
0595 {
0596     usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n",
0597               fmt->iface, fmt->altsetting, rate, fmt->clock);
0598 
0599     switch (fmt->protocol) {
0600     case UAC_VERSION_1:
0601     default:
0602         return set_sample_rate_v1(chip, fmt, rate);
0603 
0604     case UAC_VERSION_3:
0605         if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
0606             if (rate != UAC3_BADD_SAMPLING_RATE)
0607                 return -ENXIO;
0608             else
0609                 return 0;
0610         }
0611         fallthrough;
0612     case UAC_VERSION_2:
0613         return set_sample_rate_v2v3(chip, fmt, rate);
0614     }
0615 }
0616