From nobody Mon Feb 26 23:22:50 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TkGqv1kzkz5BtQg; Mon, 26 Feb 2024 23:22:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TkGqt61Tfz4V4f; Mon, 26 Feb 2024 23:22:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708989770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NROtqf0EwJGSVd9ZVcwSYxBRyYnIAZ6Qz2hqZ1F2ffo=; b=FNCQyD6WQSWaDUyMY/cm+dFTtc9POfigS+VMwAIwr6IUQro/d/dCtx0mLJbgfpKk5jqfRz Kwk5KyG3oMiiUuhlGWcqQ51Ysyija812j6hbpyWR2vnRRi5/4Tdgy1EDkarnPpqXJah1aK iyne+lJzB4Cmx41Y/g7bGd+Q3DhqSW6SwrqpkBCahvpkbn3sroigoGF39An1A9DT4vPjyM 7rXuR6dYkivjIsl/V/7y/Iwr1JJFEW3a8CXTvgDxqjPwmGfsw464hoc8LNpa65Sv8+3tnP dFx6B6c1nce4JkWtpUMxZP/4QwkY0Ush7ViCB4IcLAlOlUzsGMkLiVpqaIB3jg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1708989770; a=rsa-sha256; cv=none; b=eUqv60cs5lzOY2calOAyEgZ0rvUzjtJgLwxxTzmCHLRKo1nyXtrSwEmCpzM8fpFK1omjFy JlgrhL2DfUJjdzzX0WRSchf298C+C3aoyItq/2fdkSad0/kTTT3j2qSGvCFL54bKo3ZWz+ iKCx2Rw1eAVc7BUTXfJKC3FMB9TNvrEBmuyqr6bqeXfPUU6Jk/KTxn9G0YsIo7oczBzCi2 nmTxQt26nHWBwltApgcqU/kusCcAuEzagoS9iGty/KMUcFGt/eK16rGu201EeQUgwFipXy s3loKwR+i1eYDpqrrBxqr9SHQTgJ0oO4a5nGwwKUrQstJnT0LAgqkefd0XmPZw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708989770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NROtqf0EwJGSVd9ZVcwSYxBRyYnIAZ6Qz2hqZ1F2ffo=; b=rNBf9ikbpOwgtgbF3rZLAbMu69+icTp86seZLDrtTcvHu4XkNL9P/PWdGvKTVITr2uxzZ9 AhR5ptpaFD/E36O1sOwMFrcN8t5y5DpUsbG+iKilrsBiaDZNKK/jLavJxkVJvnoSSNO+mI w9+wupbBI3kgpCMyVExztBSjfaCGW3GudQvbrg7UCwz4GSkKnH3BhoXQokmyBpfWItb9p8 xCxA9VoUl3TPjRXUSvIO2A/8x6TciO0U0dupZ+Ox5Fx9zDZjYyPUAVbeBL7tGTG4qvG/mc cJ7EhzB/OhX1KzXPfk9lgiHzolzSAX1elmgADEx2fkxuAtgDNwhFiNtwep6Xxw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TkGqt56kyzpJy; Mon, 26 Feb 2024 23:22:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41QNMoBu028605; Mon, 26 Feb 2024 23:22:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41QNMo1E028602; Mon, 26 Feb 2024 23:22:50 GMT (envelope-from git) Date: Mon, 26 Feb 2024 23:22:50 GMT Message-Id: <202402262322.41QNMo1E028602@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Christos Margiolis Subject: git: 75358ca4fb54 - stable/14 - snd_uaudio(4): Adapt buffer length to buffer_ms tunable. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 75358ca4fb54390c313e5346574771c292bb764f Auto-Submitted: auto-generated The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=75358ca4fb54390c313e5346574771c292bb764f commit 75358ca4fb54390c313e5346574771c292bb764f Author: Florian Walpen AuthorDate: 2024-02-12 11:04:57 +0000 Commit: Christos Margiolis CommitDate: 2024-02-26 23:22:31 +0000 snd_uaudio(4): Adapt buffer length to buffer_ms tunable. Adapt the length of the driver side audio buffer to the USB transfer interval, which is adjustable through the buffer_ms tunable. This eliminates unnecessary latency in USB audio playback. To reduce power consumption caused by frequent CPU wakeups, increase the default buffer_ms value to 4ms. In combination with adaptive buffer length, this still results in less roundtrip latency compared to the previous 2ms default. Extend the buffer_ms value range to 1ms for low latency applications. MFC after: 2 weeks Reviewed by: christos Differential Revision: https://reviews.freebsd.org/D41942 (cherry picked from commit b2e97edffdab6ad1e9103a6bbe90300d78a961ed) --- sys/dev/sound/usb/uaudio.c | 70 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/sys/dev/sound/usb/uaudio.c b/sys/dev/sound/usb/uaudio.c index 917b6bd3f238..b0bf815f7797 100644 --- a/sys/dev/sound/usb/uaudio.c +++ b/sys/dev/sound/usb/uaudio.c @@ -96,7 +96,7 @@ static int uaudio_default_rate = 0; /* use rate list */ static int uaudio_default_bits = 32; static int uaudio_default_channels = 0; /* use default */ -static int uaudio_buffer_ms = 2; +static int uaudio_buffer_ms = 4; static bool uaudio_handle_hid = true; static SYSCTL_NODE(_hw_usb, OID_AUTO, uaudio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, @@ -110,6 +110,9 @@ SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_bits, CTLFLAG_RWTUN, SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_channels, CTLFLAG_RWTUN, &uaudio_default_channels, 0, "uaudio default sample channels"); +#define UAUDIO_BUFFER_MS_MIN 1 +#define UAUDIO_BUFFER_MS_MAX 8 + static int uaudio_buffer_ms_sysctl(SYSCTL_HANDLER_ARGS) { @@ -121,10 +124,10 @@ uaudio_buffer_ms_sysctl(SYSCTL_HANDLER_ARGS) if (err != 0 || req->newptr == NULL || val == uaudio_buffer_ms) return (err); - if (val > 8) - val = 8; - else if (val < 2) - val = 2; + if (val > UAUDIO_BUFFER_MS_MAX) + val = UAUDIO_BUFFER_MS_MAX; + else if (val < UAUDIO_BUFFER_MS_MIN) + val = UAUDIO_BUFFER_MS_MIN; uaudio_buffer_ms = val; @@ -133,7 +136,7 @@ uaudio_buffer_ms_sysctl(SYSCTL_HANDLER_ARGS) SYSCTL_PROC(_hw_usb_uaudio, OID_AUTO, buffer_ms, CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), uaudio_buffer_ms_sysctl, "I", - "uaudio buffering delay from 2ms to 8ms"); + "uaudio buffering delay in milliseconds, from 1 to 8"); #ifdef USB_DEBUG static int uaudio_debug; @@ -1310,13 +1313,60 @@ uaudio_detach(device_t dev) return (0); } +static uint32_t +uaudio_get_interval_frames(const usb_endpoint_descriptor_audio_t *ed) +{ + uint32_t frames = 1; + /* Isochronous transfer interval is 2^(bInterval - 1) frames. */ + if (ed->bInterval >= 1 && ed->bInterval <= 16) + frames = (1 << (ed->bInterval - 1)); + /* Limit transfer interval to maximum number of frames. */ + if (frames > UAUDIO_NFRAMES) + frames = UAUDIO_NFRAMES; + return (frames); +} + +static uint32_t +uaudio_get_buffer_ms(struct uaudio_softc *sc, uint32_t int_frames) +{ + uint32_t ms = 1; + uint32_t fps = usbd_get_isoc_fps(sc->sc_udev); + /* Make sure a whole USB transfer interval fits into the buffer. */ + if (fps >= 1000 && int_frames > 0 && int_frames <= UAUDIO_NFRAMES) { + /* Convert interval frames to milliseconds. */ + ms = ((int_frames * 1000) / fps); + } + /* Respect minimum buffer length set through buffer_ms tunable. */ + if (ms < uaudio_buffer_ms) + ms = uaudio_buffer_ms; + /* Limit buffer length to 8 milliseconds. */ + if (ms > UAUDIO_BUFFER_MS_MAX) + ms = UAUDIO_BUFFER_MS_MAX; + return (ms); +} + static uint32_t uaudio_get_buffer_size(struct uaudio_chan *ch, uint8_t alt) { struct uaudio_chan_alt *chan_alt = &ch->usb_alt[alt]; - /* We use 2 times 8ms of buffer */ - uint32_t buf_size = chan_alt->sample_size * - howmany(chan_alt->sample_rate * (UAUDIO_NFRAMES / 8), 1000); + uint32_t int_frames, ms, buf_size; + /* USB transfer interval in frames, from endpoint descriptor. */ + int_frames = uaudio_get_interval_frames(chan_alt->p_ed1); + /* Buffer length in milliseconds, and in bytes of audio data. */ + ms = uaudio_get_buffer_ms(ch->priv_sc, int_frames); + buf_size = chan_alt->sample_size * + howmany(chan_alt->sample_rate * ms, 1000); + return (buf_size); +} + +static uint32_t +uaudio_max_buffer_size(struct uaudio_chan *ch, uint8_t alt) +{ + struct uaudio_chan_alt *chan_alt = &ch->usb_alt[alt]; + uint32_t buf_size; + /* Maximum buffer length is 8 milliseconds. */ + buf_size = chan_alt->sample_size * + howmany(chan_alt->sample_rate * UAUDIO_BUFFER_MS_MAX, 1000); return (buf_size); } @@ -2626,7 +2676,7 @@ uaudio_chan_init(struct uaudio_chan *ch, struct snd_dbuf *b, buf_size = 0; for (x = 0; x != ch->num_alt; x++) { - uint32_t temp = uaudio_get_buffer_size(ch, x); + uint32_t temp = uaudio_max_buffer_size(ch, x); if (temp > buf_size) buf_size = temp; }