From 2fca03e792ef1b740e8a7370fdd360d0b627c84c Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Mon, 6 Nov 2023 16:27:11 +0100
Subject: [PATCH] seq: Clear UMP event flag for legacy apps

It seems that some applications (at least Chrome WebMIDI) set random
bits to the flags of event packet, and this confuses as if they were
UMP-events, which are eventually filtered out.

Although it's a bug of applications, it's better to avoid the
regressions.  So this patch forcibly clears the UMP flag of the
incoming and outgoing events when the application is running in the
legacy mode (i.e. midi_version = 0).

Fixes: 2aefb5c41cc0 ("seq: Add UMP support")
Closes: https://github.com/alsa-project/alsa-lib/issues/360
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 src/seq/seq.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/seq/seq.c b/src/seq/seq.c
index 5ec737a7004f..643cf159f3ef 100644
--- a/src/seq/seq.c
+++ b/src/seq/seq.c
@@ -4161,6 +4161,13 @@ int snd_seq_event_output(snd_seq_t *seq, snd_seq_event_t *ev)
 	return result;
 }
 
+/* workaround for broken legacy apps that set UMP event bit unexpectedly */
+static void clear_ump_for_legacy_apps(snd_seq_t *seq, snd_seq_event_t *ev)
+{
+	if (!seq->midi_version && snd_seq_ev_is_ump(ev))
+		ev->flags &= ~SNDRV_SEQ_EVENT_UMP;
+}
+
 /**
  * \brief output an event onto the lib buffer without draining buffer
  * \param seq sequencer handle
@@ -4178,6 +4185,7 @@ int snd_seq_event_output_buffer(snd_seq_t *seq, snd_seq_event_t *ev)
 {
 	int len;
 	assert(seq && ev);
+	clear_ump_for_legacy_apps(seq, ev);
 	len = snd_seq_event_length(ev);
 	if (len < 0)
 		return -EINVAL;
@@ -4238,6 +4246,7 @@ int snd_seq_event_output_direct(snd_seq_t *seq, snd_seq_event_t *ev)
 	ssize_t len;
 	void *buf;
 
+	clear_ump_for_legacy_apps(seq, ev);
 	len = snd_seq_event_length(ev);
 	if (len < 0)
 		return len;
@@ -4374,6 +4383,7 @@ static int snd_seq_event_retrieve_buffer(snd_seq_t *seq, snd_seq_event_t **retp)
 	snd_seq_event_t *ev;
 
 	*retp = ev = (snd_seq_event_t *)(seq->ibuf + seq->ibufptr * packet_size);
+	clear_ump_for_legacy_apps(seq, ev);
 	seq->ibufptr++;
 	seq->ibuflen--;
 	if (! snd_seq_ev_is_variable(ev))
-- 
2.35.3

