[PATCH] usb: gadget: midi2: do not repeat a MIDI 1.0 message at a request boundary

From: Jaegyun Lee

Date: Sun Nov 29 2026 - 09:02:24 EST


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@xxxxxxxxxxxxxxx
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@xxxxxxxxx>
---
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;
}