All of lore.kernel.org
 help / color / mirror / Atom feed
From: John Keeping <john@metanate.com>
To: Pavel Hofman <pavel.hofman@ivitera.com>
Cc: linux-usb@vger.kernel.org,
	Ruslan Bilovol <ruslan.bilovol@gmail.com>,
	Felipe Balbi <balbi@kernel.org>,
	Jerome Brunet <jbrunet@baylibre.com>,
	Julian Scheel <julian@jusst.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH v2 11/11] usb: gadget: f_uac2: Determining bInterval for HS and SS
Date: Wed, 22 Dec 2021 19:50:30 +0000	[thread overview]
Message-ID: <20211222195030.4d37dbc7.john@metanate.com> (raw)
In-Reply-To: <fd9646e9-0d2b-6d53-863e-2184e038476a@ivitera.com>

On Wed, 22 Dec 2021 14:35:07 +0100
Pavel Hofman <pavel.hofman@ivitera.com> wrote:

> Dne 21. 12. 21 v 13:29 John Keeping napsal(a):
> > On Mon, Dec 20, 2021 at 10:11:30PM +0100, Pavel Hofman wrote:  
> >> So far bInterval for HS and SS was fixed at 4, disallowing faster 
> >> samplerates. The patch determines the largest bInterval (4 to 1) 
> >> for which the required bandwidth of the max samplerate fits the
> >> max allowed packet size. If the required bandwidth exceeds max 
> >> bandwidth for single-packet mode (ep->mc=1), bInterval is left at 
> >> 1.  
> > 
> > I'm not sure if this is desirable - there are more concerns around 
> > the interval than just whether the bandwidth is available.
> > 
> > The nice thing about having the HS/SS interval at 4 when the FS
> > value is 1 is that these both correspond to 1ms, which means the 
> > calculations for minimum buffer & period sizes are the same for 
> > FS/HS/SS.  
> 
> Please do you see any specific place in u_audio.c where the interval of
> 1ms is assumed?
> 
> * Buffer/period size max limits are fixed
> * Bufer min size is calculated from the max_packet_size
> * snd_pcm_period_elapsed() is called when the current request fill
> overlaps the period boundary:
> 
> if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
> 		snd_pcm_period_elapsed(substream);
> 
> 
> The fixed HS bInterval=4 severely limits the available bandwidth,
> disallowing even the very basic 192kHz/2ch/24bits config.

Yes, but the problem is if the device enumerates as full-speed the
capability is no longer there.

I agree that is unlikely to be a problem in real use, but I think it
deserves consideration.

For the last few years I've been using bInterval == 1 but I also have a
hack to disable full-speed operation completely.  In my case this is
because I want to minimise latency and with the 1ms interval for FS the
minimum ALSA period size is too large.

Basically, I agree with wanting a smaller bInterval, but I want it for a
different reason and I'd like to see a patch that addresses both our use
cases ;-)

> In f_uac2.c both HS/SS the max packet size, async EP OUT feedback value, 
> as well as async EP IN momentary packet size calculations already take 
> into account the bInterval of the respective endpoint.
> 
> I have been using bInterval < 4 in most of my tests for almost a year,
> testing packet sizes at up to 1024 bytes per 125us uframe, both
> directions, and the gadget has been bitperfect for samplerates up to
> 4MHz (including correctly working async feedback, tested on linux (up to 
> 4MHz) and windows 10 WASAPI exclusive (up to 1.5MHz). For larger 
> samplerates tests I increased the buffers like in the patch below but I 
> did it just in case to minimize probability of xruns. It's not part of 
> this patchset and should be configured dynamically too, if actually 
> needed at all:

This is another case of a different trade-off - I use PREEMPT_RT to
minimise xruns and run with a period of 16 samples.

> > How do FS transfers work if the bandwidth requirements necessitate a
> >  smaller interval for HS/SS?  Doesn't that mean the FS transfers
> > must be too big?  
> 
> Only UAC2 HS/SS bIntervals are dynamic with this patch, FS stays fixed
> at 1ms. For HS/SS  the max packet size is calculated together with the
> bInterval, so that the largest bInterval possible to fit the ISOC max
> packetsize limits is chosen.

I'd really like to see FS mode become unsupported when the packet size
is too big.  This is a slight issue right now (for 1023 vs 1024) but
this patch makes it significantly worse for the high bandwidth case.

Right now I have this patch which is a hack but does at least result in
an error for the host when trying to enable audio at FS.  It would be
really nice to properly handle this in the composite gadget core so that
the audio function is exposed only at HS/SS with proper
DT_OTHER_SPEED_CONFIG handling, but currently that code assumes that the
same number of descriptors is provided for each speed.

-- 8< --
diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c
index 36fa6ef0581b..b4946409b38a 100644
--- a/drivers/usb/gadget/function/f_uac2.c
+++ b/drivers/usb/gadget/function/f_uac2.c
@@ -1356,6 +1356,9 @@ afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
 		return 0;
 	}
 
+	if (gadget->speed < USB_SPEED_HIGH && alt)
+		return -EINVAL;
+
 	if (intf == uac2->as_out_intf) {
 		uac2->as_out_alt = alt;
 
-- >8 --

> > I don't think there has ever been a check that the configured sample
> >  size, channel count and interval actually fit in the max packet
> > size for an endpoint.  Is that something that should be checked to
> > give an error on bind if the configuration can't work?  
> 
> The existing code has never had checks for any of that. Actually the
> dynamic bInterval calculation in this patch handles the bInterval and
> packetsize for configured parameters up to maximum ISOC bandwidth. Next 
> version of this patch will at least warn about exceeding the overall 
> available bandwidth.
> 
> There are many patches to go before the audio gadget becomes fool-proof,
> but at least it should be practically usable with these patches (when 
> finalized) and the gaudio controller example implementation.

Agreed, and I really appreciate the improvements you're making here.

The reason I suggested the new checks here is that it makes a lot of
sense if the bInterval value is exposed as part of the configfs
interface.  It means there's one extra value to set for high bandwidth
operation, rather than having it "just work", but I think the
latency/bandwidth tradeoffs here mean that there's no way for the kernel
to select the right value for all scenarios, so really we need to let
the user tell us what they want.


Regards,
John

  reply	other threads:[~2021-12-22 19:50 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-20 21:11 [PATCH v2 00/11] usb: gadget: audio: Multiple rates, dyn. bInterval Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 01/11] usb: gadget: u_audio: Subdevice 0 for capture ctls Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 02/11] usb: gadget: u_audio: Support multiple sampling rates Pavel Hofman
2021-12-21 11:35   ` John Keeping
2021-12-22  7:13     ` Pavel Hofman
2022-01-04 15:32       ` John Keeping
2022-01-05 10:55         ` Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 03/11] usb: gadget: f_uac2: " Pavel Hofman
2021-12-21 11:59   ` John Keeping
2021-12-22 10:01     ` Pavel Hofman
2022-01-04 15:33       ` John Keeping
2022-01-05 12:20         ` Pavel Hofman
2022-01-05 12:44           ` John Keeping
2022-01-05 14:05             ` Pavel Hofman
2022-01-06  8:45               ` Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 04/11] usb: gadget: f_uac1: " Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 05/11] usb: gadget: f_uac2: Renaming Clock Sources to fixed names Pavel Hofman
2021-12-21 12:02   ` John Keeping
2021-12-22 10:11     ` Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 06/11] usb: gadget: u_audio: Rate ctl notifies about current srate (0=stopped) Pavel Hofman
2021-12-21 12:07   ` John Keeping
2021-12-22 10:41     ` Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 07/11] usb: gadget: u_audio: Stopping PCM substream at capture/playback stop Pavel Hofman
2021-12-21 12:18   ` John Keeping
2021-12-22 12:26     ` Pavel Hofman
2021-12-28  9:04       ` [RFC: PATCH " Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 08/11] usb: gadget: u_audio: Adding suspend call Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 09/11] usb: gadget: f_uac2: Adding suspend callback Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 10/11] usb: gadget: f_uac1: " Pavel Hofman
2021-12-20 21:11 ` [PATCH v2 11/11] usb: gadget: f_uac2: Determining bInterval for HS and SS Pavel Hofman
2021-12-21 12:29   ` John Keeping
2021-12-22 13:35     ` Pavel Hofman
2021-12-22 19:50       ` John Keeping [this message]
2021-12-23  7:09         ` Pavel Hofman
2022-01-04 15:33           ` John Keeping
2022-01-05 11:31             ` Pavel Hofman
2022-01-06 14:32               ` John Keeping
2022-01-07 10:30                 ` Pavel Hofman
2021-12-21  7:59 ` [PATCH v2 00/11] usb: gadget: audio: Multiple rates, dyn. bInterval Greg Kroah-Hartman
2021-12-22 13:38   ` Pavel Hofman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211222195030.4d37dbc7.john@metanate.com \
    --to=john@metanate.com \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jbrunet@baylibre.com \
    --cc=julian@jusst.de \
    --cc=linux-usb@vger.kernel.org \
    --cc=pavel.hofman@ivitera.com \
    --cc=ruslan.bilovol@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.