public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: John Keeping <jkeeping@inmusicbrands.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: linux-usb@vger.kernel.org, stable@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Kees Cook <kees@kernel.org>,
	Abdul Rahim <abdul.rahim@myyahoo.com>,
	Michael Grzeschik <m.grzeschik@pengutronix.de>,
	Jeff Johnson <quic_jjohnson@quicinc.com>,
	Felipe Balbi <balbi@ti.com>, Daniel Mack <zonque@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: f_midi: fix MIDI Streaming descriptor lengths
Date: Thu, 30 Jan 2025 10:59:05 +0000	[thread overview]
Message-ID: <Z5tbealYSvl7S72l-jkeeping@inmusicbrands.com> (raw)
In-Reply-To: <87sep060f4.wl-tiwai@suse.de>

On Thu, Jan 30, 2025 at 11:50:07AM +0100, Takashi Iwai wrote:
> On Wed, 29 Jan 2025 18:31:35 +0100,
> John Keeping wrote:
> > 
> > On Wed, Jan 29, 2025 at 05:40:04PM +0100, Takashi Iwai wrote:
> > > On Wed, 29 Jan 2025 17:05:19 +0100,
> > > John Keeping wrote:
> > > > 
> > > > In the two loops before setting the MIDIStreaming descriptors,
> > > > ms_in_desc.baAssocJackID[] has entries written for "in_ports" values and
> > > > ms_out_desc.baAssocJackID[] has entries written for "out_ports" values.
> > > > But the counts and lengths are set the other way round in the
> > > > descriptors.
> > > > 
> > > > Fix the descriptors so that the bNumEmbMIDIJack values and the
> > > > descriptor lengths match the number of entries populated in the trailing
> > > > arrays.
> > > 
> > > Are you sure that it's a correct change?
> > > 
> > > IIUC, the in_ports and out_ports parameters are for external IN and
> > > OUT jacks, where an external OUT jack is connected to an embedded IN
> > > jack, and an external IN jack is connected to an embedded OUT jack.
> > 
> > I think it depends how the in_ports and out_ports values in configfs are
> > interpreted.  However, the case where in_ports != out_ports has been
> > broken since these files were added!
> > 
> > Without this change, setting in_ports=4 out_ports=2 we end up with:
> > 
> >       Endpoint Descriptor:
> >         [...]
> >         bEndpointAddress     0x01  EP 1 OUT
> >         [...]
> >         MIDIStreaming Endpoint Descriptor:
> >           bLength                 8
> >           bDescriptorType        37
> >           bDescriptorSubtype      1 (Invalid)
> >           bNumEmbMIDIJack         4
> >           baAssocJackID( 0)       9
> >           baAssocJackID( 1)      11
> >           baAssocJackID( 2)       9
> >           baAssocJackID( 3)       0
> >       Endpoint Descriptor:
> >         [...]
> >         bEndpointAddress     0x81  EP 1 IN
> >         [...]
> >         MIDIStreaming Endpoint Descriptor:
> >           bLength                 6
> >           bDescriptorType        37
> >           bDescriptorSubtype      1 (Invalid)
> >           bNumEmbMIDIJack         2
> >           baAssocJackID( 0)       2
> >           baAssocJackID( 1)       4
> > 
> > Note that baAssocJackID values 2 and 3 on the OUT endpoint are wrong.
> > 
> > From the same config, the jack definitions are:
> > 
> > 	1:  IN  External
> > 	2:  OUT Embedded, source 1
> > 	3:  IN  External
> > 	4:  OUT Embedded, source 3
> > 	5:  IN  External
> > 	6:  OUT Embedded, source 5
> > 	7:  IN  External
> > 	8:  OUT Embedded, source 7
> > 
> > 	9:  IN  Embedded
> > 	10: OUT External, source 9
> > 	11: IN  Embedded
> > 	12: OUT External, source 11
> > 
> > So it seems that the first 2 entries in each endpoint list are correct.
> > For the OUT endpoint, jacks 9 and 11 are embedded IN jacks and for the
> > IN endpoint, jacks 2 and 4 are embedded OUT jacks.
> > 
> > The problem is that the OUT endpoint lists two extra invalid jack IDs
> > and the IN endpoint should list jacks 6 and 8 but does not.
> > 
> > After applying this patch, the endpoint descriptors for the same config
> > are:
> > 
> >       Endpoint Descriptor:
> >         [...]
> >         bEndpointAddress     0x01  EP 1 OUT
> >         [...]
> >         MIDIStreaming Endpoint Descriptor:
> >           bLength                 6
> >           bDescriptorType        37
> >           bDescriptorSubtype      1 (Invalid)
> >           bNumEmbMIDIJack         2
> >           baAssocJackID( 0)       9
> >           baAssocJackID( 1)      11
> >       Endpoint Descriptor:
> >         [...]
> >         bEndpointAddress     0x81  EP 1 IN
> >         [...]
> >         MIDIStreaming Endpoint Descriptor:
> >           bLength                 8
> >           bDescriptorType        37
> >           bDescriptorSubtype      1 (Invalid)
> >           bNumEmbMIDIJack         4
> >           baAssocJackID( 0)       2
> >           baAssocJackID( 1)       4
> >           baAssocJackID( 2)       6
> >           baAssocJackID( 3)       8
> > 
> > Which lists all the jack IDs where they should be.
> 
> Hmm, I don't get your point.  The embedded IN is paired with the
> external OUT.  That's the intended behavior, no?

Yes, all the endpoint assignments are correct - when they appear in the
lists!

The issue is setting bNumEmbMIDIJack and bLength in the MIDIStreaming
Endpoint Descriptors.  Without this patch these are set the wrong way
round so either some ports do not appear or there are bogus entries
containing uninitialized stack memory.



Regards,
John

> > > > Cc: stable@vger.kernel.org
> > > > Fixes: c8933c3f79568 ("USB: gadget: f_midi: allow a dynamic number of input and output ports")
> > > > Signed-off-by: John Keeping <jkeeping@inmusicbrands.com>
> > > > ---
> > > >  drivers/usb/gadget/function/f_midi.c | 8 ++++----
> > > >  1 file changed, 4 insertions(+), 4 deletions(-)
> > > > 
> > > > diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c
> > > > index 837fcdfa3840f..6cc3d86cb4774 100644
> > > > --- a/drivers/usb/gadget/function/f_midi.c
> > > > +++ b/drivers/usb/gadget/function/f_midi.c
> > > > @@ -1000,11 +1000,11 @@ static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
> > > >  	}
> > > >  
> > > >  	/* configure the endpoint descriptors ... */
> > > > -	ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
> > > > -	ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
> > > > +	ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
> > > > +	ms_out_desc.bNumEmbMIDIJack = midi->out_ports;
> > > >  
> > > > -	ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
> > > > -	ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
> > > > +	ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
> > > > +	ms_in_desc.bNumEmbMIDIJack = midi->in_ports;
> > > >  
> > > >  	/* ... and add them to the list */
> > > >  	endpoint_descriptor_index = i;
> > > > -- 
> > > > 2.48.1
> > > > 
> > > > 

  reply	other threads:[~2025-01-30 10:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-29 16:05 [PATCH] usb: gadget: f_midi: fix MIDI Streaming descriptor lengths John Keeping
2025-01-29 16:40 ` Takashi Iwai
2025-01-29 17:31   ` John Keeping
2025-01-30 10:50     ` Takashi Iwai
2025-01-30 10:59       ` John Keeping [this message]
2025-01-30 12:17         ` Takashi Iwai
2025-01-30 17:10           ` John Keeping

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=Z5tbealYSvl7S72l-jkeeping@inmusicbrands.com \
    --to=jkeeping@inmusicbrands.com \
    --cc=abdul.rahim@myyahoo.com \
    --cc=balbi@ti.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=m.grzeschik@pengutronix.de \
    --cc=quic_jjohnson@quicinc.com \
    --cc=stable@vger.kernel.org \
    --cc=tiwai@suse.de \
    --cc=zonque@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox