diff options
author | 2023-02-21 18:24:12 -0800 | |
---|---|---|
committer | 2023-02-21 18:24:12 -0800 | |
commit | 5b7c4cabbb65f5c469464da6c5f614cbd7f730f2 (patch) | |
tree | cc5c2d0a898769fd59549594fedb3ee6f84e59a0 /sound/usb/clock.c | |
download | linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.tar.gz linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.zip |
Merge tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-nextgrafted
Pull networking updates from Jakub Kicinski:
"Core:
- Add dedicated kmem_cache for typical/small skb->head, avoid having
to access struct page at kfree time, and improve memory use.
- Introduce sysctl to set default RPS configuration for new netdevs.
- Define Netlink protocol specification format which can be used to
describe messages used by each family and auto-generate parsers.
Add tools for generating kernel data structures and uAPI headers.
- Expose all net/core sysctls inside netns.
- Remove 4s sleep in netpoll if carrier is instantly detected on
boot.
- Add configurable limit of MDB entries per port, and port-vlan.
- Continue populating drop reasons throughout the stack.
- Retire a handful of legacy Qdiscs and classifiers.
Protocols:
- Support IPv4 big TCP (TSO frames larger than 64kB).
- Add IP_LOCAL_PORT_RANGE socket option, to control local port range
on socket by socket basis.
- Track and report in procfs number of MPTCP sockets used.
- Support mixing IPv4 and IPv6 flows in the in-kernel MPTCP path
manager.
- IPv6: don't check net.ipv6.route.max_size and rely on garbage
collection to free memory (similarly to IPv4).
- Support Penultimate Segment Pop (PSP) flavor in SRv6 (RFC8986).
- ICMP: add per-rate limit counters.
- Add support for user scanning requests in ieee802154.
- Remove static WEP support.
- Support minimal Wi-Fi 7 Extremely High Throughput (EHT) rate
reporting.
- WiFi 7 EHT channel puncturing support (client & AP).
BPF:
- Add a rbtree data structure following the "next-gen data structure"
precedent set by recently added linked list, that is, by using
kfunc + kptr instead of adding a new BPF map type.
- Expose XDP hints via kfuncs with initial support for RX hash and
timestamp metadata.
- Add BPF_F_NO_TUNNEL_KEY extension to bpf_skb_set_tunnel_key to
better support decap on GRE tunnel devices not operating in collect
metadata.
- Improve x86 JIT's codegen for PROBE_MEM runtime error checks.
- Remove the need for trace_printk_lock for bpf_trace_printk and
bpf_trace_vprintk helpers.
- Extend libbpf's bpf_tracing.h support for tracing arguments of
kprobes/uprobes and syscall as a special case.
- Significantly reduce the search time for module symbols by
livepatch and BPF.
- Enable cpumasks to be used as kptrs, which is useful for tracing
programs tracking which tasks end up running on which CPUs in
different time intervals.
- Add support for BPF trampoline on s390x and riscv64.
- Add capability to export the XDP features supported by the NIC.
- Add __bpf_kfunc tag for marking kernel functions as kfuncs.
- Add cgroup.memory=nobpf kernel parameter option to disable BPF
memory accounting for container environments.
Netfilter:
- Remove the CLUSTERIP target. It has been marked as obsolete for
years, and we still have WARN splats wrt races of the out-of-band
/proc interface installed by this target.
- Add 'destroy' commands to nf_tables. They are identical to the
existing 'delete' commands, but do not return an error if the
referenced object (set, chain, rule...) did not exist.
Driver API:
- Improve cpumask_local_spread() locality to help NICs set the right
IRQ affinity on AMD platforms.
- Separate C22 and C45 MDIO bus transactions more clearly.
- Introduce new DCB table to control DSCP rewrite on egress.
- Support configuration of Physical Layer Collision Avoidance (PLCA)
Reconciliation Sublayer (RS) (802.3cg-2019). Modern version of
shared medium Ethernet.
- Support for MAC Merge layer (IEEE 802.3-2018 clause 99). Allowing
preemption of low priority frames by high priority frames.
- Add support for controlling MACSec offload using netlink SET.
- Rework devlink instance refcounts to allow registration and
de-registration under the instance lock. Split the code into
multiple files, drop some of the unnecessarily granular locks and
factor out common parts of netlink operation handling.
- Add TX frame aggregation parameters (for USB drivers).
- Add a new attr TCA_EXT_WARN_MSG to report TC (offload) warning
messages with notifications for debug.
- Allow offloading of UDP NEW connections via act_ct.
- Add support for per action HW stats in TC.
- Support hardware miss to TC action (continue processing in SW from
a specific point in the action chain).
- Warn if old Wireless Extension user space interface is used with
modern cfg80211/mac80211 drivers. Do not support Wireless
Extensions for Wi-Fi 7 devices at all. Everyone should switch to
using nl80211 interface instead.
- Improve the CAN bit timing configuration. Use extack to return
error messages directly to user space, update the SJW handling,
including the definition of a new default value that will benefit
CAN-FD controllers, by increasing their oscillator tolerance.
New hardware / drivers:
- Ethernet:
- nVidia BlueField-3 support (control traffic driver)
- Ethernet support for imx93 SoCs
- Motorcomm yt8531 gigabit Ethernet PHY
- onsemi NCN26000 10BASE-T1S PHY (with support for PLCA)
- Microchip LAN8841 PHY (incl. cable diagnostics and PTP)
- Amlogic gxl MDIO mux
- WiFi:
- RealTek RTL8188EU (rtl8xxxu)
- Qualcomm Wi-Fi 7 devices (ath12k)
- CAN:
- Renesas R-Car V4H
Drivers:
- Bluetooth:
- Set Per Platform Antenna Gain (PPAG) for Intel controllers.
- Ethernet NICs:
- Intel (1G, igc):
- support TSN / Qbv / packet scheduling features of i226 model
- Intel (100G, ice):
- use GNSS subsystem instead of TTY
- multi-buffer XDP support
- extend support for GPIO pins to E823 devices
- nVidia/Mellanox:
- update the shared buffer configuration on PFC commands
- implement PTP adjphase function for HW offset control
- TC support for Geneve and GRE with VF tunnel offload
- more efficient crypto key management method
- multi-port eswitch support
- Netronome/Corigine:
- add DCB IEEE support
- support IPsec offloading for NFP3800
- Freescale/NXP (enetc):
- support XDP_REDIRECT for XDP non-linear buffers
- improve reconfig, avoid link flap and waiting for idle
- support MAC Merge layer
- Other NICs:
- sfc/ef100: add basic devlink support for ef100
- ionic: rx_push mode operation (writing descriptors via MMIO)
- bnxt: use the auxiliary bus abstraction for RDMA
- r8169: disable ASPM and reset bus in case of tx timeout
- cpsw: support QSGMII mode for J721e CPSW9G
- cpts: support pulse-per-second output
- ngbe: add an mdio bus driver
- usbnet: optimize usbnet_bh() by avoiding unnecessary queuing
- r8152: handle devices with FW with NCM support
- amd-xgbe: support 10Mbps, 2.5GbE speeds and rx-adaptation
- virtio-net: support multi buffer XDP
- virtio/vsock: replace virtio_vsock_pkt with sk_buff
- tsnep: XDP support
- Ethernet high-speed switches:
- nVidia/Mellanox (mlxsw):
- add support for latency TLV (in FW control messages)
- Microchip (sparx5):
- separate explicit and implicit traffic forwarding rules, make
the implicit rules always active
- add support for egress DSCP rewrite
- IS0 VCAP support (Ingress Classification)
- IS2 VCAP filters (protos, L3 addrs, L4 ports, flags, ToS
etc.)
- ES2 VCAP support (Egress Access Control)
- support for Per-Stream Filtering and Policing (802.1Q,
8.6.5.1)
- Ethernet embedded switches:
- Marvell (mv88e6xxx):
- add MAB (port auth) offload support
- enable PTP receive for mv88e6390
- NXP (ocelot):
- support MAC Merge layer
- support for the the vsc7512 internal copper phys
- Microchip:
- lan9303: convert to PHYLINK
- lan966x: support TC flower filter statistics
- lan937x: PTP support for KSZ9563/KSZ8563 and LAN937x
- lan937x: support Credit Based Shaper configuration
- ksz9477: support Energy Efficient Ethernet
- other:
- qca8k: convert to regmap read/write API, use bulk operations
- rswitch: Improve TX timestamp accuracy
- Intel WiFi (iwlwifi):
- EHT (Wi-Fi 7) rate reporting
- STEP equalizer support: transfer some STEP (connection to radio
on platforms with integrated wifi) related parameters from the
BIOS to the firmware.
- Qualcomm 802.11ax WiFi (ath11k):
- IPQ5018 support
- Fine Timing Measurement (FTM) responder role support
- channel 177 support
- MediaTek WiFi (mt76):
- per-PHY LED support
- mt7996: EHT (Wi-Fi 7) support
- Wireless Ethernet Dispatch (WED) reset support
- switch to using page pool allocator
- RealTek WiFi (rtw89):
- support new version of Bluetooth co-existance
- Mobile:
- rmnet: support TX aggregation"
* tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (1872 commits)
page_pool: add a comment explaining the fragment counter usage
net: ethtool: fix __ethtool_dev_mm_supported() implementation
ethtool: pse-pd: Fix double word in comments
xsk: add linux/vmalloc.h to xsk.c
sefltests: netdevsim: wait for devlink instance after netns removal
selftest: fib_tests: Always cleanup before exit
net/mlx5e: Align IPsec ASO result memory to be as required by hardware
net/mlx5e: TC, Set CT miss to the specific ct action instance
net/mlx5e: Rename CHAIN_TO_REG to MAPPED_OBJ_TO_REG
net/mlx5: Refactor tc miss handling to a single function
net/mlx5: Kconfig: Make tc offload depend on tc skb extension
net/sched: flower: Support hardware miss to tc action
net/sched: flower: Move filter handle initialization earlier
net/sched: cls_api: Support hardware miss to tc action
net/sched: Rename user cookie and act cookie
sfc: fix builds without CONFIG_RTC_LIB
sfc: clean up some inconsistent indentings
net/mlx4_en: Introduce flexible array to silence overflow warning
net: lan966x: Fix possible deadlock inside PTP
net/ulp: Remove redundant ->clone() test in inet_clone_ulp().
...
Diffstat (limited to 'sound/usb/clock.c')
-rw-r--r-- | sound/usb/clock.c | 616 |
1 files changed, 616 insertions, 0 deletions
diff --git a/sound/usb/clock.c b/sound/usb/clock.c new file mode 100644 index 000000000..33db334e6 --- /dev/null +++ b/sound/usb/clock.c @@ -0,0 +1,616 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Clock domain and sample rate management functions + */ + +#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 <linux/usb/audio-v3.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" + +union uac23_clock_source_desc { + struct uac_clock_source_descriptor v2; + struct uac3_clock_source_descriptor v3; +}; + +union uac23_clock_selector_desc { + struct uac_clock_selector_descriptor v2; + struct uac3_clock_selector_descriptor v3; +}; + +union uac23_clock_multiplier_desc { + struct uac_clock_multiplier_descriptor v2; + struct uac_clock_multiplier_descriptor v3; +}; + +#define GET_VAL(p, proto, field) \ + ((proto) == UAC_VERSION_3 ? (p)->v3.field : (p)->v2.field) + +static void *find_uac_clock_desc(struct usb_host_interface *iface, int id, + bool (*validator)(void *, int, int), + u8 type, int proto) +{ + void *cs = NULL; + + while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen, + cs, type))) { + if (validator(cs, id, proto)) + return cs; + } + + return NULL; +} + +static bool validate_clock_source(void *p, int id, int proto) +{ + union uac23_clock_source_desc *cs = p; + + return GET_VAL(cs, proto, bClockID) == id; +} + +static bool validate_clock_selector(void *p, int id, int proto) +{ + union uac23_clock_selector_desc *cs = p; + + return GET_VAL(cs, proto, bClockID) == id; +} + +static bool validate_clock_multiplier(void *p, int id, int proto) +{ + union uac23_clock_multiplier_desc *cs = p; + + return GET_VAL(cs, proto, bClockID) == id; +} + +#define DEFINE_FIND_HELPER(name, obj, validator, type2, type3) \ +static obj *name(struct snd_usb_audio *chip, int id, int proto) \ +{ \ + return find_uac_clock_desc(chip->ctrl_intf, id, validator, \ + proto == UAC_VERSION_3 ? (type3) : (type2), \ + proto); \ +} + +DEFINE_FIND_HELPER(snd_usb_find_clock_source, + union uac23_clock_source_desc, validate_clock_source, + UAC2_CLOCK_SOURCE, UAC3_CLOCK_SOURCE); +DEFINE_FIND_HELPER(snd_usb_find_clock_selector, + union uac23_clock_selector_desc, validate_clock_selector, + UAC2_CLOCK_SELECTOR, UAC3_CLOCK_SELECTOR); +DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier, + union uac23_clock_multiplier_desc, validate_clock_multiplier, + UAC2_CLOCK_MULTIPLIER, UAC3_CLOCK_MULTIPLIER); + +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_quirk(struct snd_usb_audio *chip, + const struct audioformat *fmt, + int source_id) +{ + bool ret = false; + int count; + unsigned char data; + struct usb_device *dev = chip->dev; + union uac23_clock_source_desc *cs_desc; + + cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol); + if (!cs_desc) + return false; + + if (fmt->protocol == UAC_VERSION_2) { + /* + * Assume the clock is valid if clock source supports only one + * single sample rate, the terminal is connected directly to it + * (there is no clock selector) and clock type is internal. + * This is to deal with some Denon DJ controllers that always + * reports that clock is invalid. + */ + if (fmt->nr_rates == 1 && + (fmt->clock & 0xff) == cs_desc->v2.bClockID && + (cs_desc->v2.bmAttributes & 0x3) != + UAC_CLOCK_SOURCE_TYPE_EXT) + return true; + } + + /* + * MOTU MicroBook IIc + * Sample rate changes takes more than 2 seconds for this device. Clock + * validity request returns false during that period. + */ + if (chip->usb_id == USB_ID(0x07fd, 0x0004)) { + count = 0; + + while ((!ret) && (count < 50)) { + int err; + + msleep(100); + + 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 false; + } + + ret = !!data; + count++; + } + } + + return ret; +} + +static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, + const struct audioformat *fmt, + int source_id) +{ + int err; + unsigned char data; + struct usb_device *dev = chip->dev; + u32 bmControls; + union uac23_clock_source_desc *cs_desc; + + cs_desc = snd_usb_find_clock_source(chip, source_id, fmt->protocol); + if (!cs_desc) + return false; + + if (fmt->protocol == UAC_VERSION_3) + bmControls = le32_to_cpu(cs_desc->v3.bmControls); + else + bmControls = cs_desc->v2.bmControls; + + /* If a clock source can't tell us whether it's valid, we assume it is */ + if (!uac_v2v3_control_is_readable(bmControls, + UAC2_CS_CONTROL_CLOCK_VALID)) + return true; + + 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 false; + } + + if (data) + return true; + else + return uac_clock_source_is_valid_quirk(chip, fmt, source_id); +} + +static int __uac_clock_find_source(struct snd_usb_audio *chip, + const struct audioformat *fmt, int entity_id, + unsigned long *visited, bool validate) +{ + union uac23_clock_source_desc *source; + union uac23_clock_selector_desc *selector; + union uac23_clock_multiplier_desc *multiplier; + int ret, i, cur, err, pins, clock_id; + const u8 *sources; + int proto = fmt->protocol; + + 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; + } + + /* first, see if the ID we're looking at is a clock source already */ + source = snd_usb_find_clock_source(chip, entity_id, proto); + if (source) { + entity_id = GET_VAL(source, proto, bClockID); + if (validate && !uac_clock_source_is_valid(chip, fmt, + 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, entity_id, proto); + if (selector) { + pins = GET_VAL(selector, proto, bNrInPins); + clock_id = GET_VAL(selector, proto, bClockID); + sources = GET_VAL(selector, proto, baCSourceID); + cur = 0; + + if (pins == 1) { + ret = 1; + goto find_source; + } + + /* the entity ID we are looking at is a selector. + * find out what it currently selects */ + ret = uac_clock_selector_get_val(chip, clock_id); + if (ret < 0) { + if (!chip->autoclock) + return ret; + goto find_others; + } + + /* Selector values are one-based */ + + if (ret > pins || ret < 1) { + usb_audio_err(chip, + "%s(): selector reported illegal value, id %d, ret %d\n", + __func__, clock_id, ret); + + if (!chip->autoclock) + return -EINVAL; + goto find_others; + } + + find_source: + cur = ret; + ret = __uac_clock_find_source(chip, fmt, + sources[ret - 1], + visited, validate); + if (ret > 0) { + /* Skip setting clock selector again for some devices */ + if (chip->quirk_flags & QUIRK_FLAG_SKIP_CLOCK_SELECTOR) + return ret; + err = uac_clock_selector_set_val(chip, entity_id, cur); + if (err < 0) + return err; + } + + if (!validate || ret > 0 || !chip->autoclock) + return ret; + + find_others: + /* The current clock source is invalid, try others. */ + for (i = 1; i <= pins; i++) { + if (i == cur) + continue; + + ret = __uac_clock_find_source(chip, fmt, + sources[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; + } + + /* FIXME: multipliers only act as pass-thru element for now */ + multiplier = snd_usb_find_clock_multiplier(chip, entity_id, proto); + if (multiplier) + return __uac_clock_find_source(chip, fmt, + GET_VAL(multiplier, proto, bCSourceID), + visited, validate); + + return -EINVAL; +} + +/* + * For all kinds of sample rate settings and other device queries, + * the clock source (end-leaf) must be used. However, clock selectors, + * clock multipliers and sample rate converters may be specified as + * clock source input to terminal. This functions walks the clock path + * to its end and tries to find the source. + * + * The 'visited' bitfield is used internally to detect recursive loops. + * + * Returns the clock source UnitID (>=0) on success, or an error. + */ +int snd_usb_clock_find_source(struct snd_usb_audio *chip, + const struct audioformat *fmt, bool validate) +{ + DECLARE_BITMAP(visited, 256); + memset(visited, 0, sizeof(visited)); + + switch (fmt->protocol) { + case UAC_VERSION_2: + case UAC_VERSION_3: + return __uac_clock_find_source(chip, fmt, fmt->clock, visited, + validate); + default: + return -EINVAL; + } +} + +static int set_sample_rate_v1(struct snd_usb_audio *chip, + const struct audioformat *fmt, int rate) +{ + struct usb_device *dev = chip->dev; + unsigned char data[3]; + int err, crate; + + /* if endpoint doesn't have sampling rate control, bail out */ + if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) + return 0; + + data[0] = rate; + data[1] = rate >> 8; + data[2] = rate >> 16; + 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, + fmt->endpoint, data, sizeof(data)); + if (err < 0) { + dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n", + fmt->iface, fmt->altsetting, rate, fmt->endpoint); + return err; + } + + /* Don't check the sample rate for devices which we know don't + * support reading */ + if (chip->quirk_flags & QUIRK_FLAG_GET_SAMPLE_RATE) + return 0; + /* the firmware is likely buggy, don't repeat to fail too many times */ + if (chip->sample_rate_read_error > 2) + return 0; + + 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, + fmt->endpoint, data, sizeof(data)); + if (err < 0) { + dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n", + fmt->iface, fmt->altsetting, fmt->endpoint); + chip->sample_rate_read_error++; + return 0; /* some devices don't support reading */ + } + + crate = data[0] | (data[1] << 8) | (data[2] << 16); + if (!crate) { + dev_info(&dev->dev, "failed to read current rate; disabling the check\n"); + chip->sample_rate_read_error = 3; /* three strikes, see above */ + return 0; + } + + if (crate != rate) { + dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate); + // runtime->rate = crate; + } + + return 0; +} + +static int get_sample_rate_v2v3(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/v3): err %d\n", + iface, altsetting, err); + return 0; + } + + return le32_to_cpu(data); +} + +/* + * Try to set the given sample rate: + * + * Return 0 if the clock source is read-only, the actual rate on success, + * or a negative error code. + * + * This function gets called from format.c to validate each sample rate, too. + * Hence no message is shown upon error + */ +int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip, + const struct audioformat *fmt, + int clock, int rate) +{ + bool writeable; + u32 bmControls; + __le32 data; + int err; + union uac23_clock_source_desc *cs_desc; + + cs_desc = snd_usb_find_clock_source(chip, clock, fmt->protocol); + + if (!cs_desc) + return 0; + + if (fmt->protocol == UAC_VERSION_3) + bmControls = le32_to_cpu(cs_desc->v3.bmControls); + else + bmControls = cs_desc->v2.bmControls; + + writeable = uac_v2v3_control_is_writeable(bmControls, + UAC2_CS_CONTROL_SAM_FREQ); + if (!writeable) + return 0; + + data = cpu_to_le32(rate); + err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->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) + return err; + + return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock); +} + +static int set_sample_rate_v2v3(struct snd_usb_audio *chip, + const struct audioformat *fmt, int rate) +{ + int cur_rate, prev_rate; + int clock; + + /* First, try to find a valid clock. This may trigger + * automatic clock selection if the current clock is not + * valid. + */ + clock = snd_usb_clock_find_source(chip, fmt, true); + if (clock < 0) { + /* We did not find a valid clock, but that might be + * because the current sample rate does not match an + * external clock source. Try again without validation + * and we will do another validation after setting the + * rate. + */ + clock = snd_usb_clock_find_source(chip, fmt, false); + + /* Hardcoded sample rates */ + if (chip->quirk_flags & QUIRK_FLAG_IGNORE_CLOCK_SOURCE) + return 0; + + if (clock < 0) + return clock; + } + + prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock); + if (prev_rate == rate) + goto validation; + + cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate); + if (cur_rate < 0) { + usb_audio_err(chip, + "%d:%d: cannot set freq %d (v2/v3): err %d\n", + fmt->iface, fmt->altsetting, rate, cur_rate); + return cur_rate; + } + + if (!cur_rate) + cur_rate = prev_rate; + + if (cur_rate != rate) { + usb_audio_dbg(chip, + "%d:%d: freq mismatch: req %d, clock runs @%d\n", + fmt->iface, fmt->altsetting, rate, cur_rate); + /* continue processing */ + } + + /* FIXME - TEAC devices require the immediate interface setup */ + if (USB_ID_VENDOR(chip->usb_id) == 0x0644) { + bool cur_base_48k = (rate % 48000 == 0); + bool prev_base_48k = (prev_rate % 48000 == 0); + if (cur_base_48k != prev_base_48k) { + usb_set_interface(chip->dev, fmt->iface, fmt->altsetting); + if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY) + msleep(50); + } + } + +validation: + /* validate clock after rate change */ + if (!uac_clock_source_is_valid(chip, fmt, clock)) + return -ENXIO; + return 0; +} + +int snd_usb_init_sample_rate(struct snd_usb_audio *chip, + const struct audioformat *fmt, int rate) +{ + usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n", + fmt->iface, fmt->altsetting, rate, fmt->clock); + + switch (fmt->protocol) { + case UAC_VERSION_1: + default: + return set_sample_rate_v1(chip, fmt, rate); + + case UAC_VERSION_3: + if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { + if (rate != UAC3_BADD_SAMPLING_RATE) + return -ENXIO; + else + return 0; + } + fallthrough; + case UAC_VERSION_2: + return set_sample_rate_v2v3(chip, fmt, rate); + } +} + |