mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jaegyun Lee <dreams200260@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Neill Kapron <nkapron@google.com>
Cc: Jaegyun Lee <dreams200260@gmail.com>,
	Takashi Iwai <tiwai@suse.de>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] usb: gadget: midi2: do not repeat a MIDI 1.0 message at a request boundary
Date: Sun, 29 Nov 2026 23:02:09 +0900	[thread overview]
Message-ID: <20261129140209.547953-1-dreams200260@gmail.com> (raw)

In MIDI 1.0 mode (altset 0) process_midi1_transmit() converts UMP data
from user space into USB-MIDI 1.0 packets through a per-cable byte
buffer (midi2->midi1_port[cable].buf/pending). process_midi1_byte()
returns true when the packet it just wrote filled the USB request
(req_buf_size - length <= 4) and the request was queued:

	memcpy(req->buf + req->length, p, sizeof(p));
	req->length += sizeof(p);
	...
	if (midi2->info.req_buf_size - req->length <= 4) {
		queue_request_ep_raw(req);
		*req_p = NULL;
		return true;
	}

and process_midi1_pending_buf() then keeps byte c at the head of the
buffer:

	if (process_midi1_byte(midi2, cable, port->buf[c], req_p)) {
		port->pending -= c;
		if (port->pending)
			memmove(port->buf, port->buf + c, port->pending);
		return true;
	}

Byte c has already been turned into a packet, but it is kept at the
head of the buffer and the running-status state is not advanced, so on
the next pass it is converted again with the same state: the last MIDI
1.0 message of every full request (127 packets = 508 bytes) is sent
twice.

Reproduced by having the gadget write 1000 distinct MIDI 1.0 note-on
UMPs to /dev/snd/umpC<card>D0 while the host reads the MIDI 1.0 IN
endpoint:

host /dev/bus/usb/001/002, gadget /dev/snd/umpC3D0, MIDI 1.0 IN ep 0x81, sending 1000 note-ons
DUPLICATE note-on #126 (note 126 vel 1) = packet 127, byte offset 508 of the stream
DUPLICATE note-on #252 (note 124 vel 2) = packet 254, byte offset 1016 of the stream
DUPLICATE note-on #382 (note 126 vel 3) = packet 385, byte offset 1540 of the stream
DUPLICATE note-on #508 (note 124 vel 4) = packet 512, byte offset 2048 of the stream
DUPLICATE note-on #638 (note 126 vel 5) = packet 643, byte offset 2572 of the stream
DUPLICATE note-on #764 (note 124 vel 6) = packet 770, byte offset 3080 of the stream
DUPLICATE note-on #894 (note 126 vel 7) = packet 901, byte offset 3604 of the stream
RESULT sent=1000 received_packets=1007 note_on=1007 in_order=1000 duplicates=7 other=0 transfers=11

Advance the state and drop byte c too when the request is full.

Fixes: d6468be779af ("usb: gadget: midi2: MIDI 1.0 interface (altset 0) support")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
 drivers/usb/gadget/function/f_midi2.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/function/f_midi2.c
--- a/drivers/usb/gadget/function/f_midi2.c
+++ b/drivers/usb/gadget/function/f_midi2.c
@@ -925,6 +925,7 @@ static bool process_midi1_byte(struct f_
 		}
 
 		if (midi2->info.req_buf_size - req->length <= 4) {
+			port->state = next_state;
 			queue_request_ep_raw(req);
 			*req_p = NULL;
 			return true;
@@ -952,9 +953,9 @@ static bool process_midi1_pending_buf(st
 		for (c = 0; c < port->pending; c++) {
 			if (process_midi1_byte(midi2, cable, port->buf[c],
 					       req_p)) {
-				port->pending -= c;
+				port->pending -= c + 1;
 				if (port->pending)
-					memmove(port->buf, port->buf + c,
+					memmove(port->buf, port->buf + c + 1,
 						port->pending);
 				return true;
 			}

                 reply	other threads:[~2026-11-29 14:02 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=20261129140209.547953-1-dreams200260@gmail.com \
    --to=dreams200260@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=stable@vger.kernel.org \
    --cc=tiwai@suse.de \
    /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

all inboxes | Powered by JetHome®