All of lore.kernel.org
 help / color / mirror / Atom feed
* PATCH: ALSA USB MIDI: Fix case when USB MIDI interface has more than one extra endpoint descriptor
@ 2020-03-26 17:18 Andreas Steinmetz
  2020-03-29  8:45 ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Steinmetz @ 2020-03-26 17:18 UTC (permalink / raw)
  To: alsa-devel; +Cc: clemens

The Miditech MIDIFACE 16x16 (USB ID 1290:1749) has more than one extra
endpoint descriptor.

The first extra descriptor is: 0x06 0x30 0x00 0x00 0x00 0x00

As the code in snd_usbmidi_get_ms_info() looks only at the
first extra descriptor to find USB_DT_CS_ENDPOINT the device
as such is recognized but there is neither input nor output
configured.

The patch iterates through the extra descriptors to find the
proper one. With this patch the device is correctly configured.

Signed-off-by: Andreas Steinmetz <ast@domdv.de>

--- a/sourd/usb/midi.c	2020-03-26 17:34:40.073929497 +0100
+++ b/sound/usb/midi.c	2020-03-26 17:43:39.123252612 +0100
@@ -1839,7 +1839,8 @@
 	struct usb_host_endpoint *hostep;
 	struct usb_endpoint_descriptor *ep;
 	struct usb_ms_endpoint_descriptor *ms_ep;
-	int i, epidx;
+	int i, epidx, extralen;
+	unsigned char *extra;
 
 	intf = umidi->iface;
 	if (!intf)
@@ -1863,12 +1864,21 @@
 		ep = get_ep_desc(hostep);
 		if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
 			continue;
-		ms_ep = (struct usb_ms_endpoint_descriptor *)hostep->extra;
-		if (hostep->extralen < 4 ||
+		extralen = hostep->extralen;
+		extra = hostep->extra;
+repeat:		ms_ep = (struct usb_ms_endpoint_descriptor *)extra;
+		if (extralen < 1)
+			continue;
+		if (extralen < 4 ||
 		    ms_ep->bLength < 4 ||
 		    ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
-		    ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
-			continue;
+		    ms_ep->bDescriptorSubtype != UAC_MS_GENERAL) {
+			if (!extra[0])
+				continue;
+			extralen -= extra[0];
+			extra += extra[0];
+			goto repeat;
+		}
 		if (usb_endpoint_dir_out(ep)) {
 			if (endpoints[epidx].out_ep) {
 				if (++epidx >= MIDI_MAX_ENDPOINTS) {


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH: ALSA USB MIDI: Fix case when USB MIDI interface has more than one extra endpoint descriptor
  2020-03-26 17:18 PATCH: ALSA USB MIDI: Fix case when USB MIDI interface has more than one extra endpoint descriptor Andreas Steinmetz
@ 2020-03-29  8:45 ` Takashi Iwai
  2020-03-31 12:25   ` PATCH v2: " Andreas Steinmetz
  0 siblings, 1 reply; 4+ messages in thread
From: Takashi Iwai @ 2020-03-29  8:45 UTC (permalink / raw)
  To: Andreas Steinmetz; +Cc: alsa-devel, clemens

On Thu, 26 Mar 2020 18:18:55 +0100,
Andreas Steinmetz wrote:
> 
> The Miditech MIDIFACE 16x16 (USB ID 1290:1749) has more than one extra
> endpoint descriptor.
> 
> The first extra descriptor is: 0x06 0x30 0x00 0x00 0x00 0x00
> 
> As the code in snd_usbmidi_get_ms_info() looks only at the
> first extra descriptor to find USB_DT_CS_ENDPOINT the device
> as such is recognized but there is neither input nor output
> configured.
> 
> The patch iterates through the extra descriptors to find the
> proper one. With this patch the device is correctly configured.
> 
> Signed-off-by: Andreas Steinmetz <ast@domdv.de>
> 
> --- a/sourd/usb/midi.c	2020-03-26 17:34:40.073929497 +0100
> +++ b/sound/usb/midi.c	2020-03-26 17:43:39.123252612 +0100
> @@ -1839,7 +1839,8 @@
>  	struct usb_host_endpoint *hostep;
>  	struct usb_endpoint_descriptor *ep;
>  	struct usb_ms_endpoint_descriptor *ms_ep;
> -	int i, epidx;
> +	int i, epidx, extralen;
> +	unsigned char *extra;
>  
>  	intf = umidi->iface;
>  	if (!intf)
> @@ -1863,12 +1864,21 @@
>  		ep = get_ep_desc(hostep);
>  		if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
>  			continue;
> -		ms_ep = (struct usb_ms_endpoint_descriptor *)hostep->extra;
> -		if (hostep->extralen < 4 ||
> +		extralen = hostep->extralen;
> +		extra = hostep->extra;
> +repeat:		ms_ep = (struct usb_ms_endpoint_descriptor *)extra;
> +		if (extralen < 1)
> +			continue;
> +		if (extralen < 4 ||
>  		    ms_ep->bLength < 4 ||
>  		    ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
> -		    ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
> -			continue;
> +		    ms_ep->bDescriptorSubtype != UAC_MS_GENERAL) {
> +			if (!extra[0])
> +				continue;
> +			extralen -= extra[0];
> +			extra += extra[0];
> +			goto repeat;
> +		}

This kind of goto-loop in a loop is really cryptic and error-prone.
In general we use goto in the kernel code but only for limited
situations such as the unified error code path or the restart of the
whole loop after an error condition.  But this doesn't fit for such
exceptions and can be better rewritten with a simple loop (maybe even
better to factor out to a function).

So, could you revise the change to be more readable?


thanks,

Takashi

^ permalink raw reply	[flat|nested] 4+ messages in thread

* PATCH v2: ALSA USB MIDI: Fix case when USB MIDI interface has more than one extra endpoint descriptor
  2020-03-29  8:45 ` Takashi Iwai
@ 2020-03-31 12:25   ` Andreas Steinmetz
  2020-03-31 12:35     ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Steinmetz @ 2020-03-31 12:25 UTC (permalink / raw)
  To: Takashi Iwai; +Cc: alsa-devel, clemens

[v2: Patch reworked as per comment from Takashi Iwai.]

The Miditech MIDIFACE 16x16 (USB ID 1290:1749) has more than one extra
endpoint descriptor.

The first extra descriptor is: 0x06 0x30 0x00 0x00 0x00 0x00

As the code in snd_usbmidi_get_ms_info() looks only at the
first extra descriptor to find USB_DT_CS_ENDPOINT the device
as such is recognized but there is neither input nor output
configured.

The patch iterates through the extra descriptors to find the
proper one. With this patch the device is correctly configured.

Signed-off-by: Andreas Steinmetz <ast@domdv.de>

--- a/sound/usb/midi.c	2020-03-29 17:04:37.459599241 +0200
+++ b/sound/usb/midi.c	2020-03-29 17:16:23.975423372 +0200
@@ -1826,6 +1826,28 @@
 	return 0;
 }
 
+static struct usb_ms_endpoint_descriptor *find_usb_ms_endpoint_descriptor(
+					struct usb_host_endpoint *hostep)
+{
+	unsigned char *extra = hostep->extra;
+	int extralen = hostep->extralen;
+
+	while (extralen > 3) {
+		struct usb_ms_endpoint_descriptor *ms_ep =
+				(struct usb_ms_endpoint_descriptor *)extra;
+
+		if (ms_ep->bLength > 3 &&
+		    ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT &&
+		    ms_ep->bDescriptorSubtype == UAC_MS_GENERAL)
+			return ms_ep;
+		if (!extra[0])
+			break;
+		extralen -= extra[0];
+		extra += extra[0];
+	}
+	return NULL;
+}
+
 /*
  * Returns MIDIStreaming device capabilities.
  */
@@ -1863,11 +1885,8 @@
 		ep = get_ep_desc(hostep);
 		if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
 			continue;
-		ms_ep = (struct usb_ms_endpoint_descriptor *)hostep->extra;
-		if (hostep->extralen < 4 ||
-		    ms_ep->bLength < 4 ||
-		    ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
-		    ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
+		ms_ep = find_usb_ms_endpoint_descriptor(hostep);
+		if (!ms_ep)
 			continue;
 		if (usb_endpoint_dir_out(ep)) {
 			if (endpoints[epidx].out_ep) {


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH v2: ALSA USB MIDI: Fix case when USB MIDI interface has more than one extra endpoint descriptor
  2020-03-31 12:25   ` PATCH v2: " Andreas Steinmetz
@ 2020-03-31 12:35     ` Takashi Iwai
  0 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2020-03-31 12:35 UTC (permalink / raw)
  To: Andreas Steinmetz; +Cc: alsa-devel, clemens

On Tue, 31 Mar 2020 14:25:54 +0200,
Andreas Steinmetz wrote:
> 
> [v2: Patch reworked as per comment from Takashi Iwai.]
> 
> The Miditech MIDIFACE 16x16 (USB ID 1290:1749) has more than one extra
> endpoint descriptor.
> 
> The first extra descriptor is: 0x06 0x30 0x00 0x00 0x00 0x00
> 
> As the code in snd_usbmidi_get_ms_info() looks only at the
> first extra descriptor to find USB_DT_CS_ENDPOINT the device
> as such is recognized but there is neither input nor output
> configured.
> 
> The patch iterates through the extra descriptors to find the
> proper one. With this patch the device is correctly configured.
> 
> Signed-off-by: Andreas Steinmetz <ast@domdv.de>

Applied now.  Thanks.


Takashi

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-03-31 12:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-26 17:18 PATCH: ALSA USB MIDI: Fix case when USB MIDI interface has more than one extra endpoint descriptor Andreas Steinmetz
2020-03-29  8:45 ` Takashi Iwai
2020-03-31 12:25   ` PATCH v2: " Andreas Steinmetz
2020-03-31 12:35     ` Takashi Iwai

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.