Skip to contentSkip to conversation

UAC2 Gadget: feedback endpoint support

RFC PATCH Superseded Jerome Brunet 2 participants 15 messages Apr 30, 2021 – May 27, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#7 lore raw

RFC PATCH v2 2/3 usb: gadget: f_uac2: add adaptive sync support for capture

In reply to Jerome Brunet

From: Ruslan Bilovol <[email protected]>

Current 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'

---
 .../ABI/testing/configfs-usb-gadget-uac2      |   1 +
 Documentation/usb/gadget-testing.rst          |   1 +
 drivers/usb/gadget/function/f_uac2.c          | 100 ++++++++++++++++--
 drivers/usb/gadget/function/u_uac2.h          |   2 +
 4 files changed, 95 insertions(+), 9 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +1 -0
index d4356c8b8cd6..e7e59d7fb12f 100644
@@ -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
index 2085e7b24eeb..f5a12667bd41 100644
@@ -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 -9

172 lines not on this page · raw message

drivers/usb/gadget/function/u_uac2.h +2 -0
index b5035711172d..13589c3c805c 100644
@@ -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 2
 
 struct 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
2.31.1
#1 lore raw

RFC PATCH v2 1/3 usb: gadget: f_uac2/u_audio: add feedback endpoint support

In reply to Jerome Brunet

From: Ruslan Bilovol <[email protected]>

As 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  |  49 ++++++++++-
 drivers/usb/gadget/function/u_audio.c | 119 +++++++++++++++++++++++++-
 drivers/usb/gadget/function/u_audio.h |   3 +
 3 files changed, 168 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/f_uac2.c +48 -1
index 7aa4c8bc5a1a..5d63244ba319 100644
@@ -240,7 +240,7 @@ static struct usb_interface_descriptor std_as_out_if1_desc = {
 	.bDescriptorType = USB_DT_INTERFACE,
 
 	.bAlternateSetting = 1,
-	.bNumEndpoints = 1,
+	.bNumEndpoints = 2,
 	.bInterfaceClass = USB_CLASS_AUDIO,
 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
 	.bInterfaceProtocol = UAC_VERSION_2,
@@ -317,6 +317,37 @@ static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
 	.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,
+};
+
+static struct usb_endpoint_descriptor ss_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(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,
@@ -431,6 +462,7 @@ static struct usb_descriptor_header *fs_audio_desc[] = {
 	(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,
@@ -461,6 +493,7 @@ static struct usb_descriptor_header *hs_audio_desc[] = {
 	(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,
@@ -492,6 +525,7 @@ static struct usb_descriptor_header *ss_audio_desc[] = {
 	(struct usb_descriptor_header *)&ss_epout_desc,
 	(struct usb_descriptor_header *)&ss_epout_desc_comp,
 	(struct usb_descriptor_header *)&as_iso_out_desc,
+	(struct usb_descriptor_header *)&ss_epin_fback_desc,
 
 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
@@ -568,22 +602,26 @@ static void setup_headers(struct f_uac2_opts *opts,
 	struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
 	struct usb_endpoint_descriptor *epout_desc;
 	struct usb_endpoint_descriptor *epin_desc;
+	struct usb_endpoint_descriptor *epin_fback_desc;
 	int i;
 
 	switch (speed) {
 	case USB_SPEED_FULL:
 		epout_desc = &fs_epout_desc;
 		epin_desc = &fs_epin_desc;
+		epin_fback_desc = &fs_epin_fback_desc;
 		break;
 	case USB_SPEED_HIGH:
 		epout_desc = &hs_epout_desc;
 		epin_desc = &hs_epin_desc;
+		epin_fback_desc = &hs_epin_fback_desc;
 		break;
 	default:
 		epout_desc = &ss_epout_desc;
 		epin_desc = &ss_epin_desc;
 		epout_desc_comp = &ss_epout_desc_comp;
 		epin_desc_comp = &ss_epin_desc_comp;
+		epin_fback_desc = &ss_epin_fback_desc;
 	}
 
 	i = 0;
@@ -611,6 +649,7 @@ static void setup_headers(struct f_uac2_opts *opts,
 			headers[i++] = USBDHDR(epout_desc_comp);
 
 		headers[i++] = USBDHDR(&as_iso_out_desc);
+		headers[i++] = USBDHDR(epin_fback_desc);
 	}
 	if (EPIN_EN(opts)) {
 		headers[i++] = USBDHDR(&std_as_in_if0_desc);
@@ -844,6 +883,12 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 			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)) {
@@ -867,8 +912,10 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 				le16_to_cpu(ss_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;
 	ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
+	ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
 	ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
 
 	setup_descriptor(uac2_opts);
drivers/usb/gadget/function/u_audio.c +117 -2

161 lines not on this page · raw message

drivers/usb/gadget/function/u_audio.h +3 -0
index 5ea6b86f1fda..53e6baf55cbf 100644
@@ -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
2.31.1
#0 lore raw

v2 cover UAC2 Gadget: 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 will be sent as a separate patch to ALSA community.

Also added ability to switch ISO ASYNC OUT endpoint into adaptive endpoint which doesn't require feedback endpoint (as per USB spec).

v1 [0] -> v2:
* Instead of HW rate used by gadget (which the controlling application might not be directly dealing with), the kcontrol used to provide the feedback value is expressed in PPM. This way the correction is agnostic of the rate used by the gadget while retaining enough precision to allow a smooth adaptation of the clock on the playback side, if necessary.
* The minimum value for the correction is aligned with sound/usb/endpoint.c at -25%.
* The maximum correction must be chosen more carefully as it impact the bandwidth required by the output EP. The minimum is one extra frame per packet. This is ~500PPM at 192KHz. This seemed more than enough while testing.
Of course, there is a trade off between saving bandwidth and enabling devices with a significant (abnormal) clock deviation. The default extra reserved bandwidth is set at 500PPM but it is configurable through configfs, like the rest of the gadget.

I have kept Ruslan assigned as the author of series. He did the vast majority of the work and deserve the credit. I have merely tweaked in his original patchset.

This patchset has been tested on an arm64 host at HIGH_SPEED, with various rates and channel configuration. I have not tested with FULL_SPEED or SUPER_SPEED.

See here [1] an example of regulation with a patched version of alsaloop.

[0]: https://lore.kernel.org/r/[email protected]
[1]: https://pastebin.com/0hiK7LAe

Ruslan Bilovol (3):
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

 .../ABI/testing/configfs-usb-gadget-uac2      |   2 +
 Documentation/usb/gadget-testing.rst          |   2 +
 drivers/usb/gadget/function/f_uac2.c          | 144 +++++++++++-
 drivers/usb/gadget/function/u_audio.c         | 214 +++++++++++++++++-
 drivers/usb/gadget/function/u_audio.h         |  12 +
 drivers/usb/gadget/function/u_uac2.h          |   4 +
 6 files changed, 372 insertions(+), 6 deletions(-)
Signature · 1 line
2.31.1
#8 lore raw

RFC PATCH v2 3/3 usb: gadget: u_audio: add real feedback implementation

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000" mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW rate, which the application is not necessarily dealing with, while still retaining a good enough precision to allow smooth clock correction on the playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved bandwidth is rounded up to the next packet size.

Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils.

---
 .../ABI/testing/configfs-usb-gadget-uac2      |   1 +
 Documentation/usb/gadget-testing.rst          |   1 +
 drivers/usb/gadget/function/f_uac2.c          |   9 +-
 drivers/usb/gadget/function/u_audio.c         | 111 ++++++++++++++++--
 drivers/usb/gadget/function/u_audio.h         |   9 ++
 drivers/usb/gadget/function/u_uac2.h          |   2 +
 6 files changed, 124 insertions(+), 9 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +1 -0
index e7e59d7fb12f..26fb8e9b4e61 100644
@@ -9,6 +9,7 @@ Description:
 		c_srate    capture sampling rate
 		c_ssize    capture sample size (bytes)
 		c_sync     capture synchronization type (async/adaptive)
+		fb_max     maximum extra bandwidth in async mode
 		p_chmask   playback channel mask
 		p_srate    playback sampling rate
 		p_ssize    playback sample size (bytes)
Documentation/usb/gadget-testing.rst +1 -0
index f5a12667bd41..9d6276f82774 100644
@@ -729,6 +729,7 @@ 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)
+	fb_max          maximum extra bandwidth in async mode
 	p_chmask	playback channel mask
 	p_srate		playback sampling rate
 	p_ssize		playback sample size (bytes)
drivers/usb/gadget/function/f_uac2.c +8 -1
index 321e6c05ba93..ae29ff2b2b68 100644
@@ -584,8 +584,11 @@ static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
 		ssize = uac2_opts->c_ssize;
 	}
 
+	if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC))
+		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
+
 	max_size_bw = num_channels(chmask) * ssize *
-		((srate / (factor / (1 << (ep_desc->bInterval - 1)))) + 1);
+		DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
 	ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
 						    max_size_ep));
 
@@ -957,6 +960,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	agdev->params.c_srate = uac2_opts->c_srate;
 	agdev->params.c_ssize = uac2_opts->c_ssize;
 	agdev->params.req_number = uac2_opts->req_number;
+	agdev->params.fb_max = uac2_opts->fb_max;
 	ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
 	if (ret)
 		goto err_free_descs;
@@ -1329,6 +1333,7 @@ UAC2_ATTRIBUTE(c_srate);
 UAC2_ATTRIBUTE_SYNC(c_sync);
 UAC2_ATTRIBUTE(c_ssize);
 UAC2_ATTRIBUTE(req_number);
+UAC2_ATTRIBUTE(fb_max);
 
 static struct configfs_attribute *f_uac2_attrs[] = {
 	&f_uac2_opts_attr_p_chmask,
@@ -1339,6 +1344,7 @@ static struct configfs_attribute *f_uac2_attrs[] = {
 	&f_uac2_opts_attr_c_ssize,
 	&f_uac2_opts_attr_c_sync,
 	&f_uac2_opts_attr_req_number,
+	&f_uac2_opts_attr_fb_max,
 	NULL,
 };
 
@@ -1378,6 +1384,7 @@ static struct usb_function_instance *afunc_alloc_inst(void)
 	opts->c_ssize = UAC2_DEF_CSSIZE;
 	opts->c_sync = UAC2_DEF_CSYNC;
 	opts->req_number = UAC2_DEF_REQ_NUM;
+	opts->fb_max = UAC2_DEF_FB_MAX;
 	return &opts->func_inst;
 }
 
drivers/usb/gadget/function/u_audio.c +103 -8

185 lines not on this page · raw message

drivers/usb/gadget/function/u_audio.h +9 -0
index 53e6baf55cbf..a218cdf771fe 100644
@@ -11,6 +11,14 @@
 
 #include <linux/usb/composite.h>
 
+/*
+ * Same maximum frequency deviation on the slower side as in
+ * sound/usb/endpoint.c. Value is expressed in per-mil deviation.
+ * The maximum deviation on the faster side will be provided as
+ * parameter, as it impacts the endpoint required bandwidth.
+ */
+#define FBACK_SLOW_MAX	250
+
 struct uac_params {
 	/* playback */
 	int p_chmask;	/* channel mask */
@@ -23,6 +31,7 @@ struct uac_params {
 	int c_ssize;	/* sample size */
 
 	int req_number; /* number of preallocated requests */
+	int fb_max;	/* upper frequency drift feedback limit per-mil */
 };
 
 struct g_audio {
drivers/usb/gadget/function/u_uac2.h +2 -0
index 13589c3c805c..179d3ef6a195 100644
@@ -23,6 +23,7 @@
 #define UAC2_DEF_CSSIZE 2
 #define UAC2_DEF_CSYNC		USB_ENDPOINT_SYNC_ASYNC
 #define UAC2_DEF_REQ_NUM 2
+#define UAC2_DEF_FB_MAX 5
 
 struct f_uac2_opts {
 	struct usb_function_instance	func_inst;
@@ -34,6 +35,7 @@ struct f_uac2_opts {
 	int				c_ssize;
 	int				c_sync;
 	int				req_number;
+	int				fb_max;
 	bool				bound;
 
 	struct mutex			lock;
Signature · 1 line
2.31.1
#2 lore raw

Re: [RFC PATCH v2 1/3] usb: gadget: f_uac2/u_audio: add feedback endpoint support

In reply to Jerome Brunet

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 14 lines

From: Ruslan Bilovol <[email protected]>

As 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

Quoted 5 lines

Actual synchronization mechanism should be implemented separately

Hi,

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 = ((freq << 10) + 62) / 125;
        }
        *(__le32 *)buf = cpu_to_le32(ff);

}

Best regards,

Pavel.

#3 lore raw

Re: [RFC PATCH v2 1/3] usb: gadget: f_uac2/u_audio: add feedback endpoint support

Quoted 41 lines · Pavel Hofman

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 14 lines

From: Ruslan Bilovol <[email protected]>

As 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

Quoted 5 lines

Actual synchronization mechanism should be implemented separately

Hi,

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
*/

drivers/usb/gadget/function/u_audio.c

Quoted 2 lines
drivers/usb/gadget/function/u_audio.c +1 -1
-               ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3;
+               ff = ((freq << 10) + 62) / 125;

Pavel, The code posted is a little different from snip here. While I understand the "<< 10" and "/ 125", the "+ 62" would welcome a comment.

Also in the final patch, the calculation is a bit different and moved to "long long" ... but I'm sure the same type of improvement could be done.

Quoted 8 lines
        }
        *(__le32 *)buf = cpu_to_le32(ff);

}

Best regards,

Pavel.

#4 lore raw

Re: [RFC PATCH v2 1/3] usb: gadget: f_uac2/u_audio: add feedback endpoint support

Quoted 35 lines · Jerome Brunet
Quoted 33 lines · Pavel Hofman

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 14 lines

From: Ruslan Bilovol <[email protected]>

As 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

Quoted 5 lines

Actual synchronization mechanism should be implemented separately

Hi,

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).

Quoted 14 lines

drivers/usb/gadget/function/u_audio.c

Quoted 10 lines
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
*/

drivers/usb/gadget/function/u_audio.c +1 -1
-               ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3;
+               ff = ((freq << 10) + 62) / 125;

Pavel, The code posted is a little different from snip here. While I understand the "<< 10" and "/ 125", the "+ 62" would welcome a comment.

OOhhh I got it now ... I think using ROUND_UP() is lot more readable (and maintainable)

Quoted 12 lines

Also in the final patch, the calculation is a bit different and moved to "long long" ... but I'm sure the same type of improvement could be done.

Quoted 8 lines
        }
        *(__le32 *)buf = cpu_to_le32(ff);

}

Best regards,

Pavel.

#5 lore raw

Re: [RFC PATCH v2 1/3] usb: gadget: f_uac2/u_audio: add feedback endpoint support

Dne 30. 04. 21 v 19:11 Jerome Brunet napsal(a):

Quoted 53 lines · Jerome Brunet
Quoted 36 lines · Pavel Hofman
Quoted 34 lines

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 14 lines

From: Ruslan Bilovol <[email protected]>

As 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

Quoted 5 lines

Actual synchronization mechanism should be implemented separately

Hi,

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).

Quoted 14 lines

drivers/usb/gadget/function/u_audio.c

Quoted 10 lines
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
*/

drivers/usb/gadget/function/u_audio.c +1 -1
-               ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3;
+               ff = ((freq << 10) + 62) / 125;

Pavel, The code posted is a little different from snip here. While I understand the "<< 10" and "/ 125", the "+ 62" would welcome a comment.

Quoted 10 lines

OOhhh I got it now ... I think using ROUND_UP() is lot more readable (and maintainable)

Quoted 7 lines

Also in the final patch, the calculation is a bit different and moved to "long long" ... but I'm sure the same type of improvement could be done.

Quoted 3 lines
         }
         *(__le32 *)buf = cpu_to_le32(ff);

}

I copied the code from the very same calculation in the current-version alsa usb host driver, as linked above. Hence I assumed it should have no problems in the gadget code either. All I can confirm is that it works in the gadget properly and does not overflow with ff up to 4M. The gadget can easily run at megahertz samplerates, there are standard USB-audio devices with 768kHz samplerates.

Thanks a lot. Best regards,

Pavel.

#6 lore raw

Re: [RFC PATCH v2 1/3] usb: gadget: f_uac2/u_audio: add feedback endpoint support

In reply to Jerome Brunet

Dne 30. 04. 21 v 19:11 Jerome Brunet napsal(a):

Quoted 53 lines · Jerome Brunet
Quoted 36 lines · Pavel Hofman
Quoted 34 lines

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 14 lines

From: Ruslan Bilovol <[email protected]>

As 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

Quoted 5 lines

Actual synchronization mechanism should be implemented separately

Hi,

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).

Quoted 14 lines

drivers/usb/gadget/function/u_audio.c

Quoted 10 lines
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
*/

drivers/usb/gadget/function/u_audio.c +1 -1
-               ff = DIV_ROUND_UP((freq << 13), (8*1000)) << 3;
+               ff = ((freq << 10) + 62) / 125;

Pavel, The code posted is a little different from snip here. While I understand the "<< 10" and "/ 125", the "+ 62" would welcome a comment.

Quoted 6 lines

OOhhh I got it now ... I think using ROUND_UP() is lot more readable (and maintainable)

Quoted 3 lines

Also in the final patch, the calculation is a bit different and moved to "long long" ... but I'm sure the same type of improvement could be done.

Jerome, I see (sorry for the misunderstanding) that you have modified original Ruslan's patch and the freq variable being shifted by 13 is long long. Your code is definitely better, thanks.

Pavel.

#9 lore raw

Re: [RFC PATCH v2 3/3] usb: gadget: u_audio: .... PCM Rate Shift for playback too?

In reply to Jerome Brunet

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 21 lines

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000" mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW rate, which the application is not necessarily dealing with, while still retaining a good enough precision to allow smooth clock correction on the playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved bandwidth is rounded up to the next packet size.

Quoted 5 lines

Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils.

Hi, the existing patches solve the Host -> Gadget -> capturing application direction, controlling the host playback rate. The other direction (playback app -> gadget -> capturing host) is still paced by the host USB controller. Packet size is pre-calculated in u_audio_start_playback as rate/p_interval
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L441
and this fixed value is used for copying the audio data in u_audio_iso_complete
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L124
.

That means if the gadget has a physical duplex audio device with single clock and runs a duplex operation, the path gadget-> host will not run synchronously with the physical audio device (the host -> gadget has already the feedback control implemented).

How about "duplicating" the existing ALSA mixer control "Capture Pitch 1000000" to "Playback Pitch 1000000" and using pitch-adjusted p_srate in the above-linked calculations? That should make the playback side run at the playback pitch requested by gadget userspace, IIUC.

For the duplex operation with single clock, the capture pitch value determined by the userspace chain (alsaloop, CamillaDSP, etc.) would be used for setting both the capture and playback pitch controls, making both directions run synchronously.

I can prepare patches based on Jerome's patchset should you find this solution acceptable.

Thanks a lot,

Pavel.

#10 lore raw

Re: [RFC PATCH v2 3/3] usb: gadget: u_audio: .... PCM Rate Shift for playback too?

Quoted 51 lines · Pavel Hofman

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 21 lines

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000" mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW rate, which the application is not necessarily dealing with, while still retaining a good enough precision to allow smooth clock correction on the playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved bandwidth is rounded up to the next packet size.

Quoted 5 lines

Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils.

Hi, the existing patches solve the Host -> Gadget -> capturing application direction, controlling the host playback rate. The other direction (playback app -> gadget -> capturing host) is still paced by the host USB controller. Packet size is pre-calculated in u_audio_start_playback as rate/p_interval
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L441
and this fixed value is used for copying the audio data in u_audio_iso_complete
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L124
.

That means if the gadget has a physical duplex audio device with single clock and runs a duplex operation, the path gadget-> host will not run synchronously with the physical audio device (the host -> gadget has already the feedback control implemented).

How about "duplicating" the existing ALSA mixer control "Capture Pitch 1000000" to "Playback Pitch 1000000" and using pitch-adjusted p_srate in the above-linked calculations? That should make the playback side run at the playback pitch requested by gadget userspace, IIUC.

Quoted 7 lines

For the duplex operation with single clock, the capture pitch value determined by the userspace chain (alsaloop, CamillaDSP, etc.) would be used for setting both the capture and playback pitch controls, making both directions run synchronously.

I can prepare patches based on Jerome's patchset should you find this solution acceptable.

Well I have experimented with pitch on the playback path.
It does work but I'm not sure if it actually makes sense which is why I have not not included it in RFC

If you need the playback and capture to run synchronously, you'd be better off with implicit feedback (In which the host will provide the same number of samples it received from the device)

With explicit feedback, we are (supposed) to tell the host to speed up or slow down to match the device clock. This means the device run asynchronously, and the host does the adaptation (if necessary). In such case, I'm not sure adding pitch control on the playback path is a good idea.

Having pitch control on the playback side allows to forward the audio captured by the physical interface of the device (like I2S) to USB without performing any resampling between the two (when USB and I2S are not in sync). It's nice and convenient ... but I wonder if this is a feature or a hack ;)

Quoted 4 lines

Thanks a lot,

Pavel.

#11 lore raw

Re: [RFC PATCH v2 3/3] usb: gadget: u_audio: .... PCM Rate Shift for playback too?

Dne 24. 05. 21 v 17:40 Jerome Brunet napsal(a):

Quoted 76 lines · Pavel Hofman
Quoted 52 lines

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 21 lines

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000" mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW rate, which the application is not necessarily dealing with, while still retaining a good enough precision to allow smooth clock correction on the playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved bandwidth is rounded up to the next packet size.

Quoted 5 lines

Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils.

Hi, the existing patches solve the Host -> Gadget -> capturing application direction, controlling the host playback rate. The other direction (playback app -> gadget -> capturing host) is still paced by the host USB controller. Packet size is pre-calculated in u_audio_start_playback as rate/p_interval
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L441
and this fixed value is used for copying the audio data in u_audio_iso_complete
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L124
.

That means if the gadget has a physical duplex audio device with single clock and runs a duplex operation, the path gadget-> host will not run synchronously with the physical audio device (the host -> gadget has already the feedback control implemented).

How about "duplicating" the existing ALSA mixer control "Capture Pitch 1000000" to "Playback Pitch 1000000" and using pitch-adjusted p_srate in the above-linked calculations? That should make the playback side run at the playback pitch requested by gadget userspace, IIUC.

Quoted 7 lines

For the duplex operation with single clock, the capture pitch value determined by the userspace chain (alsaloop, CamillaDSP, etc.) would be used for setting both the capture and playback pitch controls, making both directions run synchronously.

I can prepare patches based on Jerome's patchset should you find this solution acceptable.

Well I have experimented with pitch on the playback path.
It does work but I'm not sure if it actually makes sense which is why I have not not included it in RFC

If you need the playback and capture to run synchronously, you'd be better off with implicit feedback (In which the host will provide the same number of samples it received from the device)

With explicit feedback, we are (supposed) to tell the host to speed up or slow down to match the device clock. This means the device run asynchronously, and the host does the adaptation (if necessary). In such case, I'm not sure adding pitch control on the playback path is a good idea.

Quoted 6 lines

Having pitch control on the playback side allows to forward the audio captured by the physical interface of the device (like I2S) to USB without performing any resampling between the two (when USB and I2S are not in sync). It's nice and convenient ... but I wonder if this is a feature or a hack ;)

Jerome, thanks a lot for your reply. The current implementation of the EP IN audio direction is timed by the USB host controller. Let's consider 48Khz samplerate and bInterval=1 fullspeed (8k packets per second). Every IN packet will always carry 6 audio frames. No matter how fast the real master clock (i.e. e.g. I2S of the gadget) runs. Until an xrun occurs, unfortunately. Even if the gadget configuration used implicit feedback, the incoming stream would still provide no synchronization information from the I2S hardware clock as the data stream is clocked by the USB host which controls the timing on the USB bus (and thus the moment of iso completion). Plus the stock usb-audio driver in Windows 10 does not support implicit feedback, according to https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/usb-2-0-audio-drivers#audio-streaming .

The problem is the fixed "slicing" of the samples into the packets which cannot be controlled. The same situation was on the host side before the feedback EP was introduced. Here the one preparing the slices (the "transmitter") is the gadget now. And the slicing pace must be controllable just like the slicing pace on the host is via the feedback EP.

Because the gadget supports different playback and capture rates (which I find nice), I suggest a separate control element (Playback Pitch, Capture Pitch).

Of course the motivation is to avoid adaptive resampling in the gadget in the direction I2S -> gadget interface -> USB host. But the very same motivation lead to implementation of the async feedback EP in the opposite direction. IMO it is not a hack, but an indispensable feature making the gadget usable for duplex operation with hardware (i.e. the real master) clock at the backend (all the other "clocks" are just software-generated slices/blocks of audio frames).

With regards,

Pavel.

#12 lore raw

Re: [RFC PATCH v2 3/3] usb: gadget: u_audio: .... PCM Rate Shift for playback too?

Dne 24. 05. 21 v 18:09 Pavel Hofman napsal(a):

Quoted 92 lines

Dne 24. 05. 21 v 17:40 Jerome Brunet napsal(a):

Quoted 78 lines · Pavel Hofman
Quoted 54 lines

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 21 lines

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000" mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW rate, which the application is not necessarily dealing with, while still retaining a good enough precision to allow smooth clock correction on the playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved bandwidth is rounded up to the next packet size.

Quoted 5 lines

Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils.

Hi, the existing patches solve the Host -> Gadget -> capturing application direction, controlling the host playback rate. The other direction (playback app -> gadget -> capturing host) is still paced by the host USB controller. Packet size is pre-calculated in u_audio_start_playback  as rate/p_interval
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L441

and this fixed value is used for copying the audio data in u_audio_iso_complete
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L124

.

That means if the gadget has a physical duplex audio device with single clock and runs a duplex operation, the path gadget-> host  will not run synchronously with the physical audio device (the host -> gadget has already the feedback control implemented).

How about "duplicating" the existing ALSA mixer control "Capture Pitch 1000000" to "Playback Pitch 1000000" and using pitch-adjusted p_srate in the above-linked calculations? That should make the playback side run at the playback pitch requested by gadget userspace, IIUC.

Quoted 7 lines

For the duplex operation with single clock, the capture pitch value determined by the userspace chain (alsaloop, CamillaDSP, etc.) would be used for setting both the capture and playback pitch controls, making both directions run synchronously.

I can prepare patches based on Jerome's patchset should you find this solution acceptable.

Well I have experimented with pitch on the playback path.
It does work but I'm not sure if it actually makes sense which is why I have not not included it in RFC

If you need the playback and capture to run synchronously, you'd be better off with implicit feedback (In which the host will provide the same number of samples it received from the device)

With explicit feedback, we are (supposed) to tell the host to speed up or slow down to match the device clock. This means the device run asynchronously, and the host does the adaptation (if necessary). In such case, I'm not sure adding pitch control on the playback path is a good idea.

Quoted 6 lines

Having pitch control on the playback side allows to forward the audio captured by the physical interface of the device (like I2S) to USB without performing any resampling between the two (when USB and I2S are not in sync). It's nice and convenient ... but I wonder if this is a feature or a hack ;)

Jerome, thanks a lot for your reply. The current implementation of the EP IN audio direction is timed by the USB host controller. Let's consider 48Khz samplerate and bInterval=1 fullspeed (8k packets per second). Every IN packet will always carry 6 audio frames. No matter how fast the real master clock (i.e. e.g. I2S of the gadget) runs. Until an xrun occurs, unfortunately. Even if the gadget configuration used implicit feedback, the incoming stream would still provide no synchronization information from the I2S hardware clock as the data stream is clocked by the USB host which controls the timing on the USB bus (and thus the moment of iso completion). Plus the stock usb-audio driver in Windows 10 does not support implicit feedback, according to https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/usb-2-0-audio-drivers#audio-streaming .

The problem is the fixed "slicing" of the samples into the packets which cannot be controlled. The same situation was on the host side before the feedback EP was introduced. Here the one preparing the slices (the "transmitter") is the gadget now. And the slicing pace must be controllable just like the slicing pace on the host is via the feedback EP.

Quoted 7 lines

Because the gadget supports different playback and capture rates (which I find nice), I suggest a separate control element (Playback Pitch, Capture Pitch).

Of course the motivation is to avoid adaptive resampling in the gadget in the direction I2S -> gadget interface -> USB host. But the very same motivation lead to implementation of the async feedback EP in the opposite direction. IMO it is not a hack, but an indispensable feature making the gadget usable for duplex operation with hardware (i.e. the real master) clock at the backend (all the other "clocks" are just software-generated slices/blocks of audio frames).

With regards,

Pavel.

Jerome, please do you still have your playback-side patches available or should I prepare them? Thanks a lot,

Pavel.

#13 lore raw

Re: [RFC PATCH v2 3/3] usb: gadget: u_audio: .... PCM Rate Shift for playback too?

Quoted 125 lines · Pavel Hofman

Dne 24. 05. 21 v 18:09 Pavel Hofman napsal(a):

Quoted 115 lines

Dne 24. 05. 21 v 17:40 Jerome Brunet napsal(a):

Quoted 85 lines
Quoted 59 lines · Pavel Hofman

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 28 lines

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real
feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000"
mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW
rate, which the application is not necessarily dealing with, while still
retaining a good enough precision to allow smooth clock correction on the
playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact
on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved
bandwidth is rounded up to the next packet size.

Quoted 6 lines

Usage of this new control is easy to implement in existing userspace tools
like alsaloop from alsa-utils.

Hi, the existing patches solve the Host -> Gadget -> capturing application direction, controlling the host playback rate. The other direction (playback app -> gadget -> capturing host) is still paced by the host USB controller. Packet size is pre-calculated in u_audio_start_playback as rate/p_interval
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L441
and this fixed value is used for copying the audio data in u_audio_iso_complete
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L124
.

That means if the gadget has a physical duplex audio device with single clock and runs a duplex operation, the path gadget-> host will not run synchronously with the physical audio device (the host -> gadget has already the feedback control implemented).

How about "duplicating" the existing ALSA mixer control "Capture Pitch 1000000" to "Playback Pitch 1000000" and using pitch-adjusted p_srate in the above-linked calculations? That should make the playback side run at the playback pitch requested by gadget userspace, IIUC.

Quoted 7 lines

For the duplex operation with single clock, the capture pitch value determined by the userspace chain (alsaloop, CamillaDSP, etc.) would be used for setting both the capture and playback pitch controls, making both directions run synchronously.

I can prepare patches based on Jerome's patchset should you find this solution acceptable.

Well I have experimented with pitch on the playback path.
It does work but I'm not sure if it actually makes sense which is why I have not not included it in RFC

If you need the playback and capture to run synchronously, you'd be better off with implicit feedback (In which the host will provide the same number of samples it received from the device)

With explicit feedback, we are (supposed) to tell the host to speed up or slow down to match the device clock. This means the device run asynchronously, and the host does the adaptation (if necessary). In such case, I'm not sure adding pitch control on the playback path is a good idea.

Quoted 6 lines

Having pitch control on the playback side allows to forward the audio captured by the physical interface of the device (like I2S) to USB without performing any resampling between the two (when USB and I2S are not in sync). It's nice and convenient ... but I wonder if this is a feature or a hack ;)

Jerome, thanks a lot for your reply. The current implementation of the EP IN audio direction is timed by the USB host controller. Let's consider 48Khz samplerate and bInterval=1 fullspeed (8k packets per second). Every IN packet will always carry 6 audio frames. No matter how fast the real master clock (i.e. e.g. I2S of the gadget) runs. Until an xrun occurs, unfortunately. Even if the gadget configuration used implicit feedback, the incoming stream would still provide no synchronization information from the I2S hardware clock as the data stream is clocked by the USB host which controls the timing on the USB bus (and thus the moment of iso completion). Plus the stock usb-audio driver in Windows 10 does not support implicit feedback, according to
https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/usb-2-0-audio-drivers#audio-streaming
.

The problem is the fixed "slicing" of the samples into the packets which cannot be controlled. The same situation was on the host side before the feedback EP was introduced. Here the one preparing the slices (the "transmitter") is the gadget now. And the slicing pace must be controllable just like the slicing pace on the host is via the feedback EP. Because the gadget supports different playback and capture rates (which I find nice), I suggest a separate control element (Playback Pitch, Capture Pitch).
Of course the motivation is to avoid adaptive resampling in the gadget

Quoted 8 lines

in the direction I2S -> gadget interface -> USB host. But the very same motivation lead to implementation of the async feedback EP in the opposite direction. IMO it is not a hack, but an indispensable feature making the gadget usable for duplex operation with hardware (i.e. the real master) clock at the backend (all the other "clocks" are just software-generated slices/blocks of audio frames).
With regards,
Pavel.

Quoted 3 lines

Jerome, please do you still have your playback-side patches available or should I prepare them? Thanks a lot,

Yes, this is what I have tested:

https://gitlab.com/jbrunet/linux/-/commit/43f1930ba548e137a5d20b1801790fae83eaa1e0
https://gitlab.com/jbrunet/linux/-/commit/acb2ed9d9219488184cd2eb592fcdbf78b042a9e

It probably requires a rebase and cleaning before it is actually ready for prime time but it does work.

For now, I'd like to focus on getting this initial explicit feedback support in. There was no major complain on it, so I just have a minor tweak to do before I send the patchset again. Hopefully soon ...

Quoted 1 line

Pavel.

#14 lore raw

Re: [RFC PATCH v2 3/3] usb: gadget: u_audio: .... PCM Rate Shift for playback too?

Dne 27. 05. 21 v 11:52 Jerome Brunet napsal(a):

Quoted 137 lines · Pavel Hofman
Quoted 126 lines

Dne 24. 05. 21 v 18:09 Pavel Hofman napsal(a):

Quoted 115 lines

Dne 24. 05. 21 v 17:40 Jerome Brunet napsal(a):

Quoted 85 lines
Quoted 59 lines · Pavel Hofman

Dne 30. 04. 21 v 16:26 Jerome Brunet napsal(a):

Quoted 28 lines

From: Ruslan Bilovol <[email protected]>

This adds interface between userspace and feedback endpoint to report real
feedback frequency to the Host.

Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000"
mixer control)

Value in PPM is chosen to have correction value agnostic of the actual HW
rate, which the application is not necessarily dealing with, while still
retaining a good enough precision to allow smooth clock correction on the
playback side, if necessary.

Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact
on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved
bandwidth is rounded up to the next packet size.

Quoted 6 lines

Usage of this new control is easy to implement in existing userspace tools
like alsaloop from alsa-utils.

Hi, the existing patches solve the Host -> Gadget -> capturing application direction, controlling the host playback rate. The other direction (playback app -> gadget -> capturing host) is still paced by the host USB controller. Packet size is pre-calculated in u_audio_start_playback as rate/p_interval
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L441
and this fixed value is used for copying the audio data in u_audio_iso_complete
https://github.com/pavhofman/linux-rpi/blob/rpi-5.10.y/drivers/usb/gadget/function/u_audio.c#L124
.

That means if the gadget has a physical duplex audio device with single clock and runs a duplex operation, the path gadget-> host will not run synchronously with the physical audio device (the host -> gadget has already the feedback control implemented).

How about "duplicating" the existing ALSA mixer control "Capture Pitch 1000000" to "Playback Pitch 1000000" and using pitch-adjusted p_srate in the above-linked calculations? That should make the playback side run at the playback pitch requested by gadget userspace, IIUC.

Quoted 7 lines

For the duplex operation with single clock, the capture pitch value determined by the userspace chain (alsaloop, CamillaDSP, etc.) would be used for setting both the capture and playback pitch controls, making both directions run synchronously.

I can prepare patches based on Jerome's patchset should you find this solution acceptable.

Well I have experimented with pitch on the playback path.
It does work but I'm not sure if it actually makes sense which is why I have not not included it in RFC

If you need the playback and capture to run synchronously, you'd be better off with implicit feedback (In which the host will provide the same number of samples it received from the device)

With explicit feedback, we are (supposed) to tell the host to speed up or slow down to match the device clock. This means the device run asynchronously, and the host does the adaptation (if necessary). In such case, I'm not sure adding pitch control on the playback path is a good idea.

Quoted 6 lines

Having pitch control on the playback side allows to forward the audio captured by the physical interface of the device (like I2S) to USB without performing any resampling between the two (when USB and I2S are not in sync). It's nice and convenient ... but I wonder if this is a feature or a hack ;)

Jerome, thanks a lot for your reply. The current implementation of the EP IN audio direction is timed by the USB host controller. Let's consider 48Khz samplerate and bInterval=1 fullspeed (8k packets per second). Every IN packet will always carry 6 audio frames. No matter how fast the real master clock (i.e. e.g. I2S of the gadget) runs. Until an xrun occurs, unfortunately. Even if the gadget configuration used implicit feedback, the incoming stream would still provide no synchronization information from the I2S hardware clock as the data stream is clocked by the USB host which controls the timing on the USB bus (and thus the moment of iso completion). Plus the stock usb-audio driver in Windows 10 does not support implicit feedback, according to
https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/usb-2-0-audio-drivers#audio-streaming
.

The problem is the fixed "slicing" of the samples into the packets which cannot be controlled. The same situation was on the host side before the feedback EP was introduced. Here the one preparing the slices (the "transmitter") is the gadget now. And the slicing pace must be controllable just like the slicing pace on the host is via the feedback EP. Because the gadget supports different playback and capture rates (which I find nice), I suggest a separate control element (Playback Pitch, Capture Pitch).
Of course the motivation is to avoid adaptive resampling in the gadget

Quoted 8 lines

in the direction I2S -> gadget interface -> USB host. But the very same motivation lead to implementation of the async feedback EP in the opposite direction. IMO it is not a hack, but an indispensable feature making the gadget usable for duplex operation with hardware (i.e. the real master) clock at the backend (all the other "clocks" are just software-generated slices/blocks of audio frames).
With regards,
Pavel.

Quoted 3 lines

Jerome, please do you still have your playback-side patches available or should I prepare them? Thanks a lot,

Yes, this is what I have tested:

https://gitlab.com/jbrunet/linux/-/commit/43f1930ba548e137a5d20b1801790fae83eaa1e0
https://gitlab.com/jbrunet/linux/-/commit/acb2ed9d9219488184cd2eb592fcdbf78b042a9e

Quoted 6 lines

It probably requires a rebase and cleaning before it is actually ready for prime time but it does work.

For now, I'd like to focus on getting this initial explicit feedback support in. There was no major complain on it, so I just have a minor tweak to do before I send the patchset again. Hopefully soon ...

Thanks for the patches, they look good. When you send your final explicit FB patchset, I will rebase your two playback patches and put all my changes (mainly rebased Julian's multiple samplerates patches) on top to test the whole suite.

Pavel.