git: 2d6fc24673cc - main - sound: Handle midistat_lock through function interface

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Sat, 06 Jul 2024 18:24:15 UTC
The branch main has been updated by christos:

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

commit 2d6fc24673ccc97020c94094f97ee015f1db9702
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2024-07-06 18:22:59 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2024-07-06 18:22:59 +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
---
 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 66fb67b31e7a..c42cb1d7a90c 100644
--- a/sys/dev/sound/midi/midi.c
+++ b/sys/dev/sound/midi/midi.c
@@ -177,7 +177,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;
@@ -258,6 +258,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.
@@ -280,7 +298,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.
@@ -358,7 +376,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);
@@ -378,7 +396,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;
 }
@@ -396,7 +414,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))
@@ -418,7 +436,7 @@ midi_uninit(struct snd_midi *m)
 err:
 	mtx_unlock(&m->lock);
 exit:
-	sx_xunlock(&midistat_lock);
+	midistat_unlock();
 	return err;
 }
 
@@ -926,9 +944,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;
@@ -940,7 +958,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;
 }
 
@@ -948,14 +966,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;
 }
 
@@ -966,13 +984,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;
@@ -981,7 +999,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;
 }
 
@@ -994,7 +1012,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)) {
@@ -1353,7 +1371,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"));
@@ -1379,7 +1397,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,
@@ -1396,7 +1414,7 @@ midi_unload(void)
 
 	MIDI_DEBUG(1, printf("midi_unload()\n"));
 	retval = EBUSY;
-	sx_xlock(&midistat_lock);
+	midistat_lock();
 	if (midistat_isopen)
 		goto exit0;
 
@@ -1409,19 +1427,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;
@@ -1470,7 +1488,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++;
 	}
@@ -1483,9 +1501,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;
 }
@@ -1502,7 +1520,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;
@@ -1517,9 +1535,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 01f9d0b4809b..6b3a2711e951 100644
--- a/sys/dev/sound/midi/sequencer.c
+++ b/sys/dev/sound/midi/sequencer.c
@@ -746,7 +746,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);
@@ -784,7 +784,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);