Linux bluetooth development
 help / color / mirror / Atom feed
From: Alessandro Parilli <nauta@google.com>
To: linux-bluetooth@vger.kernel.org
Cc: Alessandro Parilli <nauta@google.com>
Subject: [PATCH BlueZ v2] midi: Fix stall when timestampHigh is zero
Date: Thu,  3 Dec 2026 16:42:24 +0000	[thread overview]
Message-ID: <20261203164225.3278460-1-nauta@google.com> (raw)

append_timestamp_high_maybe() busy-waits until the high 6 bits of the
13-bit millisecond timestamp become non-zero. This was added by commit
c29241fe129f ("midi: Fix random empty timestamp_high value") to avoid
sending a header byte of 0x80, at the time the parser in this file
treated a timestampHigh of 0 as "header not read yet". Commit
220f102c8db9 ("profiles/midi: Fix zero timestampHigh") fixed the parser,
and the BLE MIDI specification allows a timestampHigh of 0 (the header
is 0b10hhhhhh), so the workaround is no longer needed.

It is also costly: the high bits are 0 for the first 128 ms of every
8.192 s timestamp period, so any packet started in that window makes
bluetoothd spin in its main loop for up to 128 ms, sending and reading
nothing.

With MIDI clock (24 ppqn) sent to a synth over BLE this shows up every
8.192 s: a btmon capture at 100 BPM (one clock message every 25 ms)
shows the outgoing ATT writes stopping for 135-152 ms each period,
followed by a single packet carrying the 3 clock messages that queued
up. Clock-following devices drift behind each time.

Write the header from the current time once instead.

Fixes: c29241fe129f ("midi: Fix random empty timestamp_high value")
---
v2: explain why the loop was added (c29241fe129f) and why it is no
    longer needed since 220f102c8db9; add a Fixes: tag. No code change.

 profiles/midi/libmidi.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/profiles/midi/libmidi.c b/profiles/midi/libmidi.c
index 349f5f7..d90f522 100644
--- a/profiles/midi/libmidi.c
+++ b/profiles/midi/libmidi.c
@@ -58,20 +58,18 @@ inline static uint8_t sysex_get(const snd_seq_event_t *ev, size_t i)
 
 inline static void append_timestamp_high_maybe(struct midi_write_parser *parser)
 {
-	uint8_t timestamp_high = 0x80;
-
 	if (midi_write_has_data(parser))
 		return;
 
-	/* Make sure timesampt_high is assigned a non-zero value */
-	do {
-		/* convert µs to ms */
-		parser->rtime = g_get_monotonic_time() / 1000;
-		timestamp_high |= (parser->rtime & 0x1F80) >> 7;
-	} while (timestamp_high == 0x80);
+	/* convert µs to ms */
+	parser->rtime = g_get_monotonic_time() / 1000;
 
-	/* set timestampHigh */
-	buffer_append_byte(&parser->midi_stream, timestamp_high);
+	/*
+	 * set timestampHigh: the header byte is 10hhhhhh and a timestampHigh
+	 * of 0 (header 0x80) is valid.
+	 */
+	buffer_append_byte(&parser->midi_stream,
+				0x80 | ((parser->rtime & 0x1F80) >> 7));
 }
 
 inline static void append_timestamp_low(struct midi_write_parser *parser)
-- 
2.56.1.911.g883d8bcb20-goog


                 reply	other threads:[~2026-12-03 16:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261203164225.3278460-1-nauta@google.com \
    --to=nauta@google.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /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