123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- #include <linux/bitops.h>
- #include <linux/init.h>
- #include <linux/string.h>
- #include <linux/usb.h>
- #include <linux/usb/audio.h>
- #include <linux/usb/audio-v2.h>
- #include <sound/core.h>
- #include <sound/info.h>
- #include <sound/pcm.h>
- #include "usbaudio.h"
- #include "card.h"
- #include "helper.h"
- #include "clock.h"
- #include "quirks.h"
- static struct uac_clock_source_descriptor *
- snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
- int clock_id)
- {
- struct uac_clock_source_descriptor *cs = NULL;
- while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
- ctrl_iface->extralen,
- cs, UAC2_CLOCK_SOURCE))) {
- if (cs->bClockID == clock_id)
- return cs;
- }
- return NULL;
- }
- static struct uac_clock_selector_descriptor *
- snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
- int clock_id)
- {
- struct uac_clock_selector_descriptor *cs = NULL;
- while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
- ctrl_iface->extralen,
- cs, UAC2_CLOCK_SELECTOR))) {
- if (cs->bClockID == clock_id)
- return cs;
- }
- return NULL;
- }
- static struct uac_clock_multiplier_descriptor *
- snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
- int clock_id)
- {
- struct uac_clock_multiplier_descriptor *cs = NULL;
- while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
- ctrl_iface->extralen,
- cs, UAC2_CLOCK_MULTIPLIER))) {
- if (cs->bClockID == clock_id)
- return cs;
- }
- return NULL;
- }
- static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
- {
- unsigned char buf;
- int ret;
- ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
- UAC2_CS_CUR,
- USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
- UAC2_CX_CLOCK_SELECTOR << 8,
- snd_usb_ctrl_intf(chip) | (selector_id << 8),
- &buf, sizeof(buf));
- if (ret < 0)
- return ret;
- return buf;
- }
- static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
- unsigned char pin)
- {
- int ret;
- ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
- UAC2_CS_CUR,
- USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
- UAC2_CX_CLOCK_SELECTOR << 8,
- snd_usb_ctrl_intf(chip) | (selector_id << 8),
- &pin, sizeof(pin));
- if (ret < 0)
- return ret;
- if (ret != sizeof(pin)) {
- usb_audio_err(chip,
- "setting selector (id %d) unexpected length %d\n",
- selector_id, ret);
- return -EINVAL;
- }
- ret = uac_clock_selector_get_val(chip, selector_id);
- if (ret < 0)
- return ret;
- if (ret != pin) {
- usb_audio_err(chip,
- "setting selector (id %d) to %x failed (current: %d)\n",
- selector_id, pin, ret);
- return -EINVAL;
- }
- return ret;
- }
- static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
- {
- int err;
- unsigned char data;
- struct usb_device *dev = chip->dev;
- struct uac_clock_source_descriptor *cs_desc =
- snd_usb_find_clock_source(chip->ctrl_intf, source_id);
- if (!cs_desc)
- return 0;
-
- if (!uac2_control_is_readable(cs_desc->bmControls,
- UAC2_CS_CONTROL_CLOCK_VALID - 1))
- return 1;
- err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
- UAC2_CS_CONTROL_CLOCK_VALID << 8,
- snd_usb_ctrl_intf(chip) | (source_id << 8),
- &data, sizeof(data));
- if (err < 0) {
- dev_warn(&dev->dev,
- "%s(): cannot get clock validity for id %d\n",
- __func__, source_id);
- return 0;
- }
- return !!data;
- }
- static int __uac_clock_find_source(struct snd_usb_audio *chip,
- int entity_id, unsigned long *visited,
- bool validate)
- {
- struct uac_clock_source_descriptor *source;
- struct uac_clock_selector_descriptor *selector;
- struct uac_clock_multiplier_descriptor *multiplier;
- entity_id &= 0xff;
- if (test_and_set_bit(entity_id, visited)) {
- usb_audio_warn(chip,
- "%s(): recursive clock topology detected, id %d.\n",
- __func__, entity_id);
- return -EINVAL;
- }
-
- source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
- if (source) {
- entity_id = source->bClockID;
- if (validate && !uac_clock_source_is_valid(chip, entity_id)) {
- usb_audio_err(chip,
- "clock source %d is not valid, cannot use\n",
- entity_id);
- return -ENXIO;
- }
- return entity_id;
- }
- selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
- if (selector) {
- int ret, i, cur;
-
- ret = uac_clock_selector_get_val(chip, selector->bClockID);
- if (ret < 0)
- return ret;
-
- if (ret > selector->bNrInPins || ret < 1) {
- usb_audio_err(chip,
- "%s(): selector reported illegal value, id %d, ret %d\n",
- __func__, selector->bClockID, ret);
- return -EINVAL;
- }
- cur = ret;
- ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
- visited, validate);
- if (!validate || ret > 0 || !chip->autoclock)
- return ret;
-
- for (i = 1; i <= selector->bNrInPins; i++) {
- int err;
- if (i == cur)
- continue;
- ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
- visited, true);
- if (ret < 0)
- continue;
- err = uac_clock_selector_set_val(chip, entity_id, i);
- if (err < 0)
- continue;
- usb_audio_info(chip,
- "found and selected valid clock source %d\n",
- ret);
- return ret;
- }
- return -ENXIO;
- }
-
- multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
- if (multiplier)
- return __uac_clock_find_source(chip, multiplier->bCSourceID,
- visited, validate);
- return -EINVAL;
- }
- int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
- bool validate)
- {
- DECLARE_BITMAP(visited, 256);
- memset(visited, 0, sizeof(visited));
- return __uac_clock_find_source(chip, entity_id, visited, validate);
- }
- static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
- struct usb_host_interface *alts,
- struct audioformat *fmt, int rate)
- {
- struct usb_device *dev = chip->dev;
- unsigned int ep;
- unsigned char data[3];
- int err, crate;
- ep = get_endpoint(alts, 0)->bEndpointAddress;
-
- if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
- return 0;
- data[0] = rate;
- data[1] = rate >> 8;
- data[2] = rate >> 16;
- if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
- USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
- UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
- data, sizeof(data))) < 0) {
- dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
- iface, fmt->altsetting, rate, ep);
- return err;
- }
-
- if (snd_usb_get_sample_rate_quirk(chip))
- return 0;
- if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
- USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
- UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
- data, sizeof(data))) < 0) {
- dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
- iface, fmt->altsetting, ep);
- return 0;
- }
- crate = data[0] | (data[1] << 8) | (data[2] << 16);
- if (crate != rate) {
- dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
-
- }
- return 0;
- }
- static int get_sample_rate_v2(struct snd_usb_audio *chip, int iface,
- int altsetting, int clock)
- {
- struct usb_device *dev = chip->dev;
- __le32 data;
- int err;
- err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
- UAC2_CS_CONTROL_SAM_FREQ << 8,
- snd_usb_ctrl_intf(chip) | (clock << 8),
- &data, sizeof(data));
- if (err < 0) {
- dev_warn(&dev->dev, "%d:%d: cannot get freq (v2): err %d\n",
- iface, altsetting, err);
- return 0;
- }
- return le32_to_cpu(data);
- }
- static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
- struct usb_host_interface *alts,
- struct audioformat *fmt, int rate)
- {
- struct usb_device *dev = chip->dev;
- __le32 data;
- int err, cur_rate, prev_rate;
- int clock;
- bool writeable;
- struct uac_clock_source_descriptor *cs_desc;
- clock = snd_usb_clock_find_source(chip, fmt->clock, true);
- if (clock < 0)
- return clock;
- prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
- if (prev_rate == rate)
- return 0;
- cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
- writeable = uac2_control_is_writeable(cs_desc->bmControls, UAC2_CS_CONTROL_SAM_FREQ - 1);
- if (writeable) {
- data = cpu_to_le32(rate);
- err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
- UAC2_CS_CONTROL_SAM_FREQ << 8,
- snd_usb_ctrl_intf(chip) | (clock << 8),
- &data, sizeof(data));
- if (err < 0) {
- usb_audio_err(chip,
- "%d:%d: cannot set freq %d (v2): err %d\n",
- iface, fmt->altsetting, rate, err);
- return err;
- }
- cur_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
- } else {
- cur_rate = prev_rate;
- }
- if (cur_rate != rate) {
- if (!writeable) {
- usb_audio_warn(chip,
- "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
- iface, fmt->altsetting, rate, cur_rate);
- return -ENXIO;
- }
- usb_audio_dbg(chip,
- "current rate %d is different from the runtime rate %d\n",
- cur_rate, rate);
- }
-
- if (rate != prev_rate) {
- usb_set_interface(dev, iface, 0);
- snd_usb_set_interface_quirk(dev);
- usb_set_interface(dev, iface, fmt->altsetting);
- snd_usb_set_interface_quirk(dev);
- }
- return 0;
- }
- int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
- struct usb_host_interface *alts,
- struct audioformat *fmt, int rate)
- {
- switch (fmt->protocol) {
- case UAC_VERSION_1:
- default:
- return set_sample_rate_v1(chip, iface, alts, fmt, rate);
- case UAC_VERSION_2:
- return set_sample_rate_v2(chip, iface, alts, fmt, rate);
- }
- }
|