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

From: Takashi Iwai

Date: Wed Dec 02 2026 - 03:45:01 EST


On Sun, 29 Nov 2026 15:02:09 +0100,
Jaegyun Lee wrote:
>
> 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>

Reviewed-by: Takashi Iwai <tiwai@xxxxxxx>


thanks,

Takashi