Skip to contentSkip to conversation

ALSA: Fixes for optional MIDI 1.0 port in MIDI 2.0

PATCH New Takashi Iwai 1 participant 7 messages Aug 7, 2024 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: Fixes for optional MIDI 1.0 port in MIDI 2.0

Hi,

below is a series of small fixes for handling the optional MIDI 1.0 ports that are allowed in MIDI 2.0 spec. It's rather for a rare case such as no UMP FB Info is given but only GTB is available, so most user won't notice, hopefully.

Takashi

===

Takashi Iwai (6):
ALSA: ump: Choose the protocol when protocol caps are changed
ALSA: usb-audio: Accept multiple protocols in GTBs
ALSA: usb-audio: Set MIDI1 flag appropriately for GTB MIDI 1.0 entry
ALSA: usb-audio: Update UMP group attributes for GTB blocks, too
ALSA: seq: Print MIDI 1.0 specific port in proc output
ALSA: seq: Fix missing seq port info bit return for MIDI 1.0 block

 include/sound/ump.h            |  1 +
 sound/core/seq/seq_clientmgr.c |  7 ++++++-
 sound/core/seq/seq_ports.c     |  3 +++
 sound/core/ump.c               | 31 +++++++++++++++++++----------
 sound/usb/midi2.c              | 36 ++++++++++++++++++++--------------
 5 files changed, 52 insertions(+), 26 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/6 ALSA: ump: Choose the protocol when protocol caps are changed

When the protocol capability bits are changed via Endpoint Info update notification, we should check the validity of the current protocol and reset it if needed, too.

---
 sound/core/ump.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
sound/core/ump.c +16 -6
index c7c3581bbbbc..4502de891adf 100644
@@ -657,6 +657,17 @@ static int ump_append_string(struct snd_ump_endpoint *ump, char *dest,
 		format == UMP_STREAM_MSG_FORMAT_END);
 }
 
+/* Choose the default protocol */
+static void choose_default_protocol(struct snd_ump_endpoint *ump)
+{
+	if (ump->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK)
+		return;
+	if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2)
+		ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI2;
+	else
+		ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI1;
+}
+
 /* handle EP info stream message; update the UMP attributes */
 static int ump_handle_ep_info_msg(struct snd_ump_endpoint *ump,
 				  const union snd_ump_stream_msg *buf)
@@ -678,6 +689,10 @@ static int ump_handle_ep_info_msg(struct snd_ump_endpoint *ump,
 
 	ump_dbg(ump, "EP info: version=%x, num_blocks=%x, proto_caps=%x\n",
 		ump->info.version, ump->info.num_blocks, ump->info.protocol_caps);
+
+	ump->info.protocol &= ump->info.protocol_caps;
+	choose_default_protocol(ump);
+
 	return 1; /* finished */
 }
 
@@ -1040,12 +1055,7 @@ int snd_ump_parse_endpoint(struct snd_ump_endpoint *ump)
 		ump_dbg(ump, "Unable to get UMP EP stream config\n");
 
 	/* If no protocol is set by some reason, assume the valid one */
-	if (!(ump->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK)) {
-		if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2)
-			ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI2;
-		else if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI1)
-			ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI1;
-	}
+	choose_default_protocol(ump);
 
 	/* Query and create blocks from Function Blocks */
 	for (blk = 0; blk < ump->info.num_blocks; blk++) {
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/6 ALSA: usb-audio: Accept multiple protocols in GTBs

In reply to Takashi Iwai

It's valid to give different protocols via multiple GTBs; e.g. a MIDI 1.0 port is embedded in a MIDI 2.0 device that talks with MIDI 2.0 protocol. However, the current driver implementation assumes only a single protocol over the whole Endpoint, and it can't handle such a scenario.

This patch changes the driver's behavior to parse GTBs to accept multiple protocols. Instead of switching to the last given protocol, it adds the protocol capability bits now. Meanwhile, the default protocol is chosen by the first given protocol in GTBs.

Practically seen, this should be a minor issue, as new devices should specify the protocols properly via UMP Endpoint Info messages, so this is rather just covering a corner case.

---
 sound/usb/midi2.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
sound/usb/midi2.c +3 -13
index 820d3e4b672a..fa655aa4a56f 100644
@@ -607,12 +607,8 @@ static int parse_group_terminal_block(struct snd_usb_midi2_ump *rmidi,
 		return 0;
 	}
 
-	if (ump->info.protocol && ump->info.protocol != protocol)
-		usb_audio_info(rmidi->umidi->chip,
-			       "Overriding preferred MIDI protocol in GTB %d: %x -> %x\n",
-			       rmidi->usb_block_id, ump->info.protocol,
-			       protocol);
-	ump->info.protocol = protocol;
+	if (!ump->info.protocol)
+		ump->info.protocol = protocol;
 
 	protocol_caps = protocol;
 	switch (desc->bMIDIProtocol) {
@@ -624,13 +620,7 @@ static int parse_group_terminal_block(struct snd_usb_midi2_ump *rmidi,
 		break;
 	}
 
-	if (ump->info.protocol_caps && ump->info.protocol_caps != protocol_caps)
-		usb_audio_info(rmidi->umidi->chip,
-			       "Overriding MIDI protocol caps in GTB %d: %x -> %x\n",
-			       rmidi->usb_block_id, ump->info.protocol_caps,
-			       protocol_caps);
-	ump->info.protocol_caps = protocol_caps;
-
+	ump->info.protocol_caps |= protocol_caps;
 	return 0;
 }
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/6 ALSA: usb-audio: Set MIDI1 flag appropriately for GTB MIDI 1.0 entry

In reply to Takashi Iwai

When a MIDI 1.0 protocol is specified in a GTB entry while others are set in MIDI 2.0, it should be seen as a legacy MIDI 1.0 port. Since recently we allow drivers to set a flag SNDRV_UMP_BLOCK_IS_MIDI1 to a FB for that purpose. This patch tries to set that flag when the device shows such a configuration.

---
 sound/usb/midi2.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
sound/usb/midi2.c +16 -2
index fa655aa4a56f..4fb43d9743d7 100644
@@ -863,9 +863,23 @@ static int create_gtb_block(struct snd_usb_midi2_ump *rmidi, int dir, int blk)
 		fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1 |
 			SNDRV_UMP_BLOCK_IS_LOWSPEED;
 
+	/* if MIDI 2.0 protocol is supported and yet the GTB shows MIDI 1.0,
+	 * treat it as a MIDI 1.0-specific block
+	 */
+	if (rmidi->ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2) {
+		switch (desc->bMIDIProtocol) {
+		case USB_MS_MIDI_PROTO_1_0_64:
+		case USB_MS_MIDI_PROTO_1_0_64_JRTS:
+		case USB_MS_MIDI_PROTO_1_0_128:
+		case USB_MS_MIDI_PROTO_1_0_128_JRTS:
+			fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1;
+			break;
+		}
+	}
+
 	usb_audio_dbg(umidi->chip,
-		      "Created a UMP block %d from GTB, name=%s\n",
-		      blk, fb->info.name);
+		      "Created a UMP block %d from GTB, name=%s, flags=0x%x\n",
+		      blk, fb->info.name, fb->info.flags);
 	return 0;
 }
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/6 ALSA: usb-audio: Update UMP group attributes for GTB blocks, too

In reply to Takashi Iwai

When a FB is created from a GTB instead of UMP FB Info inquiry, we missed the update of the corresponding UMP Group attributes. Export the call of updater and let it be called from the USB driver.

Fixes 0642a3c5cacc ("ALSA: ump: Update substream name from assigned FB names")
---
 include/sound/ump.h | 1 +
 sound/core/ump.c    | 9 +++++----
 sound/usb/midi2.c   | 2 ++
 3 files changed, 8 insertions(+), 4 deletions(-)
include/sound/ump.h +1 -0
index 7484f62fb234..532c2c3ea28e 100644
@@ -123,6 +123,7 @@ static inline int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
 
 int snd_ump_receive_ump_val(struct snd_ump_endpoint *ump, u32 val);
 int snd_ump_switch_protocol(struct snd_ump_endpoint *ump, unsigned int protocol);
+void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump);
 
 /*
  * Some definitions for UMP
sound/core/ump.c +5 -4
index 4502de891adf..243ecdbb2a6e 100644
@@ -525,7 +525,7 @@ static void snd_ump_proc_read(struct snd_info_entry *entry,
 }
 
 /* update dir_bits and active flag for all groups in the client */
-static void update_group_attrs(struct snd_ump_endpoint *ump)
+void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump)
 {
 	struct snd_ump_block *fb;
 	struct snd_ump_group *group;
@@ -578,6 +578,7 @@ static void update_group_attrs(struct snd_ump_endpoint *ump)
 		}
 	}
 }
+EXPORT_SYMBOL_GPL(snd_ump_update_group_attrs);
 
 /*
  * UMP endpoint and function block handling
@@ -863,7 +864,7 @@ static int ump_handle_fb_info_msg(struct snd_ump_endpoint *ump,
 	if (fb) {
 		fill_fb_info(ump, &fb->info, buf);
 		if (ump->parsed) {
-			update_group_attrs(ump);
+			snd_ump_update_group_attrs(ump);
 			seq_notify_fb_change(ump, fb);
 		}
 	}
@@ -895,7 +896,7 @@ static int ump_handle_fb_name_msg(struct snd_ump_endpoint *ump,
 				buf->raw, 3);
 	/* notify the FB name update to sequencer, too */
 	if (ret > 0 && ump->parsed) {
-		update_group_attrs(ump);
+		snd_ump_update_group_attrs(ump);
 		seq_notify_fb_change(ump, fb);
 	}
 	return ret;
@@ -1065,7 +1066,7 @@ int snd_ump_parse_endpoint(struct snd_ump_endpoint *ump)
 	}
 
 	/* initialize group attributions */
-	update_group_attrs(ump);
+	snd_ump_update_group_attrs(ump);
 
  error:
 	ump->parsed = true;
sound/usb/midi2.c +2 -0
index 4fb43d9743d7..692dfc3c182f 100644
@@ -877,6 +877,8 @@ static int create_gtb_block(struct snd_usb_midi2_ump *rmidi, int dir, int blk)
 		}
 	}
 
+	snd_ump_update_group_attrs(rmidi->ump);
+
 	usb_audio_dbg(umidi->chip,
 		      "Created a UMP block %d from GTB, name=%s, flags=0x%x\n",
 		      blk, fb->info.name, fb->info.flags);
Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/6 ALSA: seq: Print MIDI 1.0 specific port in proc output

In reply to Takashi Iwai

When a sequencer port assigned to a UMP Group that is specific to MIDI 1.0 among MIDI 2.0 client, mark it explicitly in the proc output, so that user can see it easily. This is an exceptional case where the message isn't converted to MIDI 1.0 even if the client is running in MIDI 2.0 mode.

---
 sound/core/seq/seq_clientmgr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
sound/core/seq/seq_clientmgr.c +6 -1
index 8c4ee5066afe..276b5b3f68dc 100644
@@ -2633,13 +2633,18 @@ static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
 	list_for_each_entry(p, &client->ports_list_head, list) {
 		if (p->capability & SNDRV_SEQ_PORT_CAP_INACTIVE)
 			continue;
-		snd_iprintf(buffer, "  Port %3d : \"%s\" (%c%c%c%c) [%s]\n",
+		snd_iprintf(buffer, "  Port %3d : \"%s\" (%c%c%c%c) [%s]",
 			    p->addr.port, p->name,
 			    FLAG_PERM_RD(p->capability),
 			    FLAG_PERM_WR(p->capability),
 			    FLAG_PERM_EX(p->capability),
 			    FLAG_PERM_DUPLEX(p->capability),
 			    port_direction_name(p->direction));
+#if IS_ENABLED(CONFIG_SND_SEQ_UMP)
+		if (snd_seq_client_is_midi2(client) && p->is_midi1)
+			snd_iprintf(buffer, " [MIDI1]");
+#endif
+		snd_iprintf(buffer, "\n");
 		snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, "    Connecting To: ");
 		snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, "    Connected From: ");
 	}
Signature · 1 line
2.43.0
#6 lore raw

PATCH 6/6 ALSA: seq: Fix missing seq port info bit return for MIDI 1.0 block

In reply to Takashi Iwai

The recent extension added a new ALSA sequencer port info flag bit SNDRV_SEQ_PORT_FLG_IS_MIDI1, but it's not reported back when inquired. Fix it to report properly.

Fixes 0079c9d1e58a ("ALSA: ump: Handle MIDI 1.0 Function Block in MIDI 2.0 protocol")
---
 sound/core/seq/seq_ports.c | 3 +++
 1 file changed, 3 insertions(+)
sound/core/seq/seq_ports.c +3 -0
index 535290f24eed..cc2f8e846584 100644
@@ -401,6 +401,9 @@ int snd_seq_get_port_info(struct snd_seq_client_port * port,
 		info->time_queue = port->time_queue;
 	}
 
+	if (port->is_midi1)
+		info->flags |= SNDRV_SEQ_PORT_FLG_IS_MIDI1;
+
 	/* UMP direction and group */
 	info->direction = port->direction;
 	info->ump_group = port->ump_group;
Signature · 1 line
2.43.0