git: ff6b61a2c886 - stable/14 - sound: Handle midistat_lock through function interface

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Wed, 10 Jul 2024 16:49:09 UTC
The branch stable/14 has been updated by christos:

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

commit ff6b61a2c88631e66287c478cd373ed39878b174
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2024-07-06 18:22:59 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2024-07-10 16:48:13 +0000

    sound: Handle midistat_lock through function interface
    
    midistat_lock is used outside midi/midi.c as well, so implement lock,
    unlock and lockassert functions in order not to expose the lock in the
    header file.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      2 days
    Reviewed by:    emaste
    Differential Revision:  https://reviews.freebsd.org/D45857
    
    (cherry picked from commit 2d6fc24673ccc97020c94094f97ee015f1db9702)
---
 sys/dev/sound/midi/midi.c      | 76 ++++++++++++++++++++++++++----------------
 sys/dev/sound/midi/midi.h      |  6 ++--
 sys/dev/sound/midi/sequencer.c |  4 +--
 3 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/sys/dev/sound/midi/midi.c b/sys/dev/sound/midi/midi.c
index 2dcd64625b6c..d2b495ea04cd 100644
--- a/sys/dev/sound/midi/midi.c
+++ b/sys/dev/sound/midi/midi.c
@@ -178,7 +178,7 @@ TAILQ_HEAD(, snd_midi) midi_devs;
  * /dev/midistat variables and declarations, protected by midistat_lock
  */
 
-struct sx midistat_lock;
+struct sx mstat_lock;
 
 static int      midistat_isopen = 0;
 static struct sbuf midistat_sbuf;
@@ -259,6 +259,24 @@ SYSCTL_INT(_hw_midi_stat, OID_AUTO, verbose, CTLFLAG_RW,
  * CODE START
  */
 
+void
+midistat_lock(void)
+{
+	sx_xlock(&mstat_lock);
+}
+
+void
+midistat_unlock(void)
+{
+	sx_xunlock(&mstat_lock);
+}
+
+void
+midistat_lockassert(void)
+{
+	sx_assert(&mstat_lock, SA_XLOCKED);
+}
+
 /*
  * Register a new rmidi device. cls midi_if interface unit == 0 means
  * auto-assign new unit number unit != 0 already assigned a unit number, eg.
@@ -281,7 +299,7 @@ midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
 	uint8_t *buf;
 
 	MIDI_DEBUG(1, printf("midiinit: unit %d/%d.\n", unit, channel));
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	/*
 	 * Protect against call with existing unit/channel or auto-allocate a
 	 * new unit number.
@@ -359,7 +377,7 @@ midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
 
 	TAILQ_INSERT_TAIL(&midi_devs, m, link);
 
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 
 	m->dev = make_dev(&midi_cdevsw, unit, UID_ROOT, GID_WHEEL, 0666,
 	    "midi%d.%d", unit, channel);
@@ -379,7 +397,7 @@ err1:
 	free(m->synth, M_MIDI);
 	free(m, M_MIDI);
 err0:
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	MIDI_DEBUG(1, printf("midi_init ended in error\n"));
 	return NULL;
 }
@@ -397,7 +415,7 @@ midi_uninit(struct snd_midi *m)
 	int err;
 
 	err = EBUSY;
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	mtx_lock(&m->lock);
 	if (m->busy) {
 		if (!(m->rchan || m->wchan))
@@ -419,7 +437,7 @@ midi_uninit(struct snd_midi *m)
 err:
 	mtx_unlock(&m->lock);
 exit:
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	return err;
 }
 
@@ -927,9 +945,9 @@ midistat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 
 	MIDI_DEBUG(1, printf("midistat_open\n"));
 
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	if (midistat_isopen) {
-		sx_xunlock(&midistat_lock);
+		midistat_unlock();
 		return EBUSY;
 	}
 	midistat_isopen = 1;
@@ -941,7 +959,7 @@ midistat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 out:
 	if (error)
 		midistat_isopen = 0;
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	return error;
 }
 
@@ -949,14 +967,14 @@ static int
 midistat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
 {
 	MIDI_DEBUG(1, printf("midistat_close\n"));
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	if (!midistat_isopen) {
-		sx_xunlock(&midistat_lock);
+		midistat_unlock();
 		return EBADF;
 	}
 	sbuf_delete(&midistat_sbuf);
 	midistat_isopen = 0;
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	return 0;
 }
 
@@ -967,13 +985,13 @@ midistat_read(struct cdev *i_dev, struct uio *uio, int flag)
 	int err;
 
 	MIDI_DEBUG(4, printf("midistat_read\n"));
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	if (!midistat_isopen) {
-		sx_xunlock(&midistat_lock);
+		midistat_unlock();
 		return EBADF;
 	}
 	if (uio->uio_offset < 0 || uio->uio_offset > sbuf_len(&midistat_sbuf)) {
-		sx_xunlock(&midistat_lock);
+		midistat_unlock();
 		return EINVAL;
 	}
 	err = 0;
@@ -982,7 +1000,7 @@ midistat_read(struct cdev *i_dev, struct uio *uio, int flag)
 		err = uiomove(sbuf_data(&midistat_sbuf) + uio->uio_offset, l,
 		    uio);
 	}
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	return err;
 }
 
@@ -995,7 +1013,7 @@ midistat_prepare(struct sbuf *s)
 {
 	struct snd_midi *m;
 
-	sx_assert(&midistat_lock, SA_XLOCKED);
+	midistat_lockassert();
 
 	sbuf_printf(s, "FreeBSD Midi Driver (midi2)\n");
 	if (TAILQ_EMPTY(&midi_devs)) {
@@ -1354,7 +1372,7 @@ midisynth_bender(void *n, uint8_t chn, uint16_t val)
 static int
 midi_destroy(struct snd_midi *m, int midiuninit)
 {
-	sx_assert(&midistat_lock, SA_XLOCKED);
+	midistat_lockassert();
 	mtx_assert(&m->lock, MA_OWNED);
 
 	MIDI_DEBUG(3, printf("midi_destroy\n"));
@@ -1380,7 +1398,7 @@ midi_destroy(struct snd_midi *m, int midiuninit)
 static int
 midi_load(void)
 {
-	sx_init(&midistat_lock, "midistat lock");
+	sx_init(&mstat_lock, "midistat lock");
 	TAILQ_INIT(&midi_devs);
 
 	midistat_dev = make_dev(&midistat_cdevsw, MIDI_DEV_MIDICTL, UID_ROOT,
@@ -1397,7 +1415,7 @@ midi_unload(void)
 
 	MIDI_DEBUG(1, printf("midi_unload()\n"));
 	retval = EBUSY;
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	if (midistat_isopen)
 		goto exit0;
 
@@ -1410,19 +1428,19 @@ midi_unload(void)
 		if (retval)
 			goto exit1;
 	}
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	destroy_dev(midistat_dev);
 
 	/*
 	 * Made it here then unload is complete
 	 */
-	sx_destroy(&midistat_lock);
+	sx_destroy(&mstat_lock);
 	return 0;
 
 exit1:
 	mtx_unlock(&m->lock);
 exit0:
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	if (retval)
 		MIDI_DEBUG(2, printf("midi_unload: failed\n"));
 	return retval;
@@ -1471,7 +1489,7 @@ midimapper_open_locked(void *arg1, void **cookie)
 	int retval = 0;
 	struct snd_midi *m;
 
-	sx_assert(&midistat_lock, SX_XLOCKED);
+	midistat_lockassert();
 	TAILQ_FOREACH(m, &midi_devs, link) {
 		retval++;
 	}
@@ -1484,9 +1502,9 @@ midimapper_open(void *arg1, void **cookie)
 {
 	int retval;
 
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	retval = midimapper_open_locked(arg1, cookie);
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 
 	return retval;
 }
@@ -1503,7 +1521,7 @@ midimapper_fetch_synth_locked(void *arg, void *cookie, int unit)
 	struct snd_midi *m;
 	int retval = 0;
 
-	sx_assert(&midistat_lock, SX_XLOCKED);
+	midistat_lockassert();
 	TAILQ_FOREACH(m, &midi_devs, link) {
 		if (unit == retval)
 			return (kobj_t)m->synth;
@@ -1518,9 +1536,9 @@ midimapper_fetch_synth(void *arg, void *cookie, int unit)
 {
 	kobj_t synth;
 
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	synth = midimapper_fetch_synth_locked(arg, cookie, unit);
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 
 	return synth;
 }
diff --git a/sys/dev/sound/midi/midi.h b/sys/dev/sound/midi/midi.h
index afbfdfa6abbc..2254fab690e9 100644
--- a/sys/dev/sound/midi/midi.h
+++ b/sys/dev/sound/midi/midi.h
@@ -39,10 +39,12 @@ MALLOC_DECLARE(M_MIDI);
 #define M_RXEN		0x04
 #define M_TXEN		0x08
 
-extern struct sx midistat_lock;
-
 struct snd_midi;
 
+void	midistat_lock(void);
+void	midistat_unlock(void);
+void	midistat_lockassert(void);
+
 struct snd_midi *
 midi_init(kobj_class_t _mpu_cls, int _unit, int _channel, void *cookie);
 int	midi_uninit(struct snd_midi *_m);
diff --git a/sys/dev/sound/midi/sequencer.c b/sys/dev/sound/midi/sequencer.c
index ec99fdea4637..ab2d48294a6a 100644
--- a/sys/dev/sound/midi/sequencer.c
+++ b/sys/dev/sound/midi/sequencer.c
@@ -747,7 +747,7 @@ mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	 * Mark this device busy.
 	 */
 
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	mtx_lock(&scp->seq_lock);
 	if (scp->busy) {
 		mtx_unlock(&scp->seq_lock);
@@ -785,7 +785,7 @@ mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 			}
 		}
 	}
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 
 	timer_setvals(scp, 60, 100);