USB Audio Gadget part 2: Feedback endpoint, Volume/Mute support
Subsystems (2)
cover USB Audio Gadget part 2: Feedback endpoint, Volume/Mute support
This is extendend version of "UAC2 Feedback endpoint" patch set I've sent back in 2020 [1]. It is extended with bi-directional Volume/Mute controls support for both UAC1 and UAC2 gadgets.
It fixes issues with enumeration in various operation systems because of Feedback endpoint implementation, yet also adds new Volume/Mute support which allows developers to control UAC1/2 Gadget's Volume/Mute from the Host and in the same way UAC1/2 Gadget can control Volume/Mute of the Host making it closer to a real USB Audio card.
This patch set should be applied on top of USB Audio Gadget part 1 fixes/improvements that I've sent previously to the mailing list [2]
============ UAC2 Feedback Endpoint support ============
Current UAC2 gadget implements capture/sync paths as two USB ISO ASYNC endpoints (IN and OUT).
This violates USB spec which says that ISO ASYNC OUT endpoint should have feedback companion endpoint.
See USB2.0 spec "5.12.4.1 Synchronization Type": asynchronous sink provides explicit feedback (isochronous pipe). Interesting that for ISO ASYNC *IN* endpoint respective feedback isn't required since source provides implicit feedforward (data stream).
While it's not an issue if UAC2 Gadget is connected to Linux host (Linux ignores missing feedback endpoint), with other hosts like Windows or MacOS the UAC2 Gadget isn't enumerated due to missing feedback endpoint.
This patch series adds feedback endpoint support to UAC2 function, new control to UAC2 mixer which can be used by userspace tools (like alsaloop from alsa-utils) for updating feedback frequency reported to the host. This is useful for usecases when UAC2 Gadget's audio samples are played to another codec or audio card with its own internal freerunning clock so host can be notified that more/less samples are required.
The alsaloop tool requires some (relatively small) modifications in order to start support driving feedback frequency through UAC2 mixer control. That change have been sent as a separate patch to ALSA community [3].
Also added ability to switch ISO ASYNC OUT endpoint into adaptive endpoint which doesn't require feedback endpoint (as per USB spec).
======== UAC1/2 Volume/Mute controls support ==========
Volume and Mute controls are traditionally presented in USB Audio cards that are available on the market, and Hosts usually expect they exist in the attached devices.
However, Linux UAC gadget previosly didn't have such functinality which (depending on Host's operation system) was causing different behaviour: Linux hosts (usually) in this case can adjust volume by changing audio sample's amplitude that are sent to the gadget. Other hosts may stuck with maximum volume and can't adjust it for the Linux UAC gadget device.
Volume/Mute controls support is quite useful feature. If implemented according to UAC1/2 spec, it allows to control volume in both directions: from the Host to Gadget and from Gadget to the Host. To do it, it's required to add new Feature Unit descriptor to existing UAC1/2 Gadget topology and add new interrupt endpoint so Volume/Mute changes can be reported to the Host.
This has been tested with Linux/MacOS/Windows(7,10) hosts, by attaching alsamixer to the UAC1/2 ALSA card on Gadget side, and using available Volume/Mute control applications on the Hosts.
An user can adjust Volume/Mute on the Host and see respective changes on the Gadget side. In the same way, an user can adjust Volume/Mute on the Gadget side and observe respective changes on the Host
There is a known issue with Win7 which for some reason doesn't poll UAC1 interrupt endpoint causing Volume/Mute control not working in Gadget->Host direction
========================================================
Both features have been tested on BeagleBone Black and Raspberry PI 4 boards with Linux/MacOS/Windows(7,10) hosts.
While on BeagleBone Black it works fine, Raspberry PI 4 DWC2 controller is affected by the DMA issue for control trasfers which I reported a while back in [4]. It is causing incorrect data provided by DWC2 UDC to UAC1/2 gadgets screwing up volume and mute control messages in the Host->Gadget direction. The hack is available (see [4]) but it leads to traces/issues with audio streaming.
Patches reviews and testing on your HW is welcome as usual!
[1] https://lore.kernel.org/linux-usb/[email protected]/
[2] https://lore.kernel.org/linux-usb/[email protected]/
[3] https://lore.kernel.org/linux-usb/[email protected]/
[4] https://lore.kernel.org/linux-usb/CAB=otbTVxa=nGWF4K1AYcYyPceYYRkC_1HYSb_Nhu6C9RMZEHA@mail.gmail.com/
Ruslan Bilovol (8):
usb: gadget: u_audio: convert to strscpy
usb: gadget: f_uac2/u_audio: add feedback endpoint support
usb: gadget: f_uac2: add adaptive sync support for capture
usb: gadget: u_audio: add real feedback implementation
usb: audio-v2: add ability to define feature unit descriptor
usb: gadget: u_audio: add bi-directional volume and mute support
usb: gadget: f_uac2: add volume and mute support
usb: gadget: f_uac1: add volume and mute support
Documentation/ABI/testing/configfs-usb-gadget-uac1 | 10 + Documentation/ABI/testing/configfs-usb-gadget-uac2 | 11 + Documentation/usb/gadget-testing.rst | 37 +- drivers/usb/gadget/function/f_uac1.c | 674 +++++++++++++++++- drivers/usb/gadget/function/f_uac2.c | 764 +++++++++++++++++++-- drivers/usb/gadget/function/u_audio.c | 582 +++++++++++++++- drivers/usb/gadget/function/u_audio.h | 32 + drivers/usb/gadget/function/u_uac1.h | 20 + drivers/usb/gadget/function/u_uac2.h | 23 + include/linux/usb/audio-v2.h | 14 + 10 files changed, 2069 insertions(+), 98 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/8 usb: gadget: u_audio: convert to strscpy
Usage of strlcpy in Linux Kernel has been recently deprecated [1], convert driver to strscpy
[1] https://lore.kernel.org/lkml/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw@mail.gmail.com/
--- drivers/usb/gadget/function/u_audio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/u_audio.c +3 -3
@@ -549,15 +549,15 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,if (err <0 ) goto snd_fail; -strlcpy (pcm->name, pcm_name,sizeof (pcm->name)); +strscpy (pcm->name, pcm_name,sizeof (pcm->name)); pcm->private_data = uac; uac->pcm = pcm;snd_pcm_set_ops (pcm,SNDRV_PCM_STREAM_PLAYBACK , &uac_pcm_ops);snd_pcm_set_ops (pcm,SNDRV_PCM_STREAM_CAPTURE , &uac_pcm_ops); -strlcpy (card->driver, card_name,sizeof (card->driver)); -strlcpy (card->shortname, card_name,sizeof (card->shortname)); +strscpy (card->driver, card_name,sizeof (card->driver)); +strscpy (card->shortname, card_name,sizeof (card->shortname));sprintf (card->longname,"%s %i" , card_name, card->dev->id); snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Signature · 1 line
1.9.1
PATCH 2/8 usb: gadget: f_uac2/u_audio: add feedback endpoint support
In reply to Ruslan BilovolAs per USB and UAC2 specs, asynchronous audio sink endpoint requires explicit synchronization mechanism (Isochronous Feedback Endpoint)
Implement feedback companion endpoint for ISO OUT endpoint
This patch adds all required infrastructure and USB requests handling for feedback endpoint. Syncrhonization itself is still dummy (feedback ep always reports 'nomimal frequency'
e.g. no adjustement is needed). This satisfies hosts that
require feedback endpoint (like Win10) and poll it periodically
Actual synchronization mechanism should be implemented separately
--- drivers/usb/gadget/function/f_uac2.c | 34 +++++++- drivers/usb/gadget/function/u_audio.c | 141 +++++++++++++++++++++++++++++++++- drivers/usb/gadget/function/u_audio.h | 3 + 3 files changed, 175 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/f_uac2.c +33 -1
@@ -240,7 +240,7 @@ enum { .bDescriptorType = USB_DT_INTERFACE, .bAlternateSetting = 1, - .bNumEndpoints = 1, + .bNumEndpoints = 2, .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, .bInterfaceProtocol = UAC_VERSION_2,
@@ -299,6 +299,27 @@ enum { .wLockDelay = 0, }; +/* STD AS ISO IN Feedback Endpoint */ +static struct usb_endpoint_descriptor fs_epin_fback_desc = { + .bLength =USB_DT_ENDPOINT_SIZE , + .bDescriptorType =USB_DT_ENDPOINT , + + .bEndpointAddress =USB_DIR_IN , + .bmAttributes =USB_ENDPOINT_XFER_ISOC |USB_ENDPOINT_USAGE_FEEDBACK , + .wMaxPacketSize =cpu_to_le16 (3 ), + .bInterval =1 , +}; + +static struct usb_endpoint_descriptor hs_epin_fback_desc = { + .bLength =USB_DT_ENDPOINT_SIZE , + .bDescriptorType =USB_DT_ENDPOINT , + + .bmAttributes =USB_ENDPOINT_XFER_ISOC |USB_ENDPOINT_USAGE_FEEDBACK , + .wMaxPacketSize =cpu_to_le16 (4 ), + .bInterval =4 , +}; + +/* Audio Streaming IN Interface - Alt0 */ static struct usb_interface_descriptor std_as_in_if0_desc = { .bLength =sizeof std_as_in_if0_desc,
@@ -395,6 +416,7 @@ enum { (struct usb_descriptor_header *)&as_out_fmt1_desc, (struct usb_descriptor_header *)&fs_epout_desc, (struct usb_descriptor_header *)&as_iso_out_desc, + (struct usb_descriptor_header *)&fs_epin_fback_desc, (struct usb_descriptor_header *)&std_as_in_if0_desc, (struct usb_descriptor_header *)&std_as_in_if1_desc,
@@ -425,6 +447,7 @@ enum { (struct usb_descriptor_header *)&as_out_fmt1_desc, (struct usb_descriptor_header *)&hs_epout_desc, (struct usb_descriptor_header *)&as_iso_out_desc, + (struct usb_descriptor_header *)&hs_epin_fback_desc, (struct usb_descriptor_header *)&std_as_in_if0_desc, (struct usb_descriptor_header *)&std_as_in_if1_desc,
@@ -562,6 +585,7 @@ static void setup_descriptor(struct f_uac2_opts *opts) fs_audio_desc[i++] =USBDHDR (&as_out_fmt1_desc); fs_audio_desc[i++] =USBDHDR (&fs_epout_desc); fs_audio_desc[i++] =USBDHDR (&as_iso_out_desc); + fs_audio_desc[i++] =USBDHDR (&fs_epin_fback_desc); }if (EPIN_EN (opts)) { fs_audio_desc[i++] =USBDHDR (&std_as_in_if0_desc);
@@ -595,6 +619,7 @@ static void setup_descriptor(struct f_uac2_opts *opts) hs_audio_desc[i++] =USBDHDR (&as_out_fmt1_desc); hs_audio_desc[i++] =USBDHDR (&hs_epout_desc); hs_audio_desc[i++] =USBDHDR (&as_iso_out_desc); + hs_audio_desc[i++] =USBDHDR (&hs_epin_fback_desc); }if (EPIN_EN (opts)) { hs_audio_desc[i++] =USBDHDR (&std_as_in_if0_desc);
@@ -757,6 +782,12 @@ static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)dev_err (dev,"%s:%d Error!\n" , __func__, __LINE__);return -ENODEV ; } + agdev->in_ep_fback =usb_ep_autoconfig (gadget, + &fs_epin_fback_desc); +if (!agdev->in_ep_fback) { +dev_err (dev,"%s:%d Error!\n" , __func__, __LINE__); +return -ENODEV ; + } }if (EPIN_EN (uac2_opts)) {
@@ -775,6 +806,7 @@ static int afunc_validate_opts(struct g_audio *agdev, struct device *dev) le16_to_cpu(hs_epout_desc.wMaxPacketSize)); hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; + hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress; hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;setup_descriptor (uac2_opts);
drivers/usb/gadget/function/u_audio.c +139 -2
195 lines not on this page · raw message
drivers/usb/gadget/function/u_audio.h +3 -0
@@ -30,7 +30,10 @@ struct g_audio {struct usb_gadget *gadget;struct usb_ep *in_ep; +struct usb_ep *out_ep; +/* feedback IN endpoint corresponding to out_ep */ +struct usb_ep *in_ep_fback;/* Max packet size for all in_ep possible speeds */ unsigned int in_ep_maxpsize;
Signature · 1 line
1.9.1
Re: [PATCH 2/8] usb: gadget: f_uac2/u_audio: add feedback endpoint support
Dne 01. 03. 21 v 14:05 Ruslan Bilovol napsal(a):
Quoted 16 lines
(quoted diff) +16
-0+static void u_audio_set_fback_frequency(enum usb_device_speed speed, + unsigned int freq, void *buf) +{ + u32 ff = 0; + + if (speed == USB_SPEED_FULL) { + /* + * Full-speed feedback endpoints report frequency + * in samples/microframe + * Format is encoded in Q10.10 left-justified in the 24 bits, + * so that it has a Q10.14 format. + */ + ff = DIV_ROUND_UP((freq << 14), 1000); + } else { + /* + * High-speed feedback endpoints report frequency
Review
Quoted 10 lines
(quoted diff) +10
-0+ * in samples/microframe. + * Format is encoded in Q12.13 fitted into four bytes so that + * the binary point is located between the second and the third + * byte format (that is Q16.16) + * + * Prevent integer overflow by calculating in Q12.13 format and + * then shifting to Q16.16 + */ + ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3; + }
Hi Ruslan,
Thanks a lot for your patch. The HS calculation of Q16.16 feedback value overflows at some 524kHz, disallowing use of larger samplerates (e.g. 768kHz or higher).
I tested the formula used in alsa USB driver
https://github.com/torvalds/linux/blob/d99676af540c2dc829999928fb81c58c80a1dce4/sound/usb/endpoint.c#L80
which uses only 10bit shift. The feedback control in UAC2 gadget now works up to 4M samplerate with 1Hz precision (tested on RPi4 with bInterval = 1, checked in stream0 proc file on linux host).
drivers/usb/gadget/function/u_audio.c +0 -0
@@ -118,7 +119,8 @@ static void u_audio_set_fback_frequency(enum
usb_device_speed speed,
* Prevent integer overflow by calculating in Q12.13
format and
* then shifting to Q16.16
*/
- ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3;
+ //ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3;
+ ff = ((freq << 10) + 62) / 125;
}
*(__le32 *)buf = cpu_to_le32(ff);}
Best regards,
Pavel.
PATCH 3/8 usb: gadget: f_uac2: add adaptive sync support for capture
In reply to Ruslan BilovolCurrent f_uac2 USB OUT (aka 'capture') synchronization implements 'ASYNC' scenario which means USB Gadget has it's own freerunning clock and can update Host about real clock frequency through feedback endpoint so Host can align number of samples sent to the USB gadget to prevent overruns/underruns
In case if Gadget can has no it's internal clock and can consume audio samples at any rate (for example, on the Gadget side someone records audio directly to a file, or audio samples are played through an external DAC as soon as they arrive), UAC2 spec suggests 'ADAPTIVE' synchronization type.
Change UAC2 driver to make it configurable through additional 'c_sync' configfs file.
Default remains 'asynchronous' with possibility to switch it to 'adaptive'
--- Documentation/ABI/testing/configfs-usb-gadget-uac2 | 1 + Documentation/usb/gadget-testing.rst | 1 + drivers/usb/gadget/function/f_uac2.c | 102 ++++++++++++++++++--- drivers/usb/gadget/function/u_uac2.h | 2 + 4 files changed, 95 insertions(+), 11 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +1 -0
@@ -8,6 +8,7 @@ Description: c_chmask capture channel mask c_srate capture sampling rate c_ssize capture sample size (bytes) + c_sync capture synchronization type (async/adaptive) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes)
Documentation/usb/gadget-testing.rst +1 -0
@@ -728,6 +728,7 @@ The uac2 function provides these attributes in its function directory: c_chmask capture channel mask c_srate capture sampling rate c_ssize capture sample size (bytes) + c_sync capture synchronization type (async/adaptive) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes)
drivers/usb/gadget/function/f_uac2.c +91 -11
192 lines not on this page · raw message
drivers/usb/gadget/function/u_uac2.h +2 -0
@@ -21,6 +21,7 @@#define UAC2_DEF_CCHMASK 0x3#define UAC2_DEF_CSRATE 64000#define UAC2_DEF_CSSIZE 2 +#define UAC2_DEF_CSYNC USB_ENDPOINT_SYNC_ASYNC#define UAC2_DEF_REQ_NUM 2struct f_uac2_opts {
@@ -31,6 +32,7 @@ struct f_uac2_opts {int c_chmask;int c_srate;int c_ssize; +int c_sync;int req_number;bool bound;
Signature · 1 line
1.9.1
PATCH 4/8 usb: gadget: u_audio: add real feedback implementation
In reply to Ruslan BilovolThis adds interface between userspace and feedback endpoint to report real feedback frequency to the Host.
Current implementation adds new userspace interface ALSA mixer control "PCM Feedback Frequency Hz" (similar to aloop driver's "PCM Rate Shift 100000" mixer control)
We allow +/-20% deviation of nominal sampling frequency, that usually is more than enough in real-world usecases
Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils.
--- drivers/usb/gadget/function/f_uac2.c | 4 ++ drivers/usb/gadget/function/u_audio.c | 93 +++++++++++++++++++++++++++++++++++ drivers/usb/gadget/function/u_audio.h | 7 +++ 3 files changed, 104 insertions(+)
drivers/usb/gadget/function/f_uac2.c +4 -0
@@ -506,6 +506,10 @@ static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, max_size_bw =num_channels (chmask) * ssize * ((srate / (factor / (1 << (ep_desc->bInterval -1 )))) +1 ); + +if (!is_playback && (uac2_opts->c_sync ==USB_ENDPOINT_SYNC_ASYNC )) + max_size_bw = max_size_bw *FBACK_FREQ_MAX /100 ; + ep_desc->wMaxPacketSize =cpu_to_le16 (min_t (u16, max_size_bw, max_size_ep));
drivers/usb/gadget/function/u_audio.c +93 -0
117 lines not on this page · raw message
drivers/usb/gadget/function/u_audio.h +7 -0
@@ -11,6 +11,13 @@#include <linux/usb/composite.h> +/* +* Min/max percentage of nominal sampling frequency deviation +* reported through feedback endpoint to the host +*/ +#define FBACK_FREQ_MIN 80 +#define FBACK_FREQ_MAX 120 +struct uac_params {/* playback */ int p_chmask;/* channel mask */
Signature · 1 line
1.9.1
PATCH 5/8 usb: audio-v2: add ability to define feature unit descriptor
In reply to Ruslan BilovolSimilar to UAC1 spec, UAC2 feature unit descriptor has variable size.
Current audio-v2 feature unit descriptor structure is used for parsing descriptors, but can't be used to define your own descriptor.
Add a new macro similar to what audio v1 already has.
--- include/linux/usb/audio-v2.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/usb/audio-v2.h +14 -0
@@ -156,6 +156,20 @@ struct uac2_feature_unit_descriptor {__u8 bmaControls[];/* variable length */ } __attribute__((packed)); +#define UAC2_DT_FEATURE_UNIT_SIZE (ch) (6 + ((ch) + 1) * 4) + +/* As above, but more useful for defining your own descriptors: */ +#define DECLARE_UAC2_FEATURE_UNIT_DESCRIPTOR (ch) \ +struct uac2_feature_unit_descriptor_##ch { \ + __u8 bLength; \ + __u8 bDescriptorType; \ + __u8 bDescriptorSubtype; \ + __u8 bUnitID; \ + __u8 bSourceID; \ + __le32 bmaControls[ch + 1]; \ + __u8 iFeature; \ +} __packed +/* 4.7.2.10 Effect Unit Descriptor */ struct uac2_effect_unit_descriptor {
Signature · 1 line
1.9.1
PATCH 6/8 usb: gadget: u_audio: add bi-directional volume and mute support
In reply to Ruslan BilovolUSB Audio Class 1/2 have ability to change device's volume and mute by USB Host through class-specific control requests. Device also can notify Host about volume/mute change on its side through optional interrupt endpoint.
This patch adds Volume and Mute ALSA controls which can be used by user to send and receive notifications to/from the USB Host about Volume and Mute change.
These params come from f_uac* so volume and mute controls will be created only if the function support and enable each explicitly
--- drivers/usb/gadget/function/u_audio.c | 360 +++++++++++++++++++++++++++++++++- drivers/usb/gadget/function/u_audio.h | 22 +++ 2 files changed, 372 insertions(+), 10 deletions(-)
drivers/usb/gadget/function/u_audio.c +350 -10
@@ -12,11 +12,14 @@* Jaswinder Singh ([email protected]) */ +#include <linux/kernel.h> #include <linux/module.h> #include <sound/core.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include <sound/control.h> +#include <sound/tlv.h> +#include <linux/usb/audio.h> #include "u_audio.h"
@@ -24,6 +27,12 @@#define PRD_SIZE_MAX PAGE_SIZE#define MIN_PERIODS 4 +enum { +UAC_FBACK_CTRL , +UAC_MUTE_CTRL , +UAC_VOLUME_CTRL , +}; +/* Runtime data params for one stream */ struct uac_rtd_params {struct snd_uac_chip *uac;/* parent chip */
@@ -44,6 +53,14 @@ struct uac_rtd_params {unsigned int ffback;/* Real frequency reported by feedback endpoint */ bool fb_ep_enabled;/* if the ep is enabled */ +/* Volume/Mute controls and their state */ +int fu_id;/* Feature Unit ID */ +struct snd_kcontrol *snd_kctl_volume; +struct snd_kcontrol *snd_kctl_mute; +s16 volume_min, volume_max, volume_res; +s16 volume; +int mute; +spinlock_t lock;/* lock for feedback and control transfers */ };
@@ -599,6 +616,102 @@ void u_audio_stop_playback(struct g_audio *audio_dev) }EXPORT_SYMBOL_GPL (u_audio_stop_playback); +int u_audio_get_volume (struct g_audio *audio_dev,int playback,s16 *val) +{ +struct snd_uac_chip *uac = audio_dev->uac; +struct uac_rtd_params *prm; +unsigned long flags; + +if (playback) + prm = &uac->p_prm; +else + prm = &uac->c_prm; + +spin_lock_irqsave (&prm->lock, flags); + *val = prm->volume; +spin_unlock_irqrestore (&prm->lock, flags); + +return 0 ; +} +EXPORT_SYMBOL_GPL (u_audio_get_volume); + +int u_audio_set_volume (struct g_audio *audio_dev,int playback,s16 val) +{ +struct snd_uac_chip *uac = audio_dev->uac; +struct uac_rtd_params *prm; +unsigned long flags; +int change =0 ; + +if (playback) + prm = &uac->p_prm; +else + prm = &uac->c_prm; + +spin_lock_irqsave (&prm->lock, flags); + val =clamp (val, prm->volume_min, prm->volume_max); +if (prm->volume != val) { + prm->volume = val; + change =1 ; + } +spin_unlock_irqrestore (&prm->lock, flags); + +if (change) +snd_ctl_notify (uac->card,SNDRV_CTL_EVENT_MASK_VALUE , + &prm->snd_kctl_volume->id); + +return 0 ; +} +EXPORT_SYMBOL_GPL (u_audio_set_volume); + +int u_audio_get_mute (struct g_audio *audio_dev,int playback,int *val) +{ +struct snd_uac_chip *uac = audio_dev->uac; +struct uac_rtd_params *prm; +unsigned long flags; + +if (playback) + prm = &uac->p_prm; +else + prm = &uac->c_prm; + +spin_lock_irqsave (&prm->lock, flags); + *val = prm->mute; +spin_unlock_irqrestore (&prm->lock, flags); + +return 0 ; +} +EXPORT_SYMBOL_GPL (u_audio_get_mute); + +int u_audio_set_mute (struct g_audio *audio_dev,int playback,int val) +{ +struct snd_uac_chip *uac = audio_dev->uac; +struct uac_rtd_params *prm; +unsigned long flags; +int change =0 ; +int mute; + +if (playback) + prm = &uac->p_prm; +else + prm = &uac->c_prm; + + mute = val ?1 :0 ; + +spin_lock_irqsave (&prm->lock, flags); +if (prm->mute != mute) { + prm->mute = mute; + change =1 ; + } +spin_unlock_irqrestore (&prm->lock, flags); + +if (change) +snd_ctl_notify (uac->card,SNDRV_CTL_EVENT_MASK_VALUE , + &prm->snd_kctl_mute->id); + +return 0 ; +} +EXPORT_SYMBOL_GPL (u_audio_set_mute); +static int u_audio_rate_shift_info (struct snd_kcontrol *kcontrol,struct snd_ctl_elem_info *uinfo) {
@@ -663,14 +776,157 @@ static int u_audio_rate_shift_put(struct snd_kcontrol *kcontrol,return change; } -static const struct snd_kcontrol_new u_audio_controls[] = { +static int u_audio_mute_info (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_info *uinfo) +{ + uinfo->type =SNDRV_CTL_ELEM_TYPE_BOOLEAN ; + uinfo->count =1 ; + uinfo->value.integer.min =0 ; + uinfo->value.integer.max =1 ; + uinfo->value.integer.step =1 ; + +return 0 ; +} + +static int u_audio_mute_get (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) { - .iface =SNDRV_CTL_ELEM_IFACE_PCM , - .name ="PCM Feedback Frequency Hz" , - .info = u_audio_rate_shift_info, - .get = u_audio_rate_shift_get, - .put = u_audio_rate_shift_put, -}, +struct uac_rtd_params *prm =snd_kcontrol_chip (kcontrol); +unsigned long flags; + +spin_lock_irqsave (&prm->lock, flags); + ucontrol->value.integer.value[0 ] = !prm->mute; +spin_unlock_irqrestore (&prm->lock, flags); + +return 0 ; +} + +static int u_audio_mute_put (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) +{ +struct uac_rtd_params *prm =snd_kcontrol_chip (kcontrol); +struct snd_uac_chip *uac = prm->uac; +struct g_audio *audio_dev = uac->audio_dev; +unsigned int val; +unsigned long flags; +int change =0 ; + + val = !ucontrol->value.integer.value[0 ]; + +spin_lock_irqsave (&prm->lock, flags); +if (val != prm->mute) { + prm->mute = val; + change =1 ; + } +spin_unlock_irqrestore (&prm->lock, flags); + +if (change && audio_dev->notify) + audio_dev->notify (audio_dev, prm->fu_id,UAC_FU_MUTE ); + +return change; +} + +/* +* TLV callback for mixer volume controls +*/ +static int u_audio_volume_tlv (struct snd_kcontrol *kcontrol,int op_flag, +unsigned int size,unsigned int __user *_tlv) +{ +struct uac_rtd_params *prm =snd_kcontrol_chip (kcontrol); +DECLARE_TLV_DB_MINMAX (scale,0 ,0 ); + +if (size <sizeof (scale)) +return -ENOMEM ; + +/* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */ + scale[2 ] = (prm->volume_min *100 ) /256 ; + scale[3 ] = (prm->volume_max *100 ) /256 ; +if (copy_to_user (_tlv, scale,sizeof (scale))) +return -EFAULT ; + +return 0 ; +} + +static int u_audio_volume_info (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_info *uinfo) +{ +struct uac_rtd_params *prm =snd_kcontrol_chip (kcontrol); + + uinfo->type =SNDRV_CTL_ELEM_TYPE_INTEGER ; + uinfo->count =1 ; + uinfo->value.integer.min =0 ; + uinfo->value.integer.max = + (prm->volume_max - prm->volume_min + prm->volume_res -1 ) + / prm->volume_res; + uinfo->value.integer.step =1 ; + +return 0 ; +} + +static int u_audio_volume_get (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) +{ +struct uac_rtd_params *prm =snd_kcontrol_chip (kcontrol); +unsigned long flags; + +spin_lock_irqsave (&prm->lock, flags); + ucontrol->value.integer.value[0 ] = + (prm->volume - prm->volume_min) / prm->volume_res; +spin_unlock_irqrestore (&prm->lock, flags); + +return 0 ; +} + +static int u_audio_volume_put (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) +{ +struct uac_rtd_params *prm =snd_kcontrol_chip (kcontrol); +struct snd_uac_chip *uac = prm->uac; +struct g_audio *audio_dev = uac->audio_dev; +unsigned int val; +s16 volume; +unsigned long flags; +int change =0 ; + + val = ucontrol->value.integer.value[0 ]; + +spin_lock_irqsave (&prm->lock, flags); + volume = (val * prm->volume_res) + prm->volume_min; + volume =clamp (volume, prm->volume_min, prm->volume_max); +if (volume != prm->volume) { + prm->volume = volume; + change =1 ; + } +spin_unlock_irqrestore (&prm->lock, flags); + +if (change && audio_dev->notify) + audio_dev->notify (audio_dev, prm->fu_id,UAC_FU_VOLUME ); + +return change; +} + +static struct snd_kcontrol_new u_audio_controls[] = { + [UAC_FBACK_CTRL] { + .iface =SNDRV_CTL_ELEM_IFACE_PCM , + .name ="PCM Feedback Frequency Hz" , + .info = u_audio_rate_shift_info, + .get = u_audio_rate_shift_get, + .put = u_audio_rate_shift_put, + }, + [UAC_MUTE_CTRL] { + .iface =SNDRV_CTL_ELEM_IFACE_MIXER , + .name ="" ,/* will be filled later */ + .info = u_audio_mute_info, + .get = u_audio_mute_get, + .put = u_audio_mute_put, + }, + [UAC_VOLUME_CTRL] { + .iface =SNDRV_CTL_ELEM_IFACE_MIXER , + .name ="" ,/* will be filled later */ + .info = u_audio_volume_info, + .get = u_audio_volume_get, + .put = u_audio_volume_put, + }, }; int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
@@ -682,7 +938,7 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,struct snd_kcontrol *kctl;struct uac_params *params;int p_chmask, c_chmask; -int err; +int i, err;if (!g_audio)return -EINVAL ;
@@ -769,10 +1025,18 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,snd_pcm_set_ops (pcm,SNDRV_PCM_STREAM_PLAYBACK , &uac_pcm_ops);snd_pcm_set_ops (pcm,SNDRV_PCM_STREAM_CAPTURE , &uac_pcm_ops); -if (c_chmask && g_audio->in_ep_fback) { +/* +* Create mixer and controls +* Create only if it's required on USB side +*/ +if ((c_chmask && g_audio->in_ep_fback) + || (p_chmask && params->p_fu.id) + || (c_chmask && params->c_fu.id))strscpy (card->mixername, card_name,sizeof (card->mixername)); - kctl =snd_ctl_new1 (&u_audio_controls[0 ], &uac->c_prm); +if (c_chmask && g_audio->in_ep_fback) { + kctl =snd_ctl_new1 (&u_audio_controls[UAC_FBACK_CTRL ], + &uac->c_prm);if (!kctl) { err = -ENOMEM ; goto snd_fail;
@@ -786,6 +1050,82 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, goto snd_fail; } +for (i =0 ; i <=SNDRV_PCM_STREAM_LAST ; i++) { +struct uac_rtd_params *prm; +struct uac_fu_params *fu; +char ctrl_name[24 ]; +char *direction; + +if (!pcm->streams[i].substream_count) +continue ; + +if (i ==SNDRV_PCM_STREAM_PLAYBACK ) { + prm = &uac->p_prm; + fu = ¶ms->p_fu; + direction ="Playback" ; + }else { + prm = &uac->c_prm; + fu = ¶ms->c_fu; + direction ="Capture" ; + } + + prm->fu_id = fu->id; + +if (fu->mute_present) { +snprintf (ctrl_name,sizeof (ctrl_name), +"PCM %s Switch" , direction); + + u_audio_controls[UAC_MUTE_CTRL ].name = ctrl_name; + + kctl =snd_ctl_new1 (&u_audio_controls[UAC_MUTE_CTRL ], + prm); +if (!kctl) { + err = -ENOMEM ; + goto snd_fail; + } + + kctl->id.device = pcm->device; + kctl->id.subdevice = i; + + err =snd_ctl_add (card, kctl); +if (err <0 ) + goto snd_fail; + prm->snd_kctl_mute = kctl; + prm->mute =0 ; + } + +if (fu->volume_present) { +snprintf (ctrl_name,sizeof (ctrl_name), +"PCM %s Volume" , direction); + + u_audio_controls[UAC_VOLUME_CTRL ].name = ctrl_name; + + kctl =snd_ctl_new1 (&u_audio_controls[UAC_VOLUME_CTRL ], + prm); +if (!kctl) { + err = -ENOMEM ; + goto snd_fail; + } + + kctl->id.device = pcm->device; + kctl->id.subdevice = i; + + + kctl->tlv.c = u_audio_volume_tlv; + kctl->vd[0 ].access |=SNDRV_CTL_ELEM_ACCESS_TLV_READ | +SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK ; + + err =snd_ctl_add (card, kctl); +if (err <0 ) + goto snd_fail; + prm->snd_kctl_volume = kctl; + prm->volume = fu->volume_max; + prm->volume_max = fu->volume_max; + prm->volume_min = fu->volume_min; + prm->volume_res = fu->volume_res; + } + } +strscpy (card->driver, card_name,sizeof (card->driver));strscpy (card->shortname, card_name,sizeof (card->shortname));sprintf (card->longname,"%s %i" , card_name, card->dev->id);
drivers/usb/gadget/function/u_audio.h +22 -0
@@ -18,16 +18,30 @@#define FBACK_FREQ_MIN 80#define FBACK_FREQ_MAX 120 +/* Feature Unit parameters */ +struct uac_fu_params { +int id;/* Feature Unit ID */ + +bool mute_present;/* mute control enable */ + +bool volume_present;/* volume control enable */ +s16 volume_min;/* min volume in 1/256 dB */ +s16 volume_max;/* max volume in 1/256 dB */ +s16 volume_res;/* volume resolution in 1/256 dB */ +}; +struct uac_params {/* playback */ int p_chmask;/* channel mask */ int p_srate;/* rate in Hz */ int p_ssize;/* sample size */ +struct uac_fu_params p_fu;/* Feature Unit parameters */ /* capture */ int c_chmask;/* channel mask */ int c_srate;/* rate in Hz */ int c_ssize;/* sample size */ +struct uac_fu_params c_fu;/* Feature Unit parameters */ int req_number;/* number of preallocated requests */ };
@@ -47,6 +61,9 @@ struct g_audio {/* Max packet size for all out_ep possible speeds */ unsigned int out_ep_maxpsize; +/* Notify UAC driver about control change */ +int (*notify)(struct g_audio *g_audio,int unit_id,int cs); +/* The ALSA Sound Card it represents on the USB-Client side */ struct snd_uac_chip *uac;
@@ -92,4 +109,9 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,int u_audio_start_playback (struct g_audio *g_audio);void u_audio_stop_playback (struct g_audio *g_audio); +int u_audio_get_volume (struct g_audio *g_audio,int playback,s16 *val); +int u_audio_set_volume (struct g_audio *g_audio,int playback,s16 val); +int u_audio_get_mute (struct g_audio *g_audio,int playback,int *val); +int u_audio_set_mute (struct g_audio *g_audio,int playback,int val); +#endif /* __U_AUDIO_H */
Signature · 1 line
1.9.1
PATCH 7/8 usb: gadget: f_uac2: add volume and mute support
In reply to Ruslan BilovolThis adds bi-directional (host->device, device->host) volume/mute support to the f_uac2 driver by adding Feature Units and interrupt endpoint.
Currently only master channel is supported.
Volume and mute are configurable through configfs, by default volume has -100..0 dB range with 1 dB step.
Similar to existing flexible endpoints configuration, Feature Unit won't be added to the topology if both mute and volume are not enabled, also interrupt endpoint isn't added to the device if no feature unit is present
--- Documentation/ABI/testing/configfs-usb-gadget-uac2 | 10 + Documentation/usb/gadget-testing.rst | 10 + drivers/usb/gadget/function/f_uac2.c | 640 +++++++++++++++++++-- drivers/usb/gadget/function/u_uac2.h | 21 + 4 files changed, 623 insertions(+), 58 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +10 -0
@@ -9,7 +9,17 @@ Description: c_srate capture sampling rate c_ssize capture sample size (bytes) c_sync capture synchronization type (async/adaptive) + c_mute_present capture mute control enable + c_volume_present capture volume control enable + c_volume_min capture volume control min value (in 1/256 dB) + c_volume_max capture volume control max value (in 1/256 dB) + c_volume_res capture volume control resolution (in 1/256 dB) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) + p_mute_present playback mute control enable + p_volume_present playback volume control enable + p_volume_min playback volume control min value (in 1/256 dB) + p_volume_max playback volume control max value (in 1/256 dB) + p_volume_res playback volume control resolution (in 1/256 dB) ========= ============================
Documentation/usb/gadget-testing.rst +10 -0
@@ -729,9 +729,19 @@ The uac2 function provides these attributes in its function directory: c_srate capture sampling rate c_ssize capture sample size (bytes) c_sync capture synchronization type (async/adaptive) + c_mute_present capture mute control enable + c_volume_present capture volume control enable + c_volume_min capture volume control min value (in 1/256 dB) + c_volume_max capture volume control max value (in 1/256 dB) + c_volume_res capture volume control resolution (in 1/256 dB) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) + p_mute_present playback mute control enable + p_volume_present playback volume control enable + p_volume_min playback volume control min value (in 1/256 dB) + p_volume_max playback volume control max value (in 1/256 dB) + p_volume_res playback volume control resolution (in 1/256 dB) req_number the number of pre-allocated request for both capture and playback =============== ====================================================
drivers/usb/gadget/function/f_uac2.c +582 -58
1,006 lines not on this page · raw message
drivers/usb/gadget/function/u_uac2.h +21 -0
@@ -22,7 +22,15 @@#define UAC2_DEF_CSRATE 64000#define UAC2_DEF_CSSIZE 2#define UAC2_DEF_CSYNC USB_ENDPOINT_SYNC_ASYNC + +#define UAC2_DEF_MUTE_PRESENT 1 +#define UAC2_DEF_VOLUME_PRESENT 1 +#define UAC2_DEF_MIN_DB (-100*256)/* -100 dB */ +#define UAC2_DEF_MAX_DB 0/* 0 dB */ +#define UAC2_DEF_RES_DB (1*256)/* 1 dB */ +#define UAC2_DEF_REQ_NUM 2 +#define UAC2_DEF_INT_REQ_NUM 10struct f_uac2_opts {struct usb_function_instance func_inst;
@@ -33,6 +41,19 @@ struct f_uac2_opts {int c_srate;int c_ssize;int c_sync; + +bool p_mute_present; +bool p_volume_present; +s16 p_volume_min; +s16 p_volume_max; +s16 p_volume_res; + +bool c_mute_present; +bool c_volume_present; +s16 c_volume_min; +s16 c_volume_max; +s16 c_volume_res; +int req_number;bool bound;
Signature · 1 line
1.9.1
PATCH 8/8 usb: gadget: f_uac1: add volume and mute support
In reply to Ruslan BilovolThis adds bi-directional (host->device, device->host) volume/mute support to the f_uac1 driver by adding Feature Units and interrupt endpoint.
Currently only master channel is supported.
Volume and mute are configurable through configfs, by default volume has -100..0 dB range with 1 dB step.
Similar to existing flexible endpoints configuration, Feature Unit won't be added to the topology if both mute and volume are not enabled, also interrupt endpoint isn't added to the device if no feature unit is present
--- Documentation/ABI/testing/configfs-usb-gadget-uac1 | 10 + Documentation/usb/gadget-testing.rst | 26 +- drivers/usb/gadget/function/f_uac1.c | 674 ++++++++++++++++++++- drivers/usb/gadget/function/u_uac1.h | 20 + 4 files changed, 700 insertions(+), 30 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac1 +10 -0
@@ -8,9 +8,19 @@ Description: c_chmask capture channel mask c_srate capture sampling rate c_ssize capture sample size (bytes) + c_mute_present capture mute control enable + c_volume_present capture volume control enable + c_volume_min capture volume control min value (in 1/256 dB) + c_volume_max capture volume control max value (in 1/256 dB) + c_volume_res capture volume control resolution (in 1/256 dB) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) + p_mute_present playback mute control enable + p_volume_present playback volume control enable + p_volume_min playback volume control min value (in 1/256 dB) + p_volume_max playback volume control max value (in 1/256 dB) + p_volume_res playback volume control resolution (in 1/256 dB) req_number the number of pre-allocated request for both capture and playback ========== ===================================
Documentation/usb/gadget-testing.rst +18 -8
@@ -914,14 +914,24 @@ The function name to use when creating the function directory is "uac1". The uac1 function provides these attributes in its function directory: ========== ==================================================== - c_chmask capture channel mask - c_srate capture sampling rate - c_ssize capture sample size (bytes) - p_chmask playback channel mask - p_srate playback sampling rate - p_ssize playback sample size (bytes) - req_number the number of pre-allocated request for both capture - and playback + c_chmask capture channel mask + c_srate capture sampling rate + c_ssize capture sample size (bytes) + c_mute_present capture mute control enable + c_volume_present capture volume control enable + c_volume_min capture volume control min value (in 1/256 dB) + c_volume_max capture volume control max value (in 1/256 dB) + c_volume_res capture volume control resolution (in 1/256 dB) + p_chmask playback channel mask + p_srate playback sampling rate + p_ssize playback sample size (bytes) + p_mute_present playback mute control enable + p_volume_present playback volume control enable + p_volume_min playback volume control min value (in 1/256 dB) + p_volume_max playback volume control max value (in 1/256 dB) + p_volume_res playback volume control resolution (in 1/256 dB) + req_number the number of pre-allocated request for both capture + and playback ========== ==================================================== The attributes have sane default values.
drivers/usb/gadget/function/f_uac1.c +652 -22
912 lines not on this page · raw message
drivers/usb/gadget/function/u_uac1.h +20 -0
@@ -18,6 +18,13 @@#define UAC1_DEF_PSRATE 48000#define UAC1_DEF_PSSIZE 2#define UAC1_DEF_REQ_NUM 2 +#define UAC1_DEF_INT_REQ_NUM 10 + +#define UAC1_DEF_MUTE_PRESENT 1 +#define UAC1_DEF_VOLUME_PRESENT 1 +#define UAC1_DEF_MIN_DB (-100*256)/* -100 dB */ +#define UAC1_DEF_MAX_DB 0/* 0 dB */ +#define UAC1_DEF_RES_DB (1*256)/* 1 dB */ struct f_uac1_opts {
@@ -28,6 +35,19 @@ struct f_uac1_opts {int p_chmask;int p_srate;int p_ssize; + +bool p_mute_present; +bool p_volume_present; +s16 p_volume_min; +s16 p_volume_max; +s16 p_volume_res; + +bool c_mute_present; +bool c_volume_present; +s16 c_volume_min; +s16 c_volume_max; +s16 c_volume_res; +int req_number; unsigned bound:1;
Signature · 1 line
1.9.1
Greg Kroah-Hartman
maintainerQuoted 12 lines · Ruslan Bilovol
This is extendend version of "UAC2 Feedback endpoint" patch set I've sent back in 2020 [1]. It is extended with bi-directional Volume/Mute controls support for both UAC1 and UAC2 gadgets.
It fixes issues with enumeration in various operation systems because of Feedback endpoint implementation, yet also adds new Volume/Mute support which allows developers to control UAC1/2 Gadget's Volume/Mute from the Host and in the same way UAC1/2 Gadget can control Volume/Mute of the Host making it closer to a real USB Audio card.
Quoted 3 lines
This patch set should be applied on top of USB Audio Gadget part 1 fixes/improvements that I've sent previously to the mailing list [2]
This series did not apply to my tree (except for the first patch). Can you rebase it on my usb-testing branch and resend?
thanks,
greg k-h