Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: seq: Improve UMP SysEx packing
@ 2024-06-26 16:10 Takashi Iwai
  2024-06-26 16:52 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Takashi Iwai @ 2024-06-26 16:10 UTC (permalink / raw)
  To: linux-sound

The initial implementation of 7bit SysEx message conversion to UMP did
a trick to deal with the first SysEx start byte (0xf7).  The converter
trims it and sends the first packet with the content of 5 bytes.
Although this is valid and should work, it's not ideal and a bit
confusing as if something goes wrong.

This patch improves the handling to try filling always 6 bytes in the
package.

Fixes: e9e02819a98a ("ALSA: seq: Automatic conversion of UMP events")
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/core/seq/seq_ump_convert.c | 48 +++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/sound/core/seq/seq_ump_convert.c b/sound/core/seq/seq_ump_convert.c
index e90b27a135e6..952e64c499dd 100644
--- a/sound/core/seq/seq_ump_convert.c
+++ b/sound/core/seq/seq_ump_convert.c
@@ -1192,7 +1192,7 @@ static int cvt_sysex_to_ump(struct snd_seq_client *dest,
 {
 	struct snd_seq_ump_event ev_cvt;
 	unsigned char status;
-	u8 buf[6], *xbuf;
+	u8 buf[6];
 	int offset = 0;
 	int len, err;
 
@@ -1200,36 +1200,44 @@ static int cvt_sysex_to_ump(struct snd_seq_client *dest,
 		return 0;
 
 	setup_ump_event(&ev_cvt, event);
+	len = 0;
+	status = UMP_SYSEX_STATUS_START;
 	for (;;) {
-		len = snd_seq_expand_var_event_at(event, sizeof(buf), buf, offset);
-		if (len <= 0)
+		err = snd_seq_expand_var_event_at(event, sizeof(buf) - len,
+						  buf + len, offset);
+		if (err < 0)
+			break;
+		len += err;
+		if (!len)
 			break;
 		if (WARN_ON(len > 6))
 			break;
-		offset += len;
-		xbuf = buf;
-		if (*xbuf == UMP_MIDI1_MSG_SYSEX_START) {
-			status = UMP_SYSEX_STATUS_START;
-			xbuf++;
+		offset += err;
+		if (status == UMP_SYSEX_STATUS_START &&
+		    buf[0] == UMP_MIDI1_MSG_SYSEX_START) {
 			len--;
-			if (len > 0 && xbuf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) {
-				status = UMP_SYSEX_STATUS_SINGLE;
-				len--;
-			}
-		} else {
-			if (xbuf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) {
-				status = UMP_SYSEX_STATUS_END;
-				len--;
-			} else {
-				status = UMP_SYSEX_STATUS_CONTINUE;
-			}
+			memmove(buf, buf + 1, len);
+			continue; /* fill one more byte */
 		}
-		fill_sysex7_ump(dest_port, ev_cvt.ump, status, xbuf, len);
+		if (buf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) {
+			len--;
+			if (status == UMP_SYSEX_STATUS_START)
+				status = UMP_SYSEX_STATUS_SINGLE;
+			else
+				status = UMP_SYSEX_STATUS_END;
+		}
+		fill_sysex7_ump(dest_port, ev_cvt.ump, status, buf, len);
 		err = __snd_seq_deliver_single_event(dest, dest_port,
 						     (struct snd_seq_event *)&ev_cvt,
 						     atomic, hop);
 		if (err < 0)
 			return err;
+
+		if (status == UMP_SYSEX_STATUS_START)
+			status = UMP_SYSEX_STATUS_CONTINUE;
+		else if (status != UMP_SYSEX_STATUS_CONTINUE)
+			break;
+		len = 0; /* reset filling */
 	}
 	return 0;
 }
-- 
2.43.0


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

end of thread, other threads:[~2024-06-26 16:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-26 16:10 [PATCH] ALSA: seq: Improve UMP SysEx packing Takashi Iwai
2024-06-26 16:52 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox