git: d63b11caae6c - main - sound/midi: Replace MIDIQ with a simple byte queue

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Thu, 12 Nov 2026 16:38:07 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=d63b11caae6cc2615be31de5660cbc3e2320ddf5

commit d63b11caae6cc2615be31de5660cbc3e2320ddf5
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-10 13:36:26 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-12 16:35:48 +0000

    sound/midi: Replace MIDIQ with a simple byte queue
    
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
---
 sys/dev/sound/midi/midi.c  | 90 +++++++++++++++++++++++++++++++---------------
 sys/dev/sound/midi/midiq.h | 87 --------------------------------------------
 2 files changed, 62 insertions(+), 115 deletions(-)

diff --git a/sys/dev/sound/midi/midi.c b/sys/dev/sound/midi/midi.c
index 7364e5ba3e74..5b812af749e3 100644
--- a/sys/dev/sound/midi/midi.c
+++ b/sys/dev/sound/midi/midi.c
@@ -4,7 +4,7 @@
  * Copyright (c) 2003 Mathew Kanner
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
  * All rights reserved.
- * Copyright (c) 2025 The FreeBSD Foundation
+ * Copyright (c) 2025-2026 The FreeBSD Foundation
  *
  * Portions of this software were developed by Christos Margiolis
  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
@@ -52,12 +52,17 @@
 #endif
 
 #include <dev/sound/midi/midi.h>
-#include <dev/sound/midi/midiq.h>
 
 #include "mpu_if.h"
 
 MALLOC_DEFINE(M_MIDI, "midi buffers", "Midi data allocation area");
 
+struct midi_queue {
+	uint8_t	*buf;
+	int	size;
+	int	used;			/* Bytes queued. */
+};
+
 #define MIDI_NAMELEN   16
 struct snd_midi {
 	KOBJ_FIELDS;
@@ -68,7 +73,7 @@ struct snd_midi {
 	int	channel;
 
 	int	flags;			/* File flags */
-	MIDIQ_HEAD(, char) inq, outq;
+	struct midi_queue inq, outq;
 	int	rchan, wchan;
 	struct selinfo rsel, wsel;
 	int	hiwat;			/* QLEN(outq)>High-water -> disable
@@ -97,6 +102,33 @@ static struct cdevsw midi_cdevsw = {
 struct unrhdr *dev_unr = NULL;
 struct unrhdr *chn_unr = NULL;
 
+static int
+midi_queue_avail(struct midi_queue *q)
+{
+	return (q->size - q->used);
+}
+
+static void
+midi_queue_enq(struct midi_queue *q, uint8_t *src, int n)
+{
+	KASSERT(n >= 0 && n <= midi_queue_avail(q),
+	    ("%s: bad length %d", __func__, n));
+
+	memcpy(q->buf + q->used, src, n);
+	q->used += n;
+}
+
+static void
+midi_queue_deq(struct midi_queue *q, uint8_t *dst, int n)
+{
+	KASSERT(n >= 0 && n <= q->used,
+	    ("%s: bad length %d", __func__, n));
+
+	memcpy(dst, q->buf, n);
+	q->used -= n;
+	memmove(q->buf, q->buf + n, q->used);
+}
+
 /*
  * Register a new midi device.
  *
@@ -130,8 +162,10 @@ midi_init(kobj_class_t cls, void *cookie)
 
 	m->hiwat = outqsize / 2;
 
-	MIDIQ_INIT(m->inq, ibuf, inqsize);
-	MIDIQ_INIT(m->outq, obuf, outqsize);
+	m->inq.buf = ibuf;
+	m->inq.size = inqsize;
+	m->outq.buf = obuf;
+	m->outq.size = outqsize;
 
 	m->flags = 0;
 	m->unit = alloc_unr(dev_unr);
@@ -152,8 +186,8 @@ midi_init(kobj_class_t cls, void *cookie)
 err2:
 	mtx_destroy(&m->lock);
 
-	free(MIDIQ_BUF(m->inq), M_MIDI);
-	free(MIDIQ_BUF(m->outq), M_MIDI);
+	free(m->inq.buf, M_MIDI);
+	free(m->outq.buf, M_MIDI);
 err1:
 	free(m, M_MIDI);
 	return NULL;
@@ -176,8 +210,8 @@ midi_uninit(struct snd_midi *m)
 	destroy_dev(m->dev);
 	free_unr(dev_unr, m->unit);
 	free_unr(chn_unr, m->channel);
-	free(MIDIQ_BUF(m->inq), M_MIDI);
-	free(MIDIQ_BUF(m->outq), M_MIDI);
+	free(m->inq.buf, M_MIDI);
+	free(m->outq.buf, M_MIDI);
 	mtx_destroy(&m->lock);
 	free(m, M_MIDI);
 
@@ -205,9 +239,9 @@ midi_in(struct snd_midi *m, uint8_t *buf, int size)
 
 	used = 0;
 
-	if (MIDIQ_AVAIL(m->inq) > size) {
+	if (size <= midi_queue_avail(&m->inq)) {
 		used = size;
-		MIDIQ_ENQ(m->inq, buf, size);
+		midi_queue_enq(&m->inq, buf, size);
 	} else {
 		mtx_unlock(&m->lock);
 		return 0;
@@ -236,14 +270,14 @@ midi_out(struct snd_midi *m, uint8_t *buf, int size)
 		return (0);
 	}
 
-	used = min(size, MIDIQ_LEN(m->outq));
+	used = min(size, m->outq.used);
 	if (used)
-		MIDIQ_DEQ(m->outq, buf, used);
-	if (MIDIQ_EMPTY(m->outq)) {
+		midi_queue_deq(&m->outq, buf, used);
+	if (m->outq.used == 0) {
 		m->flags &= ~M_TXEN;
 		MPU_CALLBACK(m, m->cookie, m->flags);
 	}
-	if (used && MIDIQ_AVAIL(m->outq) > m->hiwat) {
+	if (used && midi_queue_avail(&m->outq) > m->hiwat) {
 		if (m->wchan) {
 			wakeup(&m->wchan);
 			m->wchan = 0;
@@ -268,7 +302,7 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	retval = 0;
 
 	if (flags & FREAD) {
-		if (MIDIQ_SIZE(m->inq) == 0)
+		if (m->inq.size == 0)
 			retval = ENXIO;
 		else if (m->flags & M_RX)
 			retval = EBUSY;
@@ -276,7 +310,7 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 			goto err;
 	}
 	if (flags & FWRITE) {
-		if (MIDIQ_SIZE(m->outq) == 0)
+		if (m->outq.size == 0)
 			retval = ENXIO;
 		else if (m->flags & M_TX)
 			retval = EBUSY;
@@ -293,7 +327,7 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	         * Only clear the inq, the outq might still have data to drain
 	         * from a previous session
 	         */
-		MIDIQ_CLEAR(m->inq);
+		m->inq.used = 0;
 	}
 
 	if (flags & FWRITE)
@@ -350,7 +384,7 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 	struct snd_midi *m = i_dev->si_drv1;
 	int retval;
 	int used;
-	char buf[MIDI_RSIZE];
+	uint8_t buf[MIDI_RSIZE];
 
 	retval = EIO;
 
@@ -363,7 +397,7 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 		goto err1;
 
 	while (uio->uio_resid > 0) {
-		while (MIDIQ_EMPTY(m->inq)) {
+		while (m->inq.used == 0) {
 			retval = EWOULDBLOCK;
 			if (ioflag & O_NONBLOCK)
 				goto err1;
@@ -387,10 +421,10 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 	         * At this point, it is certain that m->inq has data
 	         */
 
-		used = min(MIDIQ_LEN(m->inq), uio->uio_resid);
+		used = min(m->inq.used, uio->uio_resid);
 		used = min(used, MIDI_RSIZE);
 
-		MIDIQ_DEQ(m->inq, buf, used);
+		midi_queue_deq(&m->inq, buf, used);
 		mtx_unlock(&m->lock);
 		retval = uiomove(buf, used, uio);
 		if (retval)
@@ -419,7 +453,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 	struct snd_midi *m = i_dev->si_drv1;
 	int retval;
 	int used;
-	char buf[MIDI_WSIZE];
+	uint8_t buf[MIDI_WSIZE];
 
 	retval = EIO;
 	if (m == NULL)
@@ -431,7 +465,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 		goto err1;
 
 	while (uio->uio_resid > 0) {
-		while (MIDIQ_AVAIL(m->outq) == 0) {
+		while (midi_queue_avail(&m->outq) == 0) {
 			retval = EWOULDBLOCK;
 			if (ioflag & O_NONBLOCK)
 				goto err1;
@@ -456,7 +490,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 	         * We are certain than data can be placed on the queue
 	         */
 
-		used = min(MIDIQ_AVAIL(m->outq), uio->uio_resid);
+		used = min(midi_queue_avail(&m->outq), uio->uio_resid);
 		used = min(used, MIDI_WSIZE);
 
 		mtx_unlock(&m->lock);
@@ -464,7 +498,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 		if (retval)
 			goto err0;
 		mtx_lock(&m->lock);
-		MIDIQ_ENQ(m->outq, buf, used);
+		midi_queue_enq(&m->outq, buf, used);
 		/*
 	         * Inform the bottom half that data can be written
 	         */
@@ -503,13 +537,13 @@ midi_poll(struct cdev *i_dev, int events, struct thread *td)
 	mtx_lock(&m->lock);
 
 	if (events & (POLLIN | POLLRDNORM)) {
-		if (!MIDIQ_EMPTY(m->inq))
+		if (m->inq.used != 0)
 			revents |= events & (POLLIN | POLLRDNORM);
 		else
 			selrecord(td, &m->rsel);
 	}
 	if (events & (POLLOUT | POLLWRNORM)) {
-		if (MIDIQ_AVAIL(m->outq) < m->hiwat)
+		if (midi_queue_avail(&m->outq) < m->hiwat)
 			revents |= events & (POLLOUT | POLLWRNORM);
 		else
 			selrecord(td, &m->wsel);
diff --git a/sys/dev/sound/midi/midiq.h b/sys/dev/sound/midi/midiq.h
deleted file mode 100644
index 8ffa4a40d23d..000000000000
--- a/sys/dev/sound/midi/midiq.h
+++ /dev/null
@@ -1,87 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2003 Mathew Kanner
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef MIDIQ_H
-#define MIDIQ_H
-
-#define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
-
-#define MIDIQ_HEAD(name, type)          \
-struct name {                           \
-        int h, t, s;                    \
-        type * b;                        \
-}
-
-#define MIDIQ_INIT(head, buf, size) do {                \
-        (head).h=(head).t=0;                          \
-        (head).s=size;                                 \
-        (head).b=buf;                                  \
-} while (0)
-
-#define MIDIQ_EMPTY(head)       ((head).h == (head).t )
-
-#define MIDIQ_LENBASE(head)         ((head).h - (head).t < 0 ? \
-                                        (head).h - (head).t + (head).s : \
-                                        (head).h - (head).t)
-
-#define MIDIQ_FULL(head)        ((head).h == -1)
-#define MIDIQ_AVAIL(head)       (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head))
-#define MIDIQ_LEN(head)		((head).s - MIDIQ_AVAIL(head))
-/*
- * No protection against overflow, underflow
- */
-#define MIDIQ_ENQ(head, buf, size) do {                                                                 \
-                MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * min((size), (head).s - (head).h));                       \
-                if( (head).s - (head).h < (size) ) {                                                    \
-                        MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
-		} \
-                (head).h+=(size);                                                                         \
-                (head).h%=(head).s;                                                                     \
-		if(MIDIQ_EMPTY(head)) (head).h=-1; \
-} while (0)
-
-#define MIDIQ_DEQ_I(head, buf, size, move, update) do {                                                                 \
-		if(MIDIQ_FULL(head)) (head).h=(head).t; \
-                if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * min((size), (head).s - (head).t));                       \
-                if( (head).s - (head).t < (size) ) {                                                    \
-                        if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
-		} \
-		if (update) { \
-                (head).t+=(size);                                                                         \
-                (head).t%=(head).s;                                                                     \
-		} else { \
-		  if (MIDIQ_EMPTY(head)) (head).h=-1; \
-		} \
-} while (0)
-
-#define MIDIQ_SIZE(head) ((head).s)
-#define MIDIQ_CLEAR(head) ((head).h = (head).t = 0)
-#define MIDIQ_BUF(head) ((head).b)
-#define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1)
-
-#endif