Skip to contentSkip to conversation

usb: gadget: uac2: support explicit channel counts

PATCH RESEND In review Kyle Farrell 4 participants 10 messages Oct 16 – Oct 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover usb: gadget: uac2: support explicit channel counts

USB Audio Class 2.0 permits up to 255 channels in an audio channel cluster via bNrChannels. Independently, a channel-location bitmap is specified via bmChannelConfig for up to 27 predefined spatial locations. Setting bmChannelConfig to zero indicates that the channels have no predefined spatial positions.

The current UAC2 gadget driver derives bNrChannels from the number of set bits in bmChannelConfig. Since bmChannelConfig provides 27 predefined spatial locations this limits playback and capture streams to 27 channels.

This series allows channel counts up to the UAC2 limit of 255 without requiring predefined spatial channel locations in bmChannelConfig. Existing ConfigFS channel-mask configuration continues to work unchanged. New optional playback and capture count attributes allow the number of channels to be specified separately from the channel-location bitmap.

The series is split so that the shared u_audio infrastructure is updated first without changing behavior, followed by the UAC2 functional change.

Patch 1:
Pass resolved playback and capture channel counts explicitly into the UAC1/UAC2 shared u_audio implementation instead of deriving them from channel masks.

Patch 2:
Add ConfigFS playback/capture channel-count attributes to the UAC2 function driver and use them when generating descriptors and stream configuration, allowing counts beyond the 27 predefined channel locations.

Testing

Testing was performed using a Raspberry Pi Zero 2 W configured as a USB UAC2 gadget. Streaming and descriptor validation were performed against both Linux and macOS hosts. Windows hosts were not tested.

UAC1
- Verified playback/capture operation via alsaloop.
- No descriptor or behavioral changes observed.

UAC2
- Verified legacy channel-mask configuration (1-27 channels).
- Verified explicit channel-count mode with 2, 28, 61, 62, 96 channels.
- Verified invalid channel-count/channel-mask combinations are rejected.
- Verified Feature Unit descriptor size limits at 61/62 channel boundary.
- Verified descriptors including:
* bNrChannels
* bmChannelConfig
* Feature Unit presence and bLength
* AudioControl wTotalLength
* endpoint wMaxPacketSize
- Verified ALSA-reported playback and capture channel counts.
- Verified streaming using alsaloop.

Changes in v2:
- Edit commit messages for clarity and brevity.
- Add Assisted-by: LLM attribution.
- No code changes.

v1: https://lore.kernel.org/linux-usb/[email protected]/

Kyle Farrell (2):
usb: gadget: u_audio: pass channel counts explicitly
usb: gadget: f_uac2: allow explicit channel count independent of
channel mask

 .../ABI/testing/configfs-usb-gadget-uac2      | 18 ++++-
 Documentation/usb/gadget-testing.rst          | 18 ++++-
 drivers/usb/gadget/function/f_uac1.c          |  2 +
 drivers/usb/gadget/function/f_uac2.c          | 69 ++++++++++++++-----
 drivers/usb/gadget/function/u_audio.c         | 32 ++++-----
 drivers/usb/gadget/function/u_audio.h         |  2 +
 drivers/usb/gadget/function/u_uac2.h          |  4 ++
 7 files changed, 105 insertions(+), 40 deletions(-)

base-commit: d58dffe9ee2c8883193959ff4ef995ec07932874

Signature · 1 line
2.34.1
#1 lore raw

PATCH RESEND v2 1/2 usb: gadget: u_audio: pass channel counts explicitly

u_audio currently derives the channel count from the channel mask in struct uac_params in several places. Instead, pass the resolved count explicitly, so the shared audio implementation does not depend on how an individual USB audio function represents its channel configuration.

Initialize the new channel count fields from the existing channel masks. This preserves generated descriptors and makes no change to user-visible behavior.

Assisted-by LLM
---
 drivers/usb/gadget/function/f_uac1.c  |  2 ++
 drivers/usb/gadget/function/f_uac2.c  |  2 ++
 drivers/usb/gadget/function/u_audio.c | 32 ++++++++++++---------------
 drivers/usb/gadget/function/u_audio.h |  2 ++
 4 files changed, 20 insertions(+), 18 deletions(-)
drivers/usb/gadget/function/f_uac1.c +2 -0
index 7a81cd176abd..17f856b96d2a 100644
@@ -1448,6 +1448,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
 	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
 	audio->params.c_chmask = audio_opts->c_chmask;
+	audio->params.c_channels = num_channels(audio_opts->c_chmask);
 	memcpy(audio->params.c_srates, audio_opts->c_srates,
 			sizeof(audio->params.c_srates));
 	audio->params.c_ssize = audio_opts->c_ssize;
@@ -1461,6 +1462,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 		audio->params.p_fu.volume_res = audio_opts->p_volume_res;
 	}
 	audio->params.p_chmask = audio_opts->p_chmask;
+	audio->params.p_channels = num_channels(audio_opts->p_chmask);
 	memcpy(audio->params.p_srates, audio_opts->p_srates,
 			sizeof(audio->params.p_srates));
 	audio->params.p_ssize = audio_opts->p_ssize;
drivers/usb/gadget/function/f_uac2.c +2 -0
index d8cf710085a0..46255ad0a9cf 100644
@@ -1308,6 +1308,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	agdev->gadget = gadget;
 
 	agdev->params.p_chmask = uac2_opts->p_chmask;
+	agdev->params.p_channels = num_channels(uac2_opts->p_chmask);
 	memcpy(agdev->params.p_srates, uac2_opts->p_srates,
 			sizeof(agdev->params.p_srates));
 	agdev->params.p_ssize = uac2_opts->p_ssize;
@@ -1320,6 +1321,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		agdev->params.p_fu.volume_res = uac2_opts->p_volume_res;
 	}
 	agdev->params.c_chmask = uac2_opts->c_chmask;
+	agdev->params.c_channels = num_channels(uac2_opts->c_chmask);
 	memcpy(agdev->params.c_srates, uac2_opts->c_srates,
 			sizeof(agdev->params.c_srates));
 	agdev->params.c_ssize = uac2_opts->c_ssize;
drivers/usb/gadget/function/u_audio.c +14 -18
index f0ee83abcebc..de0622ee72ab 100644
@@ -390,25 +390,22 @@ static int uac_pcm_open(struct snd_pcm_substream *substream)
 	struct uac_params *params;
 	struct uac_rtd_params *prm;
 	int p_ssize, c_ssize;
-	int p_chmask, c_chmask;
 
 	audio_dev = uac->audio_dev;
 	params = &audio_dev->params;
 	p_ssize = params->p_ssize;
 	c_ssize = params->c_ssize;
-	p_chmask = params->p_chmask;
-	c_chmask = params->c_chmask;
 	uac->p_residue_mil = 0;
 
 	runtime->hw = uac_pcm_hardware;
 
 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 		runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
-		runtime->hw.channels_min = num_channels(p_chmask);
+		runtime->hw.channels_min = params->p_channels;
 		prm = &uac->p_prm;
 	} else {
 		runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
-		runtime->hw.channels_min = num_channels(c_chmask);
+		runtime->hw.channels_min = params->c_channels;
 		prm = &uac->c_prm;
 	}
 
@@ -736,8 +733,7 @@ int u_audio_start_playback(struct g_audio *audio_dev)
 		factor = 8000;
 
 	/* pre-compute some values for iso_complete() */
-	uac->p_framesize = params->p_ssize *
-			    num_channels(params->p_chmask);
+	uac->p_framesize = params->p_ssize * params->p_channels;
 	uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
 	p_pktsize = min_t(unsigned int,
 				uac->p_framesize *
@@ -1205,7 +1201,7 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 	struct snd_pcm *pcm;
 	struct snd_kcontrol *kctl;
 	struct uac_params *params;
-	int p_chmask, c_chmask;
+	unsigned int p_channels, c_channels;
 	int i, err;
 
 	if (!g_audio)
@@ -1218,10 +1214,10 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 	uac->audio_dev = g_audio;
 
 	params = &g_audio->params;
-	p_chmask = params->p_chmask;
-	c_chmask = params->c_chmask;
+	p_channels = params->p_channels;
+	c_channels = params->c_channels;
 
-	if (c_chmask) {
+	if (c_channels) {
 		struct uac_rtd_params *prm = &uac->c_prm;
 
 		spin_lock_init(&prm->lock);
@@ -1245,7 +1241,7 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 		}
 	}
 
-	if (p_chmask) {
+	if (p_channels) {
 		struct uac_rtd_params *prm = &uac->p_prm;
 
 		spin_lock_init(&prm->lock);
@@ -1284,7 +1280,7 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 	 * Create a substream only for non-zero channel streams
 	 */
 	err = snd_pcm_new(uac->card, pcm_name, 0,
-			       p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
+			       p_channels ? 1 : 0, c_channels ? 1 : 0, &pcm);
 	if (err < 0)
 		goto snd_fail;
 
@@ -1299,12 +1295,12 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 	 * 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))
+	if ((c_channels && g_audio->in_ep_fback)
+			|| (p_channels && params->p_fu.id)
+			|| (c_channels && params->c_fu.id))
 		strscpy(card->mixername, card_name);
 
-	if (c_chmask && g_audio->in_ep_fback) {
+	if (c_channels && g_audio->in_ep_fback) {
 		kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL],
 				    &uac->c_prm);
 		if (!kctl) {
@@ -1320,7 +1316,7 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 			goto snd_fail;
 	}
 
-	if (p_chmask) {
+	if (p_channels) {
 		kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL],
 				    &uac->p_prm);
 		if (!kctl) {
drivers/usb/gadget/function/u_audio.h +2 -0
index 9512b8fccfaa..46e1d6b5ca4e 100644
@@ -41,12 +41,14 @@ struct uac_fu_params {
 struct uac_params {
 	/* playback */
 	int p_chmask;	/* channel mask */
+	unsigned int p_channels;	/* resolved number of channels */
 	int p_srates[UAC_MAX_RATES];	/* available rates in Hz (0 terminated list) */
 	int p_ssize;	/* sample size */
 	struct uac_fu_params p_fu;	/* Feature Unit parameters */
 
 	/* capture */
 	int c_chmask;	/* channel mask */
+	unsigned int c_channels;	/* resolved number of channels */
 	int c_srates[UAC_MAX_RATES];	/* available rates in Hz (0 terminated list) */
 	int c_ssize;	/* sample size */
 	struct uac_fu_params c_fu;	/* Feature Unit parameters */
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/2 RESEND] usb: gadget: u_audio: pass channel counts explicitly

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

u_audio currently derives the channel count from the channel mask in struct uac_params in several places. Instead, pass the resolved count explicitly, so the shared audio implementation does not depend on how an individual USB audio function represents its channel configuration.

Initialize the new channel count fields from the existing channel masks. This preserves generated descriptors and makes no change to user-visible behavior.

Assisted-by LLM

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/2 RESEND] usb: gadget: u_audio: pass channel counts explicitly

In reply to Kyle Farrell
Quoted 12 lines · Kyle Farrell

u_audio currently derives the channel count from the channel mask in struct uac_params in several places.  Instead, pass the resolved count explicitly, so the shared audio implementation does not depend on how an individual USB audio function represents its channel configuration.

Initialize the new channel count fields from the existing channel masks. This preserves generated descriptors and makes no change to user-visible behavior.

Assisted-by LLM

---

...

Quoted 9 lines
drivers/usb/gadget/function/u_audio.h +0 -0
index 9512b8fccfaa..46e1d6b5ca4e 100644
@@ -41,12 +41,14 @@ struct uac_fu_params {
  struct uac_params {
         /* playback */

        int p_chmask;   /* channel mask */

With these changes, the channel masks are no longer used in u_audio.c, so we can remove them.

Quoted 11 lines

+       unsigned int p_channels;        /* resolved number of channels */ 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 */
+       unsigned int c_channels;        /* resolved number of channels */ int c_srate;    /* rate in Hz */
        int c_ssize;    /* sample size */
        struct uac_fu_params c_fu;      /* Feature Unit parameters */

#4 lore raw

Re: [PATCH v2 1/2 RESEND] usb: gadget: u_audio: pass channel counts explicitly

Quoted 14 lines · Francesco Lavra
Quoted 12 lines · Kyle Farrell

u_audio currently derives the channel count from the channel mask in struct uac_params in several places.  Instead, pass the resolved count explicitly, so the shared audio implementation does not depend on how an individual USB audio function represents its channel configuration.

Initialize the new channel count fields from the existing channel masks. This preserves generated descriptors and makes no change to user-visible behavior.

Assisted-by LLM

---

...

Quoted 26 lines
Quoted 9 lines
drivers/usb/gadget/function/u_audio.h +0 -0
index 9512b8fccfaa..46e1d6b5ca4e 100644
@@ -41,12 +41,14 @@ struct uac_fu_params {
  struct uac_params {
         /* playback */

        int p_chmask;   /* channel mask */

With these changes, the channel masks are no longer used in u_audio.c, so we can remove them.

Quoted 13 lines

+       unsigned int p_channels;        /* resolved number of channels */
        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 */
+       unsigned int c_channels;        /* resolved number of channels */
        int c_srate;    /* rate in Hz */
        int c_ssize;    /* sample size */
        struct uac_fu_params c_fu;      /* Feature Unit parameters */

Agreed, and thanks for taking a look. The resolved channel counts replace the channel masks. I'll remove them in v3.

#5 lore raw

PATCH RESEND v2 2/2 usb: gadget: f_uac2: allow explicit channel count independent of channel mask

In reply to Kyle Farrell

The UAC2 gadget driver currently derives bNrChannels from the number of set bits in bmChannelConfig. Since the bmChannelConfig channel-location bitmap provides only 27 predefined channel locations, this limits playback and capture streams to 27 channels. UAC2 permits bNrChannels values up to 255, independent of bmChannelConfig.

Add ConfigFS p_channels and c_channels attributes to configure the playback and capture channel counts explicitly. Preserve the existing p_chmask and c_chmask interface and semantics. The default value of zero for p_channels and c_channels continues to derive the channel count from the corresponding p_chmask and c_chmask.

When an explicit channel count and a nonzero channel mask are both specified, require the number of set bits in the mask to match the channel count. Partial channel-location assignments permitted by UAC2 are therefore not supported. Additionally, reject Feature Unit configurations whose descriptor length would exceed the one-byte bLength field.

Assisted-by LLM
---
 .../ABI/testing/configfs-usb-gadget-uac2      | 18 ++++-
 Documentation/usb/gadget-testing.rst          | 18 ++++-
 drivers/usb/gadget/function/f_uac2.c          | 71 +++++++++++++------
 drivers/usb/gadget/function/u_uac2.h          |  4 ++
 4 files changed, 87 insertions(+), 24 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +16 -2
index 133e995c3e92..8e264843e492 100644
@@ -5,7 +5,14 @@ Description:
 		The attributes:
 
 		=====================	=======================================
-		c_chmask		capture channel mask
+		c_chmask		capture channel-location mask. The number of
+					capture channels is derived from the number of
+					set bits unless c_channels is specified.
+		c_channels		explicit capture channel count. A value of 0
+					derives the channel count from c_chmask. When
+					nonzero, c_chmask specifies channel locations
+					and may be 0. If both c_channels and c_chmask
+					are nonzero, the values must agree.
 		c_srate			list of capture sampling rates (comma-separated)
 		c_ssize			capture sample size (bytes)
 		c_hs_bint		capture bInterval for HS/SS (1-4: fixed, 0: auto)
@@ -20,7 +27,14 @@ Description:
 		c_volume_res		capture volume control resolution
 					(in 1/256 dB)
 		fb_max			maximum extra bandwidth in async mode
-		p_chmask		playback channel mask
+		p_chmask		playback channel-location mask. The number of
+					playback channels is derived from the number of
+					set bits unless p_channels is specified.
+		p_channels		explicit playback channel count. A value of 0
+					derives the channel count from p_chmask. When
+					nonzero, p_chmask specifies channel locations
+					and may be 0. If both p_channels and p_chmask
+					are nonzero, the values must agree.
 		p_srate			list of playback sampling rates (comma-separated)
 		p_ssize			playback sample size (bytes)
 		p_hs_bint		playback bInterval for HS/SS (1-4: fixed, 0: auto)
Documentation/usb/gadget-testing.rst +16 -2
index 4921e5307d49..c050218291d1 100644
@@ -741,7 +741,14 @@ The function name to use when creating the function directory is "uac2".
 The uac2 function provides these attributes in its function directory:
 
 	================ ====================================================
-	c_chmask         capture channel mask
+	c_chmask         capture channel-location mask. The number of
+			 capture channels is derived from the number of
+			 set bits unless c_channels is specified.
+	c_channels	 explicit capture channel count. A value of 0
+			 derives the channel count from c_chmask. When
+			 nonzero, c_chmask specifies channel locations
+			 and may be 0. If both c_channels and c_chmask
+			 are nonzero, the values must agree.
 	c_srate          list of capture sampling rates (comma-separated)
 	c_ssize          capture sample size (bytes)
 	c_sync           capture synchronization type (async/adaptive)
@@ -752,7 +759,14 @@ The uac2 function provides these attributes in its function directory:
 	c_volume_res     capture volume control resolution (in 1/256 dB)
 	c_hs_bint        capture bInterval for HS/SS (1-4: fixed, 0: auto)
 	fb_max           maximum extra bandwidth in async mode
-	p_chmask         playback channel mask
+	p_chmask         playback channel-location mask. The number of
+			 playback channels is derived from the number of
+			 set bits unless p_channels is specified.
+	p_channels	 explicit playback channel count. A value of 0
+			 derives the channel count from p_chmask. When
+			 nonzero, p_chmask specifies channel locations
+			 and may be 0. If both p_channels and p_chmask
+			 are nonzero, the values must agree.
 	p_srate          list of playback sampling rates (comma-separated)
 	p_ssize          playback sample size (bytes)
 	p_mute_present   playback mute control enable
drivers/usb/gadget/function/f_uac2.c +51 -20

177 lines not on this page · raw message

drivers/usb/gadget/function/u_uac2.h +4 -0
index 0df808289ded..bacd841c9df6 100644
@@ -17,10 +17,12 @@
 #include "uac_common.h"
 
 #define UAC2_DEF_PCHMASK 0x3
+#define UAC2_DEF_PCHANNELS 0  /* 0 = derive from p_chmask */
 #define UAC2_DEF_PSRATE 48000
 #define UAC2_DEF_PSSIZE 2
 #define UAC2_DEF_PHSBINT 0
 #define UAC2_DEF_CCHMASK 0x3
+#define UAC2_DEF_CCHANNELS 0  /* 0 = derive from c_chmask */
 #define UAC2_DEF_CSRATE 64000
 #define UAC2_DEF_CSSIZE 2
 #define UAC2_DEF_CHSBINT 0
@@ -46,11 +48,13 @@ struct f_uac2_opts {
 	int				p_srates[UAC_MAX_RATES];
 	int				p_ssize;
 	u8				p_hs_bint;
+	u8				p_channels;
 	int				c_chmask;
 	int				c_srates[UAC_MAX_RATES];
 	int				c_ssize;
 	int				c_sync;
 	u8				c_hs_bint;
+	u8				c_channels;
 
 	bool			p_mute_present;
 	bool			p_volume_present;
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 2/2 RESEND] usb: gadget: f_uac2: allow explicit channel count independent of channel mask

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] usb: gadget: u_audio: heap buffer overflow in u_audio_iso_complete() due to missing bounds…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] usb: gadget: u_audio: heap buffer overflow in u_audio_iso_complete() due to missing bounds check
- [High] usb: gadget: f_uac2: u16 truncation in get_max_bw_for_bint() leads to undersized wMaxPacketSize
- [High] usb: gadget: f_uac2: memory leak and race from shared static globals
--

commit c9264c2b1a112c6d9a2f079577364a2e863c803f
Author: Kyle Farrell <[email protected]>

usb: gadget: f_uac2: allow explicit channel count independent of channel mask

The UAC2 gadget driver currently derives bNrChannels from the number of set bits in bmChannelConfig. Since the bmChannelConfig channel-location bitmap provides only 27 predefined channel locations, this limits playback and capture streams to 27 channels. UAC2 permits bNrChannels values up to 255, independent of bmChannelConfig.

Quoted 4 lines
drivers/usb/gadget/function/f_uac2.c +0 -0
index 46255ad0a9cfa..c7801bb8765e6 100644

[ ... ]

Quoted 4 lines
drivers/usb/gadget/function/f_uac2.c +0 -0
@@ -689,11 +699,11 @@ static int get_max_bw_for_bint(const struct f_uac2_opts *uac2_opts,
 		// Win10 requires max packet size + 1 frame
 		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
 		// updated srate is always bigger, therefore DIV_ROUND_UP always yields +1

drivers/usb/gadget/function/f_uac2.c static int get_max_bw_for_bint(const struct f_uac2_opts *uac2_opts, · line 709

Quoted 10 lines
drivers/usb/gadget/function/f_uac2.c +2 -2
-		max_size_bw = num_channels(chmask) * ssize *
+		max_size_bw = channels * ssize *
 			(DIV_ROUND_UP(srate, factor / (1 << (bint - 1))));
 	} else {
 		// adding 1 frame provision for Win10
-		max_size_bw = num_channels(chmask) * ssize *
+		max_size_bw = channels * ssize *
 			(DIV_ROUND_UP(srate, factor / (1 << (bint - 1))) + 1);
 	}
 	return max_size_bw;

[Severity: High]
Can this overflow the 16-bit capacity of max_size_bw?

With channel counts up to 255 independent of the channel mask, this calculation can exceed 65535, causing the required bandwidth to be silently truncated.

If this truncates, could it bypass the validation check in set_ep_max_packet_size_bint() and allow invalid, undersized wMaxPacketSize dimensions to be programmed:

drivers/usb/gadget/function/f_uac2.c:set_ep_max_packet_size_bint() {
    ...
    if (max_size_bw <= max_size_ep)
        dev_dbg(dev,
            "%s %s: Would use wMaxPacketSize %d and bInterval %d\n",
            speed_names[speed], dir, max_size_bw, bint);
    else {
    ...
}

This would severely undersize downstream ALSA ring buffer chunk allocations and cause data drops.

[ ... ]

drivers/usb/gadget/function/f_uac2.c afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) · line 1090

Quoted 9 lines
drivers/usb/gadget/function/f_uac2.c +1 -1
@@ -1059,12 +1084,12 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		return PTR_ERR(us);
 
 	if (FUOUT_EN(uac2_opts)) {
-		out_feature_unit_desc = build_fu_desc(uac2_opts->c_chmask);
+		out_feature_unit_desc = build_fu_desc(false, uac2_opts);
 		if (!out_feature_unit_desc)
 			return -ENOMEM;
 	}

[Severity: High]
Does dynamically allocating memory and assigning it to the shared static global out_feature_unit_desc overwrite prior instance allocations if multiple UAC2 instances are bound to a composite gadget via configfs, leaking memory?

[ ... ]

drivers/usb/gadget/function/f_uac2.c afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) · line 1129

Quoted 9 lines
drivers/usb/gadget/function/f_uac2.c +2 -2
@@ -1099,13 +1124,13 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 
 
 	/* Initialize the configurable parameters */
-	usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
+	usb_out_it_desc.bNrChannels = uac2_num_channels(false, uac2_opts);
 	usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
-	io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
+	io_in_it_desc.bNrChannels = uac2_num_channels(true, uac2_opts);

[Severity: High]
Does modifying static descriptor templates like usb_out_it_desc directly during bind cause data races and corrupted descriptor fields if concurrent bindings occur?

[Severity: Critical]
With the channel count expanded to 255, could this cause a heap buffer overflow downstream in drivers/usb/gadget/function/u_audio.c?

When uac->p_framesize is dramatically increased by the larger channel counts, and residue accumulation triggers in u_audio_iso_complete():

drivers/usb/gadget/function/u_audio.c:u_audio_iso_complete() {
    ...
    if ((unsigned int) div_result >= uac->p_framesize) {
        req->length += uac->p_framesize;
        uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
    ...
    if (unlikely(pending < req->actual)) {
        memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
        memcpy(req->buf + pending, runtime->dma_area, req->actual - pending);
    ...
}

Will this memcpy overflow req->buf by up to p_framesize because req->length was increased without verifying it stays within ep->maxpacket?

Request buffers are tightly packed at intervals of ep->maxpacket:

drivers/usb/gadget/function/u_audio.c:u_audio_start_playback() {
    ...
    req->buf = prm->rbuf + i * ep->maxpacket;
    ...
}

This means any overflow immediately corrupts the next request's active DMA memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH v2 2/2 RESEND] usb: gadget: f_uac2: allow explicit channel count independent of channel mask

Quoted 25 lines · On 2026-10-16 10:07, [email protected] wrote:
Quoted 4 lines
drivers/usb/gadget/function/f_uac2.c +0 -0
index 46255ad0a9cfa..c7801bb8765e6 100644

[ ... ]

Quoted 6 lines
drivers/usb/gadget/function/f_uac2.c +0 -0
@@ -689,11 +699,11 @@ static int get_max_bw_for_bint(const struct 
 f_uac2_opts *uac2_opts,
 		// Win10 requires max packet size + 1 frame
 		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
 		// updated srate is always bigger, therefore DIV_ROUND_UP always 

yields +1

drivers/usb/gadget/function/f_uac2.c static int get_max_bw_for_bint(const struct f_uac2_opts *uac2_opts, · line 709

Quoted 10 lines
drivers/usb/gadget/function/f_uac2.c +2 -2
-		max_size_bw = num_channels(chmask) * ssize *
+		max_size_bw = channels * ssize *
 			(DIV_ROUND_UP(srate, factor / (1 << (bint - 1))));
 	} else {
 		// adding 1 frame provision for Win10
-		max_size_bw = num_channels(chmask) * ssize *
+		max_size_bw = channels * ssize *
 			(DIV_ROUND_UP(srate, factor / (1 << (bint - 1))) + 1);
 	}
 	return max_size_bw;

[Severity: High]
Can this overflow the 16-bit capacity of max_size_bw?

Quoted 2 lines

With channel counts up to 255 independent of the channel mask, this calculation can exceed 65535, causing the required bandwidth to be silently truncated.

The max_size_bw truncation is a valid issue: expanded channel counts can overflow the 16-bit calculation. I plan to widen the bandwidth calculation and comparison types in v3. The existing warning-and-clamp behavior will be preserved.

Quoted 10 lines

[Severity: High]
Does dynamically allocating memory and assigning it to the shared static global out_feature_unit_desc overwrite prior instance allocations if multiple UAC2 instances are bound to a composite gadget via configfs, leaking memory?

[ ... ]
[Severity: High]
Does modifying static descriptor templates like usb_out_it_desc directly during bind cause data races and corrupted descriptor fields if concurrent bindings occur?

The two shared-global issues predate this series. Regarding this patch series, the changes do not alter descriptor ownership, lifetime, or synchronization. This series does not introduce or change any shared descriptor state when constructing the descriptors. The overall concern is valid and separate from the explicit channel-count series presented here.

Quoted 25 lines

[Severity: Critical]
With the channel count expanded to 255, could this cause a heap buffer overflow downstream in drivers/usb/gadget/function/u_audio.c?

When uac->p_framesize is dramatically increased by the larger channel counts, and residue accumulation triggers in u_audio_iso_complete():

drivers/usb/gadget/function/u_audio.c:u_audio_iso_complete() {
    ...
    if ((unsigned int) div_result >= uac->p_framesize) {
        req->length += uac->p_framesize;
        uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
    ...
    if (unlikely(pending < req->actual)) {
        memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
        memcpy(req->buf + pending, runtime->dma_area, req->actual - 
pending);
    ...
}

Will this memcpy overflow req->buf by up to p_framesize because req->length was increased without verifying it stays within ep->maxpacket?

Request buffers are tightly packed at intervals of ep->maxpacket:

Quoted 8 lines
drivers/usb/gadget/function/u_audio.c:u_audio_start_playback() {
    ...
    req->buf = prm->rbuf + i * ep->maxpacket;
    ...
}

This means any overflow immediately corrupts the next request's active DMA memory.

The overflow in u_audio_iso_complete(): this is very interesting. I'm able to achieve an oversized request using existing channel mask configuration (27 channels, 4 bytes/sample, 19200 Hz sampling rate, p_hs_bint=3). The endpoint bandwidth calculation reported a required packet size of 1080 bytes, exceeding the 1024-byte high-speed endpoint limit.

I instrumented the driver to detect oversized requests and clamp the request length before the memcpy. The warning confirmed that req->length exceeded ep->maxpacket by 56 bytes, although the test requires a bandwidth- insufficient endpoint configuration.

This demonstrates that the underlying bounds issue is reachable without the new channel-count attributes. Looks like this will expand to a 4-patch series. The u_audio_iso_complete() patch may need to stand on its own ahead of the other patches.

Thanks,
Kyle

#8 lore raw
In reply to Kyle Farrell
Quoted 54 lines · Kyle Farrell

USB Audio Class 2.0 permits up to 255 channels in an audio channel cluster via bNrChannels. Independently, a channel-location bitmap is specified via bmChannelConfig for up to 27 predefined spatial locations. Setting bmChannelConfig to zero indicates that the channels have no predefined spatial positions.

The current UAC2 gadget driver derives bNrChannels from the number of set bits in bmChannelConfig. Since bmChannelConfig provides 27 predefined spatial locations this limits playback and capture streams to 27 channels.

This series allows channel counts up to the UAC2 limit of 255 without requiring predefined spatial channel locations in bmChannelConfig. Existing ConfigFS channel-mask configuration continues to work unchanged. New optional playback and capture count attributes allow the number of channels to be specified separately from the channel-location bitmap.

The series is split so that the shared u_audio infrastructure is updated first without changing behavior, followed by the UAC2 functional change.

Patch 1:
Pass resolved playback and capture channel counts explicitly into the UAC1/UAC2 shared u_audio implementation instead of deriving them from channel masks.

Patch 2:
Add ConfigFS playback/capture channel-count attributes to the UAC2 function driver and use them when generating descriptors and stream configuration, allowing counts beyond the 27 predefined channel locations.

Testing

Testing was performed using a Raspberry Pi Zero 2 W configured as a USB UAC2 gadget. Streaming and descriptor validation were performed against both Linux and macOS hosts. Windows hosts were not tested.

UAC1
- Verified playback/capture operation via alsaloop.
- No descriptor or behavioral changes observed.

UAC2
- Verified legacy channel-mask configuration (1-27 channels).
- Verified explicit channel-count mode with 2, 28, 61, 62, 96 channels.
- Verified invalid channel-count/channel-mask combinations are rejected.
- Verified Feature Unit descriptor size limits at 61/62 channel boundary.
- Verified descriptors including:
* bNrChannels
* bmChannelConfig
* Feature Unit presence and bLength
* AudioControl wTotalLength
* endpoint wMaxPacketSize
- Verified ALSA-reported playback and capture channel counts.
- Verified streaming using alsaloop.

Quoted 4 lines

Changes in v2:
- Edit commit messages for clarity and brevity.
- Add Assisted-by: LLM attribution.
- No code changes.

Why was RESEND needed here? Saying that would be nice.

Also, sashiko points out a number of problems...

thanks,

greg k-h

#9 lore raw
Quoted 63 lines · Greg Kroah-Hartman
Quoted 57 lines · Kyle Farrell

USB Audio Class 2.0 permits up to 255 channels in an audio channel cluster via bNrChannels. Independently, a channel-location bitmap is specified via bmChannelConfig for up to 27 predefined spatial locations. Setting bmChannelConfig to zero indicates that the channels have no predefined spatial positions.

The current UAC2 gadget driver derives bNrChannels from the number of set bits in bmChannelConfig. Since bmChannelConfig provides 27 predefined spatial locations this limits playback and capture streams to 27 channels.

This series allows channel counts up to the UAC2 limit of 255 without requiring predefined spatial channel locations in bmChannelConfig. Existing ConfigFS channel-mask configuration continues to work unchanged. New optional playback and capture count attributes allow the number of channels to be specified separately from the channel-location bitmap.

The series is split so that the shared u_audio infrastructure is updated first without changing behavior, followed by the UAC2 functional change.

Patch 1:
Pass resolved playback and capture channel counts explicitly into the UAC1/UAC2 shared u_audio implementation instead of deriving them from channel masks.

Patch 2:
Add ConfigFS playback/capture channel-count attributes to the UAC2 function driver and use them when generating descriptors and stream configuration, allowing counts beyond the 27 predefined channel locations.

Testing

Testing was performed using a Raspberry Pi Zero 2 W configured as a USB UAC2 gadget. Streaming and descriptor validation were performed against both Linux and macOS hosts. Windows hosts were not tested.

UAC1
- Verified playback/capture operation via alsaloop.
- No descriptor or behavioral changes observed.

UAC2
- Verified legacy channel-mask configuration (1-27 channels).
- Verified explicit channel-count mode with 2, 28, 61, 62, 96
channels.
- Verified invalid channel-count/channel-mask combinations are
rejected.
- Verified Feature Unit descriptor size limits at 61/62 channel
boundary.
- Verified descriptors including:
* bNrChannels
* bmChannelConfig
* Feature Unit presence and bLength
* AudioControl wTotalLength
* endpoint wMaxPacketSize
- Verified ALSA-reported playback and capture channel counts.
- Verified streaming using alsaloop.

Quoted 4 lines

Changes in v2:
- Edit commit messages for clarity and brevity.
- Add Assisted-by: LLM attribution.
- No code changes.

Quoted 7 lines

Why was RESEND needed here? Saying that would be nice.

Also, sashiko points out a number of problems...

thanks,

greg k-h

I resent v2 as both it and v1 had received no review. I should have stated that in the cover letter.

I'm reviewing the Sashiko findings and will address relevant issues before submitting a revised series.

Thanks,
Kyle