From c06269955785b74639aeb81042a1d53ab90d1949 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sat, 10 Jan 2015 19:25:35 +0100
Subject: [PATCH 2/2] kernel/sound: Port midi.

---
 sys/dev/sound/midi/midi.c        | 474 +++++++++++++++++--------------
 sys/dev/sound/midi/midiq.h       |  12 +-
 sys/dev/sound/midi/mpu401.c      |  29 +-
 sys/dev/sound/midi/sequencer.c   | 589 +++++++++++++++++++++------------------
 sys/dev/sound/midi/synth_if.m    |  40 +--
 sys/dev/sound/pci/emu10kx-midi.c |  14 +-
 6 files changed, 638 insertions(+), 520 deletions(-)

diff --git a/sys/dev/sound/midi/midi.c b/sys/dev/sound/midi/midi.c
index ace7dfc..a54d66d 100644
--- a/sys/dev/sound/midi/midi.c
+++ b/sys/dev/sound/midi/midi.c
@@ -45,7 +45,6 @@ __FBSDID("$FreeBSD: head/sys/dev/sound/midi/midi.c 227309 2011-11-07 15:43:11Z e
 #include <sys/proc.h>
 #include <sys/signalvar.h>
 #include <sys/conf.h>
-#include <sys/selinfo.h>
 #include <sys/sysctl.h>
 #include <sys/types.h>
 #include <sys/malloc.h>
@@ -59,6 +58,7 @@ __FBSDID("$FreeBSD: head/sys/dev/sound/midi/midi.c 227309 2011-11-07 15:43:11Z e
 #include <sys/sbuf.h>
 #include <sys/kobj.h>
 #include <sys/module.h>
+#include <sys/device.h>
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_snd.h"
@@ -99,7 +99,7 @@ enum midi_states {
 #define MIDI_NAMELEN   16
 struct snd_midi {
 	KOBJ_FIELDS;
-	struct mtx lock;		/* Protects all but queues */
+	struct lock lock;		/* Protects all but queues */
 	void   *cookie;
 
 	int	unit;			/* Should only be used in midistat */
@@ -108,10 +108,10 @@ struct snd_midi {
 	int	busy;
 	int	flags;			/* File flags */
 	char	name[MIDI_NAMELEN];
-	struct mtx qlock;		/* Protects inq, outq and flags */
+	struct lock qlock;		/* Protects inq, outq and flags */
 	MIDIQ_HEAD(, char) inq, outq;
 	int	rchan, wchan;
-	struct selinfo rsel, wsel;
+	struct kqinfo rkq, wkq;
 	int	hiwat;			/* QLEN(outq)>High-water -> disable
 					 * writes from userland */
 	enum midi_states inq_state;
@@ -180,7 +180,7 @@ TAILQ_HEAD(, snd_midi) midi_devs;
  * /dev/midistat variables and declarations, protected by midistat_lock
  */
 
-static struct mtx midistat_lock;
+static struct lock midistat_lock;
 static int      midistat_isopen = 0;
 static struct sbuf midistat_sbuf;
 static int      midistat_bufptr;
@@ -194,14 +194,21 @@ static d_open_t midistat_open;
 static d_close_t midistat_close;
 static d_read_t midistat_read;
 
-static struct cdevsw midistat_cdevsw = {
-	.d_version = D_VERSION,
+static void	midi_filter_detach(struct knote *);
+static int	midi_filter_read(struct knote *, long);
+static int	midi_filter_write(struct knote *, long);
+
+static struct dev_ops midistat_ops = {
+	{ "midistat", 0, D_MPSAFE },
 	.d_open = midistat_open,
 	.d_close = midistat_close,
 	.d_read = midistat_read,
-	.d_name = "midistat",
 };
 
+static struct filterops midi_read_filterops =
+	{ FILTEROP_ISFD, NULL, midi_filter_detach, midi_filter_read };
+static struct filterops midi_write_filterops =
+	{ FILTEROP_ISFD, NULL, midi_filter_detach, midi_filter_write };
 
 /*
  * /dev/rmidi dev_t declarations, struct variable access is protected by
@@ -213,17 +220,16 @@ static d_close_t midi_close;
 static d_ioctl_t midi_ioctl;
 static d_read_t midi_read;
 static d_write_t midi_write;
-static d_poll_t midi_poll;
+static d_kqfilter_t midi_kqfilter;
 
-static struct cdevsw midi_cdevsw = {
-	.d_version = D_VERSION,
+static struct dev_ops midi_ops = {
+	{ "rmidi", 0, D_MPSAFE },
 	.d_open = midi_open,
 	.d_close = midi_close,
 	.d_read = midi_read,
 	.d_write = midi_write,
 	.d_ioctl = midi_ioctl,
-	.d_poll = midi_poll,
-	.d_name = "rmidi",
+	.d_kqfilter = midi_kqfilter,
 };
 
 /*
@@ -281,18 +287,18 @@ midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
 	int inqsize, outqsize;
 	MIDI_TYPE *buf;
 
-	MIDI_DEBUG(1, printf("midiinit: unit %d/%d.\n", unit, channel));
-	mtx_lock(&midistat_lock);
+	MIDI_DEBUG(1, kprintf("midiinit: unit %d/%d.\n", unit, channel));
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 	/*
 	 * Protect against call with existing unit/channel or auto-allocate a
 	 * new unit number.
 	 */
 	i = -1;
 	TAILQ_FOREACH(m, &midi_devs, link) {
-		mtx_lock(&m->lock);
+		lockmgr(&m->lock, LK_EXCLUSIVE);
 		if (unit != 0) {
 			if (m->unit == unit && m->channel == channel) {
-				mtx_unlock(&m->lock);
+				lockmgr(&m->lock, LK_RELEASE);
 				goto err0;
 			}
 		} else {
@@ -302,43 +308,41 @@ midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
 			if (m->unit > i)
 				i = m->unit;
 		}
-		mtx_unlock(&m->lock);
+		lockmgr(&m->lock, LK_RELEASE);
 	}
 
 	if (unit == 0)
 		unit = i + 1;
 
-	MIDI_DEBUG(1, printf("midiinit #2: unit %d/%d.\n", unit, channel));
-	m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
-	if (m == NULL)
-		goto err0;
+	MIDI_DEBUG(1, kprintf("midiinit #2: unit %d/%d.\n", unit, channel));
+	m = kmalloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO);
 
-	m->synth = malloc(sizeof(*m->synth), M_MIDI, M_NOWAIT | M_ZERO);
+	m->synth = kmalloc(sizeof(*m->synth), M_MIDI, M_WAITOK | M_ZERO);
 	kobj_init((kobj_t)m->synth, &midisynth_class);
 	m->synth->m = m;
 	kobj_init((kobj_t)m, cls);
 	inqsize = MPU_INQSIZE(m, cookie);
 	outqsize = MPU_OUTQSIZE(m, cookie);
 
-	MIDI_DEBUG(1, printf("midiinit queues %d/%d.\n", inqsize, outqsize));
+	MIDI_DEBUG(1, kprintf("midiinit queues %d/%d.\n", inqsize, outqsize));
 	if (!inqsize && !outqsize)
 		goto err1;
 
-	mtx_init(&m->lock, "raw midi", NULL, 0);
-	mtx_init(&m->qlock, "q raw midi", NULL, 0);
+	lockinit(&m->lock, "raw midi", 0, LK_CANRECURSE);
+	lockinit(&m->qlock, "q raw midi", 0, LK_CANRECURSE);
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	if (inqsize)
-		buf = malloc(sizeof(MIDI_TYPE) * inqsize, M_MIDI, M_NOWAIT);
+		buf = kmalloc(sizeof(MIDI_TYPE) * inqsize, M_MIDI, M_WAITOK);
 	else
 		buf = NULL;
 
 	MIDIQ_INIT(m->inq, buf, inqsize);
 
 	if (outqsize)
-		buf = malloc(sizeof(MIDI_TYPE) * outqsize, M_MIDI, M_NOWAIT);
+		buf = kmalloc(sizeof(MIDI_TYPE) * outqsize, M_MIDI, M_WAITOK);
 	else
 		buf = NULL;
 	m->hiwat = outqsize / 2;
@@ -359,30 +363,30 @@ midi_init(kobj_class_t cls, int unit, int channel, void *cookie)
 	if (MPU_INIT(m, cookie))
 		goto err2;
 
-	mtx_unlock(&m->lock);
-	mtx_unlock(&m->qlock);
+	lockmgr(&m->lock, LK_RELEASE);
+	lockmgr(&m->qlock, LK_RELEASE);
 
 	TAILQ_INSERT_TAIL(&midi_devs, m, link);
 
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 
-	m->dev = make_dev(&midi_cdevsw,
+	m->dev = make_dev(&midi_ops,
 	    MIDIMKMINOR(unit, MIDI_DEV_RAW, channel),
 	    UID_ROOT, GID_WHEEL, 0666, "midi%d.%d", unit, channel);
 	m->dev->si_drv1 = m;
 
 	return m;
 
-err2:	mtx_destroy(&m->qlock);
-	mtx_destroy(&m->lock);
+err2:	lockuninit(&m->qlock);
+	lockuninit(&m->lock);
 
 	if (MIDIQ_BUF(m->inq))
-		free(MIDIQ_BUF(m->inq), M_MIDI);
+		kfree(MIDIQ_BUF(m->inq), M_MIDI);
 	if (MIDIQ_BUF(m->outq))
-		free(MIDIQ_BUF(m->outq), M_MIDI);
-err1:	free(m, M_MIDI);
-err0:	mtx_unlock(&midistat_lock);
-	MIDI_DEBUG(1, printf("midi_init ended in error\n"));
+		kfree(MIDIQ_BUF(m->outq), M_MIDI);
+err1:	kfree(m, M_MIDI);
+err0:	lockmgr(&midistat_lock, LK_RELEASE);
+	MIDI_DEBUG(1, kprintf("midi_init ended in error\n"));
 	return NULL;
 }
 
@@ -399,8 +403,8 @@ midi_uninit(struct snd_midi *m)
 	int err;
 
 	err = ENXIO;
-	mtx_lock(&midistat_lock);
-	mtx_lock(&m->lock);
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
 	if (m->busy) {
 		if (!(m->rchan || m->wchan))
 			goto err;
@@ -418,8 +422,8 @@ midi_uninit(struct snd_midi *m)
 	if (!err)
 		goto exit;
 
-err:	mtx_unlock(&m->lock);
-exit:	mtx_unlock(&midistat_lock);
+err:	lockmgr(&m->lock, LK_RELEASE);
+exit:	lockmgr(&midistat_lock, LK_RELEASE);
 	return err;
 }
 
@@ -451,7 +455,7 @@ midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
 	int used;
 
 	/* MIDI_TYPE       data; */
-	MIDI_DEBUG(5, printf("midi_in: m=%p size=%d\n", m, size));
+	MIDI_DEBUG(5, kprintf("midi_in: m=%p size=%d\n", m, size));
 
 /*
  * XXX: locking flub
@@ -461,7 +465,7 @@ midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
 
 	used = 0;
 
-	mtx_lock(&m->qlock);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 #if 0
 	/*
 	 * Don't bother queuing if not in read mode.  Discard everything and
@@ -544,31 +548,31 @@ midi_in(struct snd_midi *m, MIDI_TYPE *buf, int size)
 	}
 	if (sig) {
 #endif
-		MIDI_DEBUG(6, printf("midi_in: len %jd avail %jd\n",
+		MIDI_DEBUG(6, kprintf("midi_in: len %jd avail %jd\n",
 		    (intmax_t)MIDIQ_LEN(m->inq),
 		    (intmax_t)MIDIQ_AVAIL(m->inq)));
 		if (MIDIQ_AVAIL(m->inq) > size) {
 			used = size;
 			MIDIQ_ENQ(m->inq, buf, size);
 		} else {
-			MIDI_DEBUG(4, printf("midi_in: Discarding data qu\n"));
-			mtx_unlock(&m->qlock);
+			MIDI_DEBUG(4, kprintf("midi_in: Discarding data qu\n"));
+			lockmgr(&m->qlock, LK_RELEASE);
 			return 0;
 		}
 		if (m->rchan) {
 			wakeup(&m->rchan);
 			m->rchan = 0;
 		}
-		selwakeup(&m->rsel);
+		KNOTE(&m->rkq.ki_note, 0);
 		if (m->async) {
-			PROC_LOCK(m->async);
-			kern_psignal(m->async, SIGIO);
-			PROC_UNLOCK(m->async);
+			PHOLD(m->async);
+			ksignal(m->async, SIGIO);
+			PRELE(m->async);
 		}
 #if 0
 	}
 #endif
-	mtx_unlock(&m->qlock);
+	lockmgr(&m->qlock, LK_RELEASE);
 	return used;
 }
 
@@ -586,10 +590,10 @@ midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
 	if (!(m->flags & M_TXEN))
 		return 0;
 
-	MIDI_DEBUG(2, printf("midi_out: %p\n", m));
-	mtx_lock(&m->qlock);
+	MIDI_DEBUG(2, kprintf("midi_out: %p\n", m));
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 	used = MIN(size, MIDIQ_LEN(m->outq));
-	MIDI_DEBUG(3, printf("midi_out: used %d\n", used));
+	MIDI_DEBUG(3, kprintf("midi_out: used %d\n", used));
 	if (used)
 		MIDIQ_DEQ(m->outq, buf, used);
 	if (MIDIQ_EMPTY(m->outq)) {
@@ -601,14 +605,14 @@ midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
 			wakeup(&m->wchan);
 			m->wchan = 0;
 		}
-		selwakeup(&m->wsel);
+		KNOTE(&m->wkq.ki_note, 0);
 		if (m->async) {
-			PROC_LOCK(m->async);
-			kern_psignal(m->async, SIGIO);
-			PROC_UNLOCK(m->async);
+			PHOLD(m->async);
+			ksignal(m->async, SIGIO);
+			PRELE(m->async);
 		}
 	}
-	mtx_unlock(&m->qlock);
+	lockmgr(&m->qlock, LK_RELEASE);
 	return used;
 }
 
@@ -617,18 +621,22 @@ midi_out(struct snd_midi *m, MIDI_TYPE *buf, int size)
  * /dev/rmidi#.#	device access functions
  */
 int
-midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
+midi_open(struct dev_open_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	int flags = ap->a_oflags;
 	struct snd_midi *m = i_dev->si_drv1;
 	int retval;
 
-	MIDI_DEBUG(1, printf("midiopen %p %s %s\n", td,
+#if 0 /* XXX */
+	MIDI_DEBUG(1, kprintf("midiopen %p %s %s\n", td,
 	    flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
+#endif
 	if (m == NULL)
 		return ENXIO;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	retval = 0;
 
@@ -668,28 +676,32 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 
 	MPU_CALLBACK(m, m->cookie, m->flags);
 
-	MIDI_DEBUG(2, printf("midi_open: opened.\n"));
+	MIDI_DEBUG(2, kprintf("midi_open: opened.\n"));
 
-err:	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
+err:	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
 	return retval;
 }
 
 int
-midi_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
+midi_close(struct dev_close_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	int flags = ap->a_fflag;
 	struct snd_midi *m = i_dev->si_drv1;
 	int retval;
 	int oldflags;
 
-	MIDI_DEBUG(1, printf("midi_close %p %s %s\n", td,
+#if 0 /* XXX */
+	MIDI_DEBUG(1, kprintf("midi_close %p %s %s\n", td,
 	    flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
+#endif
 
 	if (m == NULL)
 		return ENXIO;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	if ((flags & FREAD && !(m->flags & M_RX)) ||
 	    (flags & FWRITE && !(m->flags & M_TX))) {
@@ -708,10 +720,10 @@ midi_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
 		MPU_CALLBACK(m, m->cookie, m->flags);
 
-	MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
+	MIDI_DEBUG(1, kprintf("midi_close: closed, busy = %d.\n", m->busy));
 
-	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
+	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
 	retval = 0;
 err:	return retval;
 }
@@ -721,15 +733,18 @@ err:	return retval;
  * as data is available.
  */
 int
-midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
+midi_read(struct dev_read_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	struct uio *uio = ap->a_uio;
+	int ioflag = ap->a_ioflag;
 #define MIDI_RSIZE 32
 	struct snd_midi *m = i_dev->si_drv1;
 	int retval;
 	int used;
 	char buf[MIDI_RSIZE];
 
-	MIDI_DEBUG(5, printf("midiread: count=%lu\n",
+	MIDI_DEBUG(5, kprintf("midiread: count=%lu\n",
 	    (unsigned long)uio->uio_resid));
 
 	retval = EIO;
@@ -737,8 +752,8 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 	if (m == NULL)
 		goto err0;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	if (!(m->flags & M_RX))
 		goto err1;
@@ -748,10 +763,10 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 			retval = EWOULDBLOCK;
 			if (ioflag & O_NONBLOCK)
 				goto err1;
-			mtx_unlock(&m->lock);
+			lockmgr(&m->lock, LK_RELEASE);
 			m->rchan = 1;
-			retval = msleep(&m->rchan, &m->qlock,
-			    PCATCH | PDROP, "midi RX", 0);
+			retval = lksleep(&m->rchan, &m->qlock,
+			    PCATCH, "midi RX", 0);
 			/*
 			 * We slept, maybe things have changed since last
 			 * dying check
@@ -763,13 +778,13 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 			/* if (retval && retval != ERESTART) */
 			if (retval)
 				goto err0;
-			mtx_lock(&m->lock);
-			mtx_lock(&m->qlock);
+			lockmgr(&m->lock, LK_EXCLUSIVE);
+			lockmgr(&m->qlock, LK_EXCLUSIVE);
 			m->rchan = 0;
 			if (!m->busy)
 				goto err1;
 		}
-		MIDI_DEBUG(6, printf("midi_read start\n"));
+		MIDI_DEBUG(6, kprintf("midi_read start\n"));
 		/*
 	         * At this point, it is certain that m->inq has data
 	         */
@@ -777,7 +792,7 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 		used = MIN(MIDIQ_LEN(m->inq), uio->uio_resid);
 		used = MIN(used, MIDI_RSIZE);
 
-		MIDI_DEBUG(6, printf("midiread: uiomove cc=%d\n", used));
+		MIDI_DEBUG(6, kprintf("midiread: uiomove cc=%d\n", used));
 		MIDIQ_DEQ(m->inq, buf, used);
 		retval = uiomove(buf, used, uio);
 		if (retval)
@@ -788,9 +803,9 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 	 * If we Made it here then transfer is good
 	 */
 	retval = 0;
-err1:	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
-err0:	MIDI_DEBUG(4, printf("midi_read: ret %d\n", retval));
+err1:	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
+err0:	MIDI_DEBUG(4, kprintf("midi_read: ret %d\n", retval));
 	return retval;
 }
 
@@ -799,8 +814,11 @@ err0:	MIDI_DEBUG(4, printf("midi_read: ret %d\n", retval));
  */
 
 int
-midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
+midi_write(struct dev_write_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	struct uio *uio = ap->a_uio;
+	int ioflag = ap->a_ioflag;
 #define MIDI_WSIZE 32
 	struct snd_midi *m = i_dev->si_drv1;
 	int retval;
@@ -808,13 +826,13 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 	char buf[MIDI_WSIZE];
 
 
-	MIDI_DEBUG(4, printf("midi_write\n"));
+	MIDI_DEBUG(4, kprintf("midi_write\n"));
 	retval = 0;
 	if (m == NULL)
 		goto err0;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	if (!(m->flags & M_TX))
 		goto err1;
@@ -824,11 +842,11 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 			retval = EWOULDBLOCK;
 			if (ioflag & O_NONBLOCK)
 				goto err1;
-			mtx_unlock(&m->lock);
+			lockmgr(&m->lock, LK_RELEASE);
 			m->wchan = 1;
-			MIDI_DEBUG(3, printf("midi_write msleep\n"));
-			retval = msleep(&m->wchan, &m->qlock,
-			    PCATCH | PDROP, "midi TX", 0);
+			MIDI_DEBUG(3, kprintf("midi_write lksleep\n"));
+			retval = lksleep(&m->wchan, &m->qlock,
+			    PCATCH, "midi TX", 0);
 			/*
 			 * We slept, maybe things have changed since last
 			 * dying check
@@ -839,8 +857,8 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 				retval = ENXIO;
 			if (retval)
 				goto err0;
-			mtx_lock(&m->lock);
-			mtx_lock(&m->qlock);
+			lockmgr(&m->lock, LK_EXCLUSIVE);
+			lockmgr(&m->qlock, LK_EXCLUSIVE);
 			m->wchan = 0;
 			if (!m->busy)
 				goto err1;
@@ -852,12 +870,12 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 
 		used = MIN(MIDIQ_AVAIL(m->outq), uio->uio_resid);
 		used = MIN(used, MIDI_WSIZE);
-		MIDI_DEBUG(5, printf("midiout: resid %zd len %jd avail %jd\n",
+		MIDI_DEBUG(5, kprintf("midiout: resid %zd len %jd avail %jd\n",
 		    uio->uio_resid, (intmax_t)MIDIQ_LEN(m->outq),
 		    (intmax_t)MIDIQ_AVAIL(m->outq)));
 
 
-		MIDI_DEBUG(5, printf("midi_write: uiomove cc=%d\n", used));
+		MIDI_DEBUG(5, kprintf("midi_write: uiomove cc=%d\n", used));
 		retval = uiomove(buf, used, uio);
 		if (retval)
 			goto err1;
@@ -874,51 +892,94 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 	 * If we Made it here then transfer is good
 	 */
 	retval = 0;
-err1:	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
+err1:	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
 err0:	return retval;
 }
 
 int
-midi_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
-    struct thread *td)
+midi_ioctl(struct dev_ioctl_args *ap)
 {
 	return ENXIO;
 }
 
 int
-midi_poll(struct cdev *i_dev, int events, struct thread *td)
+midi_kqfilter(struct dev_kqfilter_args *ap)
 {
-	struct snd_midi *m = i_dev->si_drv1;
-	int revents;
+	cdev_t dev = ap->a_head.a_dev;
+	struct knote *kn = ap->a_kn;
+	struct snd_midi *m;
+	struct klist *klist;
 
-	if (m == NULL)
-		return 0;
+	ap->a_result = 0;
+	m = dev->si_drv1;
+
+	switch (kn->kn_filter) {
+	case EVFILT_READ:
+		kn->kn_fop = &midi_read_filterops;
+		kn->kn_hook = (caddr_t)m;
+		klist = &m->rkq.ki_note;
+		break;
+	case EVFILT_WRITE:
+		kn->kn_fop = &midi_write_filterops;
+		kn->kn_hook = (caddr_t)m;
+		klist = &m->wkq.ki_note;
+		break;
+	default:
+		ap->a_result = EOPNOTSUPP;
+		return (0);
+	}
+
+	knote_insert(klist, kn);
 
-	revents = 0;
+	return(0);
+}
+
+static void
+midi_filter_detach(struct knote *kn)
+{
+	struct snd_midi *m = (struct snd_midi *)kn->kn_hook;
+	struct klist *rklist = &m->rkq.ki_note;
+	struct klist *wklist = &m->wkq.ki_note;
+
+	knote_remove(rklist, kn);
+	knote_remove(wklist, kn);
+}
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+static int
+midi_filter_read(struct knote *kn, long hint)
+{
+	struct snd_midi *m = (struct snd_midi *)kn->kn_hook;
+	int ready = 0;
 
-	if (events & (POLLIN | POLLRDNORM))
-		if (!MIDIQ_EMPTY(m->inq))
-			events |= events & (POLLIN | POLLRDNORM);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
-	if (events & (POLLOUT | POLLWRNORM))
-		if (MIDIQ_AVAIL(m->outq) < m->hiwat)
-			events |= events & (POLLOUT | POLLWRNORM);
+	if (!MIDIQ_EMPTY(m->inq))
+		ready = 1;
 
-	if (revents == 0) {
-		if (events & (POLLIN | POLLRDNORM))
-			selrecord(td, &m->rsel);
+	lockmgr(&m->lock, LK_RELEASE);
+	lockmgr(&m->qlock, LK_RELEASE);
 
-		if (events & (POLLOUT | POLLWRNORM))
-			selrecord(td, &m->wsel);
-	}
-	mtx_unlock(&m->lock);
-	mtx_unlock(&m->qlock);
+	return (ready);
+}
+
+static int
+midi_filter_write(struct knote *kn, long hint)
+{
+	struct snd_midi *m = (struct snd_midi *)kn->kn_hook;
+	int ready = 0;
+
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
+
+	if (MIDIQ_AVAIL(m->outq) < m->hiwat)
+		ready = 1;
+
+	lockmgr(&m->lock, LK_RELEASE);
+	lockmgr(&m->qlock, LK_RELEASE);
 
-	return (revents);
+	return (ready);
 }
 
 /*
@@ -926,73 +987,74 @@ midi_poll(struct cdev *i_dev, int events, struct thread *td)
  *
  */
 static int
-midistat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
+midistat_open(struct dev_open_args *ap)
 {
 	int error;
 
-	MIDI_DEBUG(1, printf("midistat_open\n"));
-	mtx_lock(&midistat_lock);
+	MIDI_DEBUG(1, kprintf("midistat_open\n"));
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 
 	if (midistat_isopen) {
-		mtx_unlock(&midistat_lock);
+		lockmgr(&midistat_lock, LK_RELEASE);
 		return EBUSY;
 	}
 	midistat_isopen = 1;
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 
 	if (sbuf_new(&midistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
 		error = ENXIO;
-		mtx_lock(&midistat_lock);
+		lockmgr(&midistat_lock, LK_EXCLUSIVE);
 		goto out;
 	}
-	mtx_lock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 	midistat_bufptr = 0;
 	error = (midistat_prepare(&midistat_sbuf) > 0) ? 0 : ENOMEM;
 
 out:	if (error)
 		midistat_isopen = 0;
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 	return error;
 }
 
 static int
-midistat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
+midistat_close(struct dev_close_args *ap)
 {
-	MIDI_DEBUG(1, printf("midistat_close\n"));
-	mtx_lock(&midistat_lock);
+	MIDI_DEBUG(1, kprintf("midistat_close\n"));
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 	if (!midistat_isopen) {
-		mtx_unlock(&midistat_lock);
+		lockmgr(&midistat_lock, LK_RELEASE);
 		return EBADF;
 	}
 	sbuf_delete(&midistat_sbuf);
 	midistat_isopen = 0;
 
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 	return 0;
 }
 
 static int
-midistat_read(struct cdev *i_dev, struct uio *buf, int flag)
+midistat_read(struct dev_read_args *ap)
 {
+	struct uio *buf = ap->a_uio;
 	int l, err;
 
-	MIDI_DEBUG(4, printf("midistat_read\n"));
-	mtx_lock(&midistat_lock);
+	MIDI_DEBUG(4, kprintf("midistat_read\n"));
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 	if (!midistat_isopen) {
-		mtx_unlock(&midistat_lock);
+		lockmgr(&midistat_lock, LK_RELEASE);
 		return EBADF;
 	}
 	l = min(buf->uio_resid, sbuf_len(&midistat_sbuf) - midistat_bufptr);
 	err = 0;
 	if (l > 0) {
-		mtx_unlock(&midistat_lock);
+		lockmgr(&midistat_lock, LK_RELEASE);
 		err = uiomove(sbuf_data(&midistat_sbuf) + midistat_bufptr, l,
 		    buf);
-		mtx_lock(&midistat_lock);
+		lockmgr(&midistat_lock, LK_EXCLUSIVE);
 	} else
 		l = 0;
 	midistat_bufptr += l;
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 	return err;
 }
 
@@ -1005,7 +1067,7 @@ midistat_prepare(struct sbuf *s)
 {
 	struct snd_midi *m;
 
-	mtx_assert(&midistat_lock, MA_OWNED);
+	KKASSERT(lockowned(&midistat_lock));
 
 	sbuf_printf(s, "FreeBSD Midi Driver (midi2)\n");
 	if (TAILQ_EMPTY(&midi_devs)) {
@@ -1016,12 +1078,12 @@ midistat_prepare(struct sbuf *s)
 	sbuf_printf(s, "Installed devices:\n");
 
 	TAILQ_FOREACH(m, &midi_devs, link) {
-		mtx_lock(&m->lock);
+		lockmgr(&m->lock, LK_EXCLUSIVE);
 		sbuf_printf(s, "%s [%d/%d:%s]", m->name, m->unit, m->channel,
 		    MPU_PROVIDER(m, m->cookie));
 		sbuf_printf(s, "%s", MPU_DESCR(m, m->cookie, midistat_verbose));
 		sbuf_printf(s, "\n");
-		mtx_unlock(&m->lock);
+		lockmgr(&m->lock, LK_RELEASE);
 	}
 
 	sbuf_finish(s);
@@ -1077,14 +1139,14 @@ midisynth_open(void *n, void *arg, int flags)
 	struct snd_midi *m = ((struct synth_midi *)n)->m;
 	int retval;
 
-	MIDI_DEBUG(1, printf("midisynth_open %s %s\n",
+	MIDI_DEBUG(1, kprintf("midisynth_open %s %s\n",
 	    flags & FREAD ? "M_RX" : "", flags & FWRITE ? "M_TX" : ""));
 
 	if (m == NULL)
 		return ENXIO;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	retval = 0;
 
@@ -1129,9 +1191,9 @@ midisynth_open(void *n, void *arg, int flags)
 	MPU_CALLBACK(m, m->cookie, m->flags);
 
 
-err:	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
-	MIDI_DEBUG(2, printf("midisynth_open: return %d.\n", retval));
+err:	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
+	MIDI_DEBUG(2, kprintf("midisynth_open: return %d.\n", retval));
 	return retval;
 }
 
@@ -1142,15 +1204,15 @@ midisynth_close(void *n)
 	int retval;
 	int oldflags;
 
-	MIDI_DEBUG(1, printf("midisynth_close %s %s\n",
+	MIDI_DEBUG(1, kprintf("midisynth_close %s %s\n",
 	    m->synth_flags & FREAD ? "M_RX" : "",
 	    m->synth_flags & FWRITE ? "M_TX" : ""));
 
 	if (m == NULL)
 		return ENXIO;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	if ((m->synth_flags & FREAD && !(m->flags & M_RX)) ||
 	    (m->synth_flags & FWRITE && !(m->flags & M_TX))) {
@@ -1169,10 +1231,10 @@ midisynth_close(void *n)
 	if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN)))
 		MPU_CALLBACK(m, m->cookie, m->flags);
 
-	MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy));
+	MIDI_DEBUG(1, kprintf("midi_close: closed, busy = %d.\n", m->busy));
 
-	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
+	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
 	retval = 0;
 err:	return retval;
 }
@@ -1189,21 +1251,21 @@ midisynth_writeraw(void *n, uint8_t *buf, size_t len)
 	int used;
 	int i;
 
-	MIDI_DEBUG(4, printf("midisynth_writeraw\n"));
+	MIDI_DEBUG(4, kprintf("midisynth_writeraw\n"));
 
 	retval = 0;
 
 	if (m == NULL)
 		return ENXIO;
 
-	mtx_lock(&m->lock);
-	mtx_lock(&m->qlock);
+	lockmgr(&m->lock, LK_EXCLUSIVE);
+	lockmgr(&m->qlock, LK_EXCLUSIVE);
 
 	if (!(m->flags & M_TX))
 		goto err1;
 
 	if (midi_dumpraw)
-		printf("midi dump: ");
+		kprintf("midi dump: ");
 
 	while (len > 0) {
 		while (MIDIQ_AVAIL(m->outq) == 0) {
@@ -1211,11 +1273,11 @@ midisynth_writeraw(void *n, uint8_t *buf, size_t len)
 				m->flags |= M_TXEN;
 				MPU_CALLBACK(m, m->cookie, m->flags);
 			}
-			mtx_unlock(&m->lock);
+			lockmgr(&m->lock, LK_RELEASE);
 			m->wchan = 1;
-			MIDI_DEBUG(3, printf("midisynth_writeraw msleep\n"));
-			retval = msleep(&m->wchan, &m->qlock,
-			    PCATCH | PDROP, "midi TX", 0);
+			MIDI_DEBUG(3, kprintf("midisynth_writeraw lksleep\n"));
+			retval = lksleep(&m->wchan, &m->qlock,
+			    PCATCH, "midi TX", 0);
 			/*
 			 * We slept, maybe things have changed since last
 			 * dying check
@@ -1225,8 +1287,8 @@ midisynth_writeraw(void *n, uint8_t *buf, size_t len)
 
 			if (retval)
 				goto err0;
-			mtx_lock(&m->lock);
-			mtx_lock(&m->qlock);
+			lockmgr(&m->lock, LK_EXCLUSIVE);
+			lockmgr(&m->qlock, LK_EXCLUSIVE);
 			m->wchan = 0;
 			if (!m->busy)
 				goto err1;
@@ -1239,13 +1301,13 @@ midisynth_writeraw(void *n, uint8_t *buf, size_t len)
 		used = MIN(MIDIQ_AVAIL(m->outq), len);
 		used = MIN(used, MIDI_WSIZE);
 		MIDI_DEBUG(5,
-		    printf("midi_synth: resid %zu len %jd avail %jd\n",
+		    kprintf("midi_synth: resid %zu len %jd avail %jd\n",
 		    len, (intmax_t)MIDIQ_LEN(m->outq),
 		    (intmax_t)MIDIQ_AVAIL(m->outq)));
 
 		if (midi_dumpraw)
 			for (i = 0; i < used; i++)
-				printf("%x ", buf[i]);
+				kprintf("%x ", buf[i]);
 
 		MIDIQ_ENQ(m->outq, buf, used);
 		len -= used;
@@ -1262,11 +1324,11 @@ midisynth_writeraw(void *n, uint8_t *buf, size_t len)
 	 * If we Made it here then transfer is good
 	 */
 	if (midi_dumpraw)
-		printf("\n");
+		kprintf("\n");
 
 	retval = 0;
-err1:	mtx_unlock(&m->qlock);
-	mtx_unlock(&m->lock);
+err1:	lockmgr(&m->qlock, LK_RELEASE);
+	lockmgr(&m->lock, LK_RELEASE);
 err0:	return retval;
 }
 
@@ -1369,21 +1431,21 @@ static int
 midi_destroy(struct snd_midi *m, int midiuninit)
 {
 
-	mtx_assert(&midistat_lock, MA_OWNED);
-	mtx_assert(&m->lock, MA_OWNED);
+	KKASSERT(lockowned(&midistat_lock));
+	KKASSERT(lockowned(&m->lock));
 
-	MIDI_DEBUG(3, printf("midi_destroy\n"));
+	MIDI_DEBUG(3, kprintf("midi_destroy\n"));
 	m->dev->si_drv1 = NULL;
-	mtx_unlock(&m->lock);	/* XXX */
+	lockmgr(&m->lock, LK_RELEASE);	/* XXX */
 	destroy_dev(m->dev);
 	TAILQ_REMOVE(&midi_devs, m, link);
 	if (midiuninit)
 		MPU_UNINIT(m, m->cookie);
-	free(MIDIQ_BUF(m->inq), M_MIDI);
-	free(MIDIQ_BUF(m->outq), M_MIDI);
-	mtx_destroy(&m->qlock);
-	mtx_destroy(&m->lock);
-	free(m, M_MIDI);
+	kfree(MIDIQ_BUF(m->inq), M_MIDI);
+	kfree(MIDIQ_BUF(m->outq), M_MIDI);
+	lockuninit(&m->qlock);
+	lockuninit(&m->lock);
+	kfree(m, M_MIDI);
 	return 0;
 }
 
@@ -1392,12 +1454,12 @@ midi_destroy(struct snd_midi *m, int midiuninit)
  */
 
 static int
-midi_load()
+midi_load(void)
 {
-	mtx_init(&midistat_lock, "midistat lock", NULL, 0);
+	lockinit(&midistat_lock, "midistat lock", 0, LK_CANRECURSE);
 	TAILQ_INIT(&midi_devs);		/* Initialize the queue. */
 
-	midistat_dev = make_dev(&midistat_cdevsw,
+	midistat_dev = make_dev(&midistat_ops,
 	    MIDIMKMINOR(0, MIDI_DEV_MIDICTL, 0),
 	    UID_ROOT, GID_WHEEL, 0666, "midistat");
 
@@ -1405,19 +1467,19 @@ midi_load()
 }
 
 static int
-midi_unload()
+midi_unload(void)
 {
 	struct snd_midi *m;
 	int retval;
 
-	MIDI_DEBUG(1, printf("midi_unload()\n"));
+	MIDI_DEBUG(1, kprintf("midi_unload()\n"));
 	retval = EBUSY;
-	mtx_lock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 	if (midistat_isopen)
 		goto exit0;
 
 	TAILQ_FOREACH(m, &midi_devs, link) {
-		mtx_lock(&m->lock);
+		lockmgr(&m->lock, LK_EXCLUSIVE);
 		if (m->busy)
 			retval = EBUSY;
 		else
@@ -1426,21 +1488,21 @@ midi_unload()
 			goto exit1;
 	}
 
-	mtx_unlock(&midistat_lock);	/* XXX */
+	lockmgr(&midistat_lock, LK_RELEASE);	/* XXX */
 
 	destroy_dev(midistat_dev);
 	/*
 	 * Made it here then unload is complete
 	 */
-	mtx_destroy(&midistat_lock);
+	lockuninit(&midistat_lock);
 	return 0;
 
 exit1:
-	mtx_unlock(&m->lock);
+	lockmgr(&m->lock, LK_RELEASE);
 exit0:
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 	if (retval)
-		MIDI_DEBUG(2, printf("midi_unload: failed\n"));
+		MIDI_DEBUG(2, kprintf("midi_unload: failed\n"));
 	return retval;
 }
 
@@ -1491,13 +1553,13 @@ midimapper_open(void *arg1, void **cookie)
 	int retval = 0;
 	struct snd_midi *m;
 
-	mtx_lock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 
 	TAILQ_FOREACH(m, &midi_devs, link) {
 		retval++;
 	}
 
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 	return retval;
 }
 
@@ -1513,17 +1575,17 @@ midimapper_fetch_synth(void *arg, void *cookie, int unit)
 	struct snd_midi *m;
 	int retval = 0;
 
-	mtx_lock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_EXCLUSIVE);
 
 	TAILQ_FOREACH(m, &midi_devs, link) {
 		if (unit == retval) {
-			mtx_unlock(&midistat_lock);
+			lockmgr(&midistat_lock, LK_RELEASE);
 			return (kobj_t)m->synth;
 		}
 		retval++;
 	}
 
-	mtx_unlock(&midistat_lock);
+	lockmgr(&midistat_lock, LK_RELEASE);
 	return NULL;
 }
 
diff --git a/sys/dev/sound/midi/midiq.h b/sys/dev/sound/midi/midiq.h
index 14fb25d..733429a 100644
--- a/sys/dev/sound/midi/midiq.h
+++ b/sys/dev/sound/midi/midiq.h
@@ -58,7 +58,7 @@ struct name {                           \
  */
 #define MIDIQ_ENQ(head, buf, size) do {                                                                 \
 		if(MIDIQ_DEBUG)\
-                	printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n",            \
+                	kprintf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n",            \
 			       &(head).b[(head).h], (buf),                                        \
 			       (intmax_t)(sizeof(*(head).b) *                                     \
 					  MIN( (size), (head).s - (head).h) ),                   \
@@ -66,24 +66,24 @@ struct name {                           \
                 MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * MIN((size), (head).s - (head).h));                       \
                 if( (head).s - (head).h < (size) ) {                                                    \
 			if(MIDIQ_DEBUG) \
-                        	printf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).h, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
+                        	kprintf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).h, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
                         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; \
 		if(MIDIQ_DEBUG)\
-                	printf("#E h %d t %d\n", (head).h, (head).t);                       \
+                	kprintf("#E h %d t %d\n", (head).h, (head).t);                       \
 } while (0)
 
 #define MIDIQ_DEQ_I(head, buf, size, move, update) do {                                                                 \
 		if(MIDIQ_FULL(head)) (head).h=(head).t; \
 		if(MIDIQ_DEBUG)\
-                	printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n", &(head).b[(head).t], (buf), (intmax_t)sizeof(*(head).b) * MIN((size), (head).s - (head).t), (size), (head).h, (head).t);                       \
+                	kprintf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n", &(head).b[(head).t], (buf), (intmax_t)sizeof(*(head).b) * MIN((size), (head).s - (head).t), (size), (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(MIDIQ_DEBUG) \
-                        	printf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).t, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
+                        	kprintf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).t, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
                         if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
 		} \
 		if (update) { \
@@ -93,7 +93,7 @@ struct name {                           \
 		  if (MIDIQ_EMPTY(head)) (head).h=-1; \
 		} \
 		if(MIDIQ_DEBUG)\
-                	printf("#E h %d t %d\n", (head).h, (head).t);                       \
+                	kprintf("#E h %d t %d\n", (head).h, (head).t);                       \
 } while (0)
 
 #define MIDIQ_SIZE(head) ((head).s)
diff --git a/sys/dev/sound/midi/mpu401.c b/sys/dev/sound/midi/mpu401.c
index e567d15..7d37d9d 100644
--- a/sys/dev/sound/midi/mpu401.c
+++ b/sys/dev/sound/midi/mpu401.c
@@ -124,12 +124,12 @@ mpu401_intr(struct mpu401 *m)
 	int s;
 
 /*
-	printf("mpu401_intr\n");
+	kprintf("mpu401_intr\n");
 */
 #define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0)
 #define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0)
 #if 0
-#define D(x,l) printf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
+#define D(x,l) kprintf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"")
 #else
 #define D(x,l)
 #endif
@@ -139,7 +139,7 @@ mpu401_intr(struct mpu401 *m)
 	while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) {
 		b[i] = READ(m);
 /*
-		printf("mpu401_intr in i %d d %d\n", i, b[i]);
+		kprintf("mpu401_intr in i %d d %d\n", i, b[i]);
 */
 		i++;
 		s = STATUS(m);
@@ -150,13 +150,13 @@ mpu401_intr(struct mpu401 *m)
 	while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) {
 		if (midi_out(m->mid, b, 1)) {
 /*
-			printf("mpu401_intr out i %d d %d\n", i, b[0]);
+			kprintf("mpu401_intr out i %d d %d\n", i, b[0]);
 */
 
 			WRITE(m, *b);
 		} else {
 /*
-			printf("mpu401_intr write: no output\n");
+			kprintf("mpu401_intr write: no output\n");
 */
 			return 0;
 		}
@@ -178,14 +178,11 @@ mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
 	struct mpu401 *m;
 
 	*cb = NULL;
-	m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO);
-
-	if (!m)
-		return NULL;
+	m = kmalloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO);
 
 	kobj_init((kobj_t)m, cls);
 
-	callout_init(&m->timer, CALLOUT_MPSAFE);
+	callout_init_mp(&m->timer);
 
 	m->si = softintr;
 	m->cookie = cookie;
@@ -197,8 +194,8 @@ mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr,
 	*cb = mpu401_intr;
 	return m;
 err:
-	printf("mpu401_init error\n");
-	free(m, M_MIDI);
+	kprintf("mpu401_init error\n");
+	kfree(m, M_MIDI);
 	return NULL;
 }
 
@@ -211,7 +208,7 @@ mpu401_uninit(struct mpu401 *m)
 	retval = midi_uninit(m->mid);
 	if (retval)
 		return retval;
-	free(m, M_MIDI);
+	kfree(m, M_MIDI);
 	return 0;
 }
 
@@ -235,7 +232,7 @@ mpu401_minit(struct snd_midi *sm, void *arg)
 		CMD(m, MPU_UART);
 		return 0;
 	}
-	printf("mpu401_minit failed active sensing\n");
+	kprintf("mpu401_minit failed active sensing\n");
 	return 1;
 }
 
@@ -265,7 +262,7 @@ mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
 {
 	struct mpu401 *m = arg;
 #if 0
-	printf("mpu401_callback %s %s %s %s\n",
+	kprintf("mpu401_callback %s %s %s %s\n",
 	    flags & M_RX ? "M_RX" : "",
 	    flags & M_TX ? "M_TX" : "",
 	    flags & M_RXEN ? "M_RXEN" : "",
@@ -280,7 +277,7 @@ mpu401_mcallback(struct snd_midi *sm, void *arg, int flags)
 static void
 mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags)
 {
-/*	printf("mpu401_callbackp\n"); */
+/*	kprintf("mpu401_callbackp\n"); */
 	mpu401_mcallback(sm, arg, flags);
 }
 
diff --git a/sys/dev/sound/midi/sequencer.c b/sys/dev/sound/midi/sequencer.c
index 3747a26..71bdcac 100644
--- a/sys/dev/sound/midi/sequencer.c
+++ b/sys/dev/sound/midi/sequencer.c
@@ -53,8 +53,6 @@ __FBSDID("$FreeBSD: head/sys/dev/sound/midi/sequencer.c 274035 2014-11-03 11:11:
 #include <sys/errno.h>
 #include <sys/malloc.h>
 #include <sys/bus.h>
-#include <machine/resource.h>
-#include <machine/bus.h>
 #include <machine/clock.h>		/* for DELAY */
 #include <sys/soundcard.h>
 #include <sys/rman.h>
@@ -64,7 +62,7 @@ __FBSDID("$FreeBSD: head/sys/dev/sound/midi/sequencer.c 274035 2014-11-03 11:11:
 #include <sys/condvar.h>
 #include <sys/kthread.h>
 #include <sys/unistd.h>
-#include <sys/selinfo.h>
+#include <sys/device.h>
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_snd.h"
@@ -103,23 +101,31 @@ static d_close_t seq_close;
 static d_ioctl_t seq_ioctl;
 static d_read_t seq_read;
 static d_write_t seq_write;
-static d_poll_t seq_poll;
+static d_kqfilter_t seq_kqfilter;
 
-static struct cdevsw seq_cdevsw = {
-	.d_version = D_VERSION,
+static void	seq_filter_detach(struct knote *);
+static int	seq_filter_read(struct knote *, long);
+static int	seq_filter_write(struct knote *, long);
+
+static struct dev_ops seq_ops = {
+	{ "sequencer", 0, D_MPSAFE },
 	.d_open = seq_open,
 	.d_close = seq_close,
 	.d_read = seq_read,
 	.d_write = seq_write,
 	.d_ioctl = seq_ioctl,
-	.d_poll = seq_poll,
-	.d_name = "sequencer",
+	.d_kqfilter = seq_kqfilter,
 };
 
+static struct filterops seq_read_filterops =
+	{ FILTEROP_ISFD, NULL, seq_filter_detach, seq_filter_read };
+static struct filterops seq_write_filterops =
+	{ FILTEROP_ISFD, NULL, seq_filter_detach, seq_filter_write };
+
 struct seq_softc {
 	KOBJ_FIELDS;
 
-	struct mtx seq_lock, q_lock;
+	struct lock seq_lock, q_lock;
 	struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv;
 
 	MIDIQ_HEAD(, u_char) in_q, out_q;
@@ -132,7 +138,7 @@ struct seq_softc {
 	int	out_water;		/* Sequence output threshould */
 	snd_sync_parm sync_parm;	/* AIOSYNC parameter set */
 	struct thread *sync_thread;	/* AIOSYNCing thread */
-	struct selinfo in_sel, out_sel;
+	struct kqinfo in_kq, out_kq;
 	int	midi_number;
 	struct cdev *seqdev, *musicdev;
 	int	unit;
@@ -298,7 +304,7 @@ static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md);
 void	seq_copytoinput(struct seq_softc *scp, u_char *event, int len);
 int	seq_modevent(module_t mod, int type, void *data);
 struct seq_softc *seqs[10];
-static struct mtx seqinfo_mtx;
+static struct lock seqinfo_lock;
 static u_long nseq = 0;
 
 static void timer_start(struct seq_softc *t);
@@ -350,7 +356,7 @@ timer_wait(struct seq_softc *t, int ticks, int wait_abs)
 	unsigned long long i;
 
 	while (t->timerrun == 0) {
-		SEQ_DEBUG(2, printf("Timer wait when timer isn't running\n"));
+		SEQ_DEBUG(2, kprintf("Timer wait when timer isn't running\n"));
 		/*
 	         * The old sequencer used timeouts that only increased
 	         * the timer when the timer was running.
@@ -368,7 +374,7 @@ timer_wait(struct seq_softc *t, int ticks, int wait_abs)
 	when.tv_usec = i % 1000000;
 
 #if 0
-	printf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
+	kprintf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
 	    t->tempo, t->timerbase, ticks, wait_abs, i);
 #endif
 
@@ -379,7 +385,7 @@ timer_wait(struct seq_softc *t, int ticks, int wait_abs)
 	}
 	if (when.tv_sec < 0 || when.tv_usec < 0) {
 		SEQ_DEBUG(3,
-		    printf("seq_timer error negative time %lds.%06lds\n",
+		    kprintf("seq_timer error negative time %lds.%06lds\n",
 		    (long)when.tv_sec, (long)when.tv_usec));
 		return;
 	}
@@ -388,7 +394,7 @@ timer_wait(struct seq_softc *t, int ticks, int wait_abs)
 	i *= hz;
 	i /= 1000000ull;
 #if 0
-	printf("seq_timer usec %llu ticks %llu\n",
+	kprintf("seq_timer usec %llu ticks %llu\n",
 	    when.tv_sec * 1000000ull + when.tv_usec, i);
 #endif
 	t->waiting = 1;
@@ -396,7 +402,7 @@ timer_wait(struct seq_softc *t, int ticks, int wait_abs)
 	t->waiting = 0;
 
 	if (ret != EWOULDBLOCK)
-		SEQ_DEBUG(3, printf("seq_timer didn't timeout\n"));
+		SEQ_DEBUG(3, kprintf("seq_timer didn't timeout\n"));
 
 }
 
@@ -422,7 +428,7 @@ timer_now(struct seq_softc *t)
 
 	ret = i;
 	/*
-	 * printf("timer_now: %llu %d\n", i, ret);
+	 * kprintf("timer_now: %llu %d\n", i, ret);
 	 */
 
 	return ret;
@@ -434,8 +440,8 @@ seq_eventthread(void *arg)
 	struct seq_softc *scp = arg;
 	char event[EV_SZ];
 
-	mtx_lock(&scp->seq_lock);
-	SEQ_DEBUG(2, printf("seq_eventthread started\n"));
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
+	SEQ_DEBUG(2, kprintf("seq_eventthread started\n"));
 	while (scp->done == 0) {
 restart:
 		while (scp->playing == 0) {
@@ -457,16 +463,16 @@ restart:
 
 		if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) {
 			cv_broadcast(&scp->out_cv);
-			selwakeup(&scp->out_sel);
+			KNOTE(&scp->out_kq.ki_note, 0);
 		}
 		seq_processevent(scp, event);
 	}
 
 done:
 	cv_broadcast(&scp->th_cv);
-	mtx_unlock(&scp->seq_lock);
-	SEQ_DEBUG(2, printf("seq_eventthread finished\n"));
-	kproc_exit(0);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
+	SEQ_DEBUG(2, kprintf("seq_eventthread finished\n"));
+	kthread_exit();
 }
 
 /*
@@ -490,11 +496,11 @@ seq_processevent(struct seq_softc *scp, u_char *event)
 		    event[0] != EV_SYSEX &&
 	    event[0] != SEQ_MIDIPUTC) {
 		ret = 1;
-		SEQ_DEBUG(2, printf("seq_processevent not known %d\n",
+		SEQ_DEBUG(2, kprintf("seq_processevent not known %d\n",
 		    event[0]));
 	} else if (seq_fetch_mid(scp, event[1], &m) != 0) {
 		ret = 1;
-		SEQ_DEBUG(2, printf("seq_processevent midi unit not found %d\n",
+		SEQ_DEBUG(2, kprintf("seq_processevent midi unit not found %d\n",
 		    event[1]));
 	} else
 		switch (event[0]) {
@@ -508,9 +514,9 @@ seq_processevent(struct seq_softc *scp, u_char *event)
 			ret = seq_sysex(scp, m, event);
 			break;
 		case SEQ_MIDIPUTC:
-			mtx_unlock(&scp->seq_lock);
+			lockmgr(&scp->seq_lock, LK_RELEASE);
 			ret = SYNTH_WRITERAW(m, &event[2], 1);
-			mtx_lock(&scp->seq_lock);
+			lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 			break;
 		}
 	return ret;
@@ -525,33 +531,22 @@ seq_addunit(void)
 
 	/* Allocate the softc. */
 	ret = ENOMEM;
-	scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO);
-	if (scp == NULL) {
-		SEQ_DEBUG(1, printf("seq_addunit: softc allocation failed.\n"));
-		goto err;
-	}
+	scp = kmalloc(sizeof(*scp), M_DEVBUF, M_WAITOK | M_ZERO);
 	kobj_init((kobj_t)scp, &sequencer_class);
 
-	buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
-	if (buf == NULL)
-		goto err;
+	buf = kmalloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_WAITOK | M_ZERO);
 	MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024);
-	buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
-	if (buf == NULL)
-		goto err;
+	buf = kmalloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_WAITOK | M_ZERO);
 	MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024);
 	ret = EINVAL;
 
-	scp->midis = malloc(sizeof(kobj_t) * 32, M_TEMP, M_NOWAIT | M_ZERO);
-	scp->midi_flags = malloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
-	    M_NOWAIT | M_ZERO);
-
-	if (scp->midis == NULL || scp->midi_flags == NULL)
-		goto err;
+	scp->midis = kmalloc(sizeof(kobj_t) * 32, M_TEMP, M_WAITOK | M_ZERO);
+	scp->midi_flags = kmalloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
+	    M_WAITOK | M_ZERO);
 
 	scp->flags = 0;
 
-	mtx_init(&scp->seq_lock, "seqflq", NULL, 0);
+	lockinit(&scp->seq_lock, "seqflq", 0, LK_CANRECURSE);
 	cv_init(&scp->state_cv, "seqstate");
 	cv_init(&scp->empty_cv, "seqempty");
 	cv_init(&scp->reset_cv, "seqtimer");
@@ -567,11 +562,11 @@ seq_addunit(void)
 	if (scp->mapper == NULL)
 		goto err;
 
-	scp->seqdev = make_dev(&seq_cdevsw,
+	scp->seqdev = make_dev(&seq_ops,
 	    MIDIMKMINOR(scp->unit, SND_DEV_SEQ, 0), UID_ROOT,
 	    GID_WHEEL, 0666, "sequencer%d", scp->unit);
 
-	scp->musicdev = make_dev(&seq_cdevsw,
+	scp->musicdev = make_dev(&seq_ops,
 	    MIDIMKMINOR(scp->unit, SND_DEV_MUSIC, 0), UID_ROOT,
 	    GID_WHEEL, 0666, "music%d", scp->unit);
 
@@ -582,8 +577,8 @@ seq_addunit(void)
 	 */
 
 	ret =
-	    kproc_create
-	    (seq_eventthread, scp, NULL, RFHIGHPID, 0,
+	    kthread_create
+	    (seq_eventthread, scp, NULL,
 	    "sequencer %02d", scp->unit);
 
 	if (ret)
@@ -591,13 +586,13 @@ seq_addunit(void)
 
 	scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp;
 
-	SEQ_DEBUG(2, printf("sequencer %d created scp %p\n", scp->unit, scp));
+	SEQ_DEBUG(2, kprintf("sequencer %d created scp %p\n", scp->unit, scp));
 
 	ret = 0;
 
-	mtx_lock(&seqinfo_mtx);
+	lockmgr(&seqinfo_lock, LK_EXCLUSIVE);
 	seqs[nseq++] = scp;
-	mtx_unlock(&seqinfo_mtx);
+	lockmgr(&seqinfo_lock, LK_RELEASE);
 
 	goto ok;
 
@@ -611,14 +606,14 @@ err:
 	         * TODO: Destroy mutex and cv
 	         */
 		if (scp->midis != NULL)
-			free(scp->midis, M_TEMP);
+			kfree(scp->midis, M_TEMP);
 		if (scp->midi_flags != NULL)
-			free(scp->midi_flags, M_TEMP);
+			kfree(scp->midi_flags, M_TEMP);
 		if (scp->out_q.b)
-			free(scp->out_q.b, M_TEMP);
+			kfree(scp->out_q.b, M_TEMP);
 		if (scp->in_q.b)
-			free(scp->in_q.b, M_TEMP);
-		free(scp, M_DEVBUF);
+			kfree(scp->in_q.b, M_TEMP);
+		kfree(scp, M_DEVBUF);
 	}
 ok:
 	return ret;
@@ -630,62 +625,62 @@ seq_delunit(int unit)
 	struct seq_softc *scp = seqs[unit];
 	int i;
 
-	//SEQ_DEBUG(4, printf("seq_delunit: %d\n", unit));
-	SEQ_DEBUG(1, printf("seq_delunit: 1 \n"));
-	mtx_lock(&scp->seq_lock);
+	//SEQ_DEBUG(4, kprintf("seq_delunit: %d\n", unit));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 1 \n"));
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 
 	scp->playing = 0;
 	scp->done = 1;
 	cv_broadcast(&scp->out_cv);
 	cv_broadcast(&scp->state_cv);
 	cv_broadcast(&scp->reset_cv);
-	SEQ_DEBUG(1, printf("seq_delunit: 2 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 2 \n"));
 	cv_wait(&scp->th_cv, &scp->seq_lock);
-	SEQ_DEBUG(1, printf("seq_delunit: 3.0 \n"));
-	mtx_unlock(&scp->seq_lock);
-	SEQ_DEBUG(1, printf("seq_delunit: 3.1 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 3.0 \n"));
+	lockmgr(&scp->seq_lock, LK_RELEASE);
+	SEQ_DEBUG(1, kprintf("seq_delunit: 3.1 \n"));
 
 	cv_destroy(&scp->state_cv);
-	SEQ_DEBUG(1, printf("seq_delunit: 4 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 4 \n"));
 	cv_destroy(&scp->empty_cv);
-	SEQ_DEBUG(1, printf("seq_delunit: 5 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 5 \n"));
 	cv_destroy(&scp->reset_cv);
-	SEQ_DEBUG(1, printf("seq_delunit: 6 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 6 \n"));
 	cv_destroy(&scp->out_cv);
-	SEQ_DEBUG(1, printf("seq_delunit: 7 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 7 \n"));
 	cv_destroy(&scp->in_cv);
-	SEQ_DEBUG(1, printf("seq_delunit: 8 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 8 \n"));
 	cv_destroy(&scp->th_cv);
 
-	SEQ_DEBUG(1, printf("seq_delunit: 10 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 10 \n"));
 	if (scp->seqdev)
 		destroy_dev(scp->seqdev);
-	SEQ_DEBUG(1, printf("seq_delunit: 11 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 11 \n"));
 	if (scp->musicdev)
 		destroy_dev(scp->musicdev);
-	SEQ_DEBUG(1, printf("seq_delunit: 12 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 12 \n"));
 	scp->seqdev = scp->musicdev = NULL;
 	if (scp->midis != NULL)
-		free(scp->midis, M_TEMP);
-	SEQ_DEBUG(1, printf("seq_delunit: 13 \n"));
+		kfree(scp->midis, M_TEMP);
+	SEQ_DEBUG(1, kprintf("seq_delunit: 13 \n"));
 	if (scp->midi_flags != NULL)
-		free(scp->midi_flags, M_TEMP);
-	SEQ_DEBUG(1, printf("seq_delunit: 14 \n"));
-	free(scp->out_q.b, M_TEMP);
-	SEQ_DEBUG(1, printf("seq_delunit: 15 \n"));
-	free(scp->in_q.b, M_TEMP);
+		kfree(scp->midi_flags, M_TEMP);
+	SEQ_DEBUG(1, kprintf("seq_delunit: 14 \n"));
+	kfree(scp->out_q.b, M_TEMP);
+	SEQ_DEBUG(1, kprintf("seq_delunit: 15 \n"));
+	kfree(scp->in_q.b, M_TEMP);
 
-	SEQ_DEBUG(1, printf("seq_delunit: 16 \n"));
+	SEQ_DEBUG(1, kprintf("seq_delunit: 16 \n"));
 
-	mtx_destroy(&scp->seq_lock);
-	SEQ_DEBUG(1, printf("seq_delunit: 17 \n"));
-	free(scp, M_DEVBUF);
+	lockuninit(&scp->seq_lock);
+	SEQ_DEBUG(1, kprintf("seq_delunit: 17 \n"));
+	kfree(scp, M_DEVBUF);
 
-	mtx_lock(&seqinfo_mtx);
+	lockmgr(&seqinfo_lock, LK_EXCLUSIVE);
 	for (i = unit; i < (nseq - 1); i++)
 		seqs[i] = seqs[i + 1];
 	nseq--;
-	mtx_unlock(&seqinfo_mtx);
+	lockmgr(&seqinfo_lock, LK_RELEASE);
 
 	return 0;
 }
@@ -699,7 +694,7 @@ seq_modevent(module_t mod, int type, void *data)
 
 	switch (type) {
 	case MOD_LOAD:
-		mtx_init(&seqinfo_mtx, "seqmod", NULL, 0);
+		lockinit(&seqinfo_lock, "seqmod", 0, LK_CANRECURSE);
 		retval = seq_addunit();
 		break;
 
@@ -713,7 +708,7 @@ seq_modevent(module_t mod, int type, void *data)
 		}
 		if (nseq == 0) {
 			retval = 0;
-			mtx_destroy(&seqinfo_mtx);
+			lockuninit(&seqinfo_lock);
 		}
 		break;
 
@@ -737,25 +732,27 @@ seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md)
 }
 
 int
-seq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
+seq_open(struct dev_open_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	int flags = ap->a_oflags;
 	struct seq_softc *scp = i_dev->si_drv1;
 	int i;
 
 	if (scp == NULL)
 		return ENXIO;
 
-	SEQ_DEBUG(3, printf("seq_open: scp %p unit %d, flags 0x%x.\n",
+	SEQ_DEBUG(3, kprintf("seq_open: scp %p unit %d, flags 0x%x.\n",
 	    scp, scp->unit, flags));
 
 	/*
 	 * Mark this device busy.
 	 */
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	if (scp->busy) {
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(2, printf("seq_open: unit %d is busy.\n", scp->unit));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(2, kprintf("seq_open: unit %d is busy.\n", scp->unit));
 		return EBUSY;
 	}
 	scp->fflags = flags;
@@ -772,7 +769,7 @@ seq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie);
 
 	if (scp->maxunits == 0)
-		SEQ_DEBUG(2, printf("seq_open: no midi devices\n"));
+		SEQ_DEBUG(2, kprintf("seq_open: no midi devices\n"));
 
 	for (i = 0; i < scp->maxunits; i++) {
 		scp->midis[scp->midi_number] =
@@ -803,15 +800,15 @@ seq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	scp->out_water = MIDIQ_SIZE(scp->out_q) / 2;
 
 	scp->busy = 1;
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 
-	SEQ_DEBUG(2, printf("seq_open: opened, mode %s.\n",
+	SEQ_DEBUG(2, kprintf("seq_open: opened, mode %s.\n",
 	    scp->music ? "music" : "sequencer"));
 	SEQ_DEBUG(2,
-	    printf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
+	    kprintf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
 		scp->unit, scp, scp->maxunits, scp->midi_number));
 	for (i = 0; i < scp->midi_number; i++)
-		SEQ_DEBUG(3, printf("  midi %d %p\n", i, scp->midis[i]));
+		SEQ_DEBUG(3, kprintf("  midi %d %p\n", i, scp->midis[i]));
 
 	return 0;
 }
@@ -820,8 +817,9 @@ seq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
  * seq_close
  */
 int
-seq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
+seq_close(struct dev_close_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
 	int i;
 	struct seq_softc *scp = i_dev->si_drv1;
 	int ret;
@@ -829,9 +827,9 @@ seq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	if (scp == NULL)
 		return ENXIO;
 
-	SEQ_DEBUG(2, printf("seq_close: unit %d.\n", scp->unit));
+	SEQ_DEBUG(2, kprintf("seq_close: unit %d.\n", scp->unit));
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 
 	ret = ENXIO;
 	if (scp->busy == 0)
@@ -852,14 +850,17 @@ seq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
 	ret = 0;
 
 err:
-	SEQ_DEBUG(3, printf("seq_close: closed ret = %d.\n", ret));
-	mtx_unlock(&scp->seq_lock);
+	SEQ_DEBUG(3, kprintf("seq_close: closed ret = %d.\n", ret));
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 	return ret;
 }
 
 int
-seq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
+seq_read(struct dev_read_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	struct uio *uio = ap->a_uio;
+	int ioflag = ap->a_ioflag;
 	int retval, used;
 	struct seq_softc *scp = i_dev->si_drv1;
 
@@ -869,12 +870,12 @@ seq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 	if (scp == NULL)
 		return ENXIO;
 
-	SEQ_DEBUG(7, printf("seq_read: unit %d, resid %zd.\n",
+	SEQ_DEBUG(7, kprintf("seq_read: unit %d, resid %zd.\n",
 	    scp->unit, uio->uio_resid));
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	if ((scp->fflags & FREAD) == 0) {
-		SEQ_DEBUG(2, printf("seq_read: unit %d is not for reading.\n",
+		SEQ_DEBUG(2, kprintf("seq_read: unit %d is not for reading.\n",
 		    scp->unit));
 		retval = EIO;
 		goto err1;
@@ -917,7 +918,7 @@ seq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 		used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid);
 		used = MIN(used, SEQ_RSIZE);
 
-		SEQ_DEBUG(8, printf("midiread: uiomove cc=%d\n", used));
+		SEQ_DEBUG(8, kprintf("midiread: uiomove cc=%d\n", used));
 		MIDIQ_DEQ(scp->in_q, buf, used);
 		retval = uiomove(buf, used, uio);
 		if (retval)
@@ -926,31 +927,34 @@ seq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
 
 	retval = 0;
 err1:
-	mtx_unlock(&scp->seq_lock);
-	SEQ_DEBUG(6, printf("seq_read: ret %d, resid %zd.\n",
+	lockmgr(&scp->seq_lock, LK_RELEASE);
+	SEQ_DEBUG(6, kprintf("seq_read: ret %d, resid %zd.\n",
 	    retval, uio->uio_resid));
 
 	return retval;
 }
 
 int
-seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
+seq_write(struct dev_write_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	struct uio *uio = ap->a_uio;
+	int ioflag = ap->a_ioflag;
 	u_char event[EV_SZ], newevent[EV_SZ], ev_code;
 	struct seq_softc *scp = i_dev->si_drv1;
 	int retval;
 	int used;
 
-	SEQ_DEBUG(7, printf("seq_write: unit %d, resid %zd.\n",
+	SEQ_DEBUG(7, kprintf("seq_write: unit %d, resid %zd.\n",
 	    scp->unit, uio->uio_resid));
 
 	if (scp == NULL)
 		return ENXIO;
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 
 	if ((scp->fflags & FWRITE) == 0) {
-		SEQ_DEBUG(2, printf("seq_write: unit %d is not for writing.\n",
+		SEQ_DEBUG(2, kprintf("seq_write: unit %d is not for writing.\n",
 		    scp->unit));
 		retval = EIO;
 		goto err0;
@@ -962,7 +966,7 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 				goto err0;
 			if (ioflag & O_NONBLOCK)
 				goto err0;
-			SEQ_DEBUG(8, printf("seq_write cvwait\n"));
+			SEQ_DEBUG(8, kprintf("seq_write cvwait\n"));
 
 			scp->playing = 1;
 			cv_broadcast(&scp->out_cv);
@@ -986,7 +990,7 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 
 		used = MIN(uio->uio_resid, 4);
 
-		SEQ_DEBUG(8, printf("seqout: resid %zd len %jd avail %jd\n",
+		SEQ_DEBUG(8, kprintf("seqout: resid %zd len %jd avail %jd\n",
 		    uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q),
 		    (intmax_t)MIDIQ_AVAIL(scp->out_q)));
 
@@ -999,7 +1003,7 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 			goto err0;
 
 		ev_code = event[0];
-		SEQ_DEBUG(8, printf("seq_write: unit %d, event %s.\n",
+		SEQ_DEBUG(8, kprintf("seq_write: unit %d, event %s.\n",
 		    scp->unit, midi_cmdname(ev_code, cmdtab_seqevent)));
 
 		/* Have a look at the event code. */
@@ -1014,13 +1018,13 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 			 * synthesizer.
 			 */
 			midiunit = *(u_short *)&event[2];
-			mtx_lock(&sd->seq_lock);
+			lockmgr(&sd->seq_lock, LK_EXCLUSIVE);
 			ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
-			mtx_unlock(&sd->seq_lock);
+			lockmgr(&sd->seq_lock, LK_RELEASE);
 			if (ret != 0)
 				return (ret);
 
-			SEQ_DEBUG(printf("seq_write: loading a patch to the unit %d.\n", midiunit));
+			SEQ_DEBUG(kprintf("seq_write: loading a patch to the unit %d.\n", midiunit));
 
 			ret = md->synth.loadpatch(md, *(short *)&event[0], buf,
 			    p + 4, count, 0);
@@ -1030,7 +1034,7 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 			 * For now, just flush the darn buffer
 			 */
 			SEQ_DEBUG(2,
-			   printf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
+			   kprintf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
 			while (uio->uio_resid > 0) {
 				retval = uiomove(event, EV_SZ, uio);
 				if (retval)
@@ -1049,12 +1053,12 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 			 * bytes. scoop extra info.
 			 */
 			if (scp->music && ev_code == SEQ_EXTENDED) {
-				SEQ_DEBUG(2, printf("seq_write: invalid level two event %x.\n", ev_code));
+				SEQ_DEBUG(2, kprintf("seq_write: invalid level two event %x.\n", ev_code));
 				goto err0;
 			}
 			if (uiomove((caddr_t)&event[4], 4, uio)) {
 				SEQ_DEBUG(2,
-				   printf("seq_write: user memory mangled?\n"));
+				   kprintf("seq_write: user memory mangled?\n"));
 				goto err0;
 			}
 		} else {
@@ -1062,7 +1066,7 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 			 * Size four event.
 			 */
 			if (scp->music) {
-				SEQ_DEBUG(2, printf("seq_write: four byte event in music mode.\n"));
+				SEQ_DEBUG(2, kprintf("seq_write: four byte event in music mode.\n"));
 				goto err0;
 			}
 		}
@@ -1106,16 +1110,19 @@ seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
 
 err0:
 	SEQ_DEBUG(6,
-	    printf("seq_write done: leftover buffer length %zd retval %d\n",
+	    kprintf("seq_write done: leftover buffer length %zd retval %d\n",
 	    uio->uio_resid, retval));
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 	return retval;
 }
 
 int
-seq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
-    struct thread *td)
+seq_ioctl(struct dev_ioctl_args *ap)
 {
+	cdev_t i_dev = ap->a_head.a_dev;
+	u_long cmd = ap->a_cmd;
+	caddr_t arg = ap->a_data;
+	int mode = ap->a_fflag;
 	int midiunit, ret, tmp;
 	struct seq_softc *scp = i_dev->si_drv1;
 	struct synth_info *synthinfo;
@@ -1132,7 +1139,7 @@ seq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
 	if (scp == NULL)
 		return ENXIO;
 
-	SEQ_DEBUG(6, printf("seq_ioctl: unit %d, cmd %s.\n",
+	SEQ_DEBUG(6, kprintf("seq_ioctl: unit %d, cmd %s.\n",
 	    scp->unit, midi_cmdname(cmd, cmdtab_seqioctl)));
 
 	ret = 0;
@@ -1142,10 +1149,10 @@ seq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
 		/*
 		 * ioctl needed by libtse
 		 */
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		*(int *)arg = timer_now(scp);
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(6, printf("seq_ioctl: gettime %d.\n", *(int *)arg));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(6, kprintf("seq_ioctl: gettime %d.\n", *(int *)arg));
 		ret = 0;
 		break;
 	case SNDCTL_TMR_METRONOME:
@@ -1180,18 +1187,18 @@ seq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
 		event[1] = TMR_CONTINUE;
 timerevent:
 		event[0] = EV_TIMING;
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		if (!scp->music) {
 			ret = EINVAL;
-			mtx_unlock(&scp->seq_lock);
+			lockmgr(&scp->seq_lock, LK_RELEASE);
 			break;
 		}
 		seq_processevent(scp, event);
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		break;
 	case SNDCTL_TMR_SELECT:
 		SEQ_DEBUG(2,
-		    printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
+		    kprintf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
 		ret = EINVAL;
 		break;
 	case SNDCTL_SEQ_SYNC:
@@ -1199,9 +1206,9 @@ timerevent:
 			ret = 0;
 			break;
 		}
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		ret = seq_sync(scp);
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		break;
 	case SNDCTL_SEQ_PANIC:
 		/* fallthrough */
@@ -1209,28 +1216,28 @@ timerevent:
 		/*
 		 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
 		 */
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		seq_reset(scp);
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		ret = 0;
 		break;
 	case SNDCTL_SEQ_TESTMIDI:
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		/*
 		 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
 		 * device?".
 		 */
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		break;
 #if 0
 	case SNDCTL_SEQ_GETINCOUNT:
 		if (mode == O_WRONLY)
 			*(int *)arg = 0;
 		else {
-			mtx_lock(&scp->seq_lock);
+			lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 			*(int *)arg = scp->in_q.rl;
-			mtx_unlock(&scp->seq_lock);
-			SEQ_DEBUG(printf("seq_ioctl: incount %d.\n",
+			lockmgr(&scp->seq_lock, LK_RELEASE);
+			SEQ_DEBUG(kprintf("seq_ioctl: incount %d.\n",
 			    *(int *)arg));
 		}
 		ret = 0;
@@ -1239,10 +1246,10 @@ timerevent:
 		if (mode == O_RDONLY)
 			*(int *)arg = 0;
 		else {
-			mtx_lock(&scp->seq_lock);
+			lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 			*(int *)arg = scp->out_q.fl;
-			mtx_unlock(&scp->seq_lock);
-			SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n",
+			lockmgr(&scp->seq_lock, LK_RELEASE);
+			SEQ_DEBUG(kprintf("seq_ioctl: outcount %d.\n",
 			    *(int *)arg));
 		}
 		ret = 0;
@@ -1253,10 +1260,10 @@ timerevent:
 			ret = EINVAL;
 			break;
 		}
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		*(int *)arg = scp->timerbase;
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(3, printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(3, kprintf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
 		ret = 0;
 		break;
 		/*
@@ -1264,9 +1271,9 @@ timerevent:
 		 */
 #if 0
 	case SNDCTL_SEQ_RESETSAMPLES:
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		if (ret != 0)
 			break;
 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
@@ -1274,14 +1281,14 @@ timerevent:
 		break;
 #endif
 	case SNDCTL_SEQ_NRSYNTHS:
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		*(int *)arg = scp->midi_number;
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(3, printf("seq_ioctl: synths %d.\n", *(int *)arg));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(3, kprintf("seq_ioctl: synths %d.\n", *(int *)arg));
 		ret = 0;
 		break;
 	case SNDCTL_SEQ_NRMIDIS:
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		if (scp->music)
 			*(int *)arg = 0;
 		else {
@@ -1290,8 +1297,8 @@ timerevent:
 		         */
 			*(int *)arg = scp->midi_number;
 		}
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(3, printf("seq_ioctl: midis %d.\n", *(int *)arg));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(3, kprintf("seq_ioctl: midis %d.\n", *(int *)arg));
 		ret = 0;
 		break;
 		/*
@@ -1299,9 +1306,9 @@ timerevent:
 		 */
 #if 0
 	case SNDCTL_SYNTH_MEMAVL:
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		if (ret != 0)
 			break;
 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
@@ -1312,7 +1319,7 @@ timerevent:
 		for (ret = 0; ret < EV_SZ; ret++)
 			event[ret] = (u_char)arg[0];
 
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		if (scp->music)
 			ret = seq_processevent(scp, event);
 		else {
@@ -1321,12 +1328,12 @@ timerevent:
 			else
 				ret = EINVAL;
 		}
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		break;
 	case SNDCTL_SYNTH_INFO:
 		synthinfo = (struct synth_info *)arg;
 		midiunit = synthinfo->device;
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		if (seq_fetch_mid(scp, midiunit, &md) == 0) {
 			bzero(synthinfo, sizeof(*synthinfo));
 			synthinfo->name[0] = 'f';
@@ -1344,12 +1351,12 @@ timerevent:
 			ret = 0;
 		} else
 			ret = EINVAL;
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		break;
 	case SNDCTL_MIDI_INFO:
 		midiinfo = (struct midi_info *)arg;
 		midiunit = midiinfo->device;
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		if (seq_fetch_mid(scp, midiunit, &md) == 0) {
 			bzero(midiinfo, sizeof(*midiinfo));
 			midiinfo->name[0] = 'f';
@@ -1369,25 +1376,25 @@ timerevent:
 			ret = 0;
 		} else
 			ret = EINVAL;
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		break;
 	case SNDCTL_SEQ_THRESHOLD:
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
 		scp->out_water = *(int *)arg;
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(3, printf("seq_ioctl: water %d.\n", *(int *)arg));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(3, kprintf("seq_ioctl: water %d.\n", *(int *)arg));
 		ret = 0;
 		break;
 	case SNDCTL_MIDI_PRETIME:
 		tmp = *(int *)arg;
 		if (tmp < 0)
 			tmp = 0;
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 		scp->pre_event_timeout = (hz * tmp) / 10;
 		*(int *)arg = scp->pre_event_timeout;
-		mtx_unlock(&scp->seq_lock);
-		SEQ_DEBUG(3, printf("seq_ioctl: pretime %d.\n", *(int *)arg));
+		lockmgr(&scp->seq_lock, LK_RELEASE);
+		SEQ_DEBUG(3, kprintf("seq_ioctl: pretime %d.\n", *(int *)arg));
 		ret = 0;
 		break;
 	case SNDCTL_FM_4OP_ENABLE:
@@ -1408,7 +1415,7 @@ timerevent:
 		 * Then pass on the ioctl to device 0
 		 */
 		SEQ_DEBUG(2,
-		    printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
+		    kprintf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
 		ret = EINVAL;
 		break;
 	}
@@ -1417,49 +1424,103 @@ timerevent:
 }
 
 int
-seq_poll(struct cdev *i_dev, int events, struct thread *td)
+seq_kqfilter(struct dev_kqfilter_args *ap)
 {
-	int ret, lim;
-	struct seq_softc *scp = i_dev->si_drv1;
+	cdev_t dev = ap->a_head.a_dev;
+	struct knote *kn = ap->a_kn;
+	struct seq_softc *scp;
+	struct klist *klist;
+
+	ap->a_result = 0;
+	scp = dev->si_drv1;
 
-	SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit));
-	SEQ_DEBUG(1, printf("seq_poll: unit %d.\n", scp->unit));
+	switch (kn->kn_filter) {
+	case EVFILT_READ:
+		kn->kn_fop = &seq_read_filterops;
+		kn->kn_hook = (caddr_t)scp;
+		klist = &scp->in_kq.ki_note;
+		break;
+	case EVFILT_WRITE:
+		kn->kn_fop = &seq_write_filterops;
+		kn->kn_hook = (caddr_t)scp;
+		klist = &scp->out_kq.ki_note;
+		break;
+	default:
+		ap->a_result = EOPNOTSUPP;
+		return (0);
+	}
 
-	mtx_lock(&scp->seq_lock);
+	knote_insert(klist, kn);
 
-	ret = 0;
+	return(0);
+}
+
+static void
+seq_filter_detach(struct knote *kn)
+{
+	struct seq_softc *scp = (struct seq_softc *)kn->kn_hook;
+	struct klist *in_klist = &scp->in_kq.ki_note;
+	struct klist *out_klist = &scp->out_kq.ki_note;
+
+	knote_remove(in_klist, kn);
+	knote_remove(out_klist, kn);
+}
+
+static int
+seq_filter_read(struct knote *kn, long hint)
+{
+	struct seq_softc *scp = (struct seq_softc *)kn->kn_hook;
+	int ready = 0;
+	int lim;
+
+	SEQ_DEBUG(3, kprintf("seq_filter_read: unit %d.\n", scp->unit));
+	SEQ_DEBUG(1, kprintf("seq_filter_read: unit %d.\n", scp->unit));
+
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 
 	/* Look up the apropriate queue and select it. */
-	if ((events & (POLLOUT | POLLWRNORM)) != 0) {
-		/* Start playing. */
-		scp->playing = 1;
-		cv_broadcast(&scp->state_cv);
-		cv_broadcast(&scp->out_cv);
-
-		lim = scp->out_water;
-
-		if (MIDIQ_AVAIL(scp->out_q) < lim)
-			/* No enough space, record select. */
-			selrecord(td, &scp->out_sel);
-		else
-			/* We can write now. */
-			ret |= events & (POLLOUT | POLLWRNORM);
+
+	/* TODO: Start recording. */
+
+	/* Find out the boundary. */
+	lim = 1;
+	if (MIDIQ_LEN(scp->in_q) >= lim) {
+		/* We can read now. */
+		ready = 1;
 	}
-	if ((events & (POLLIN | POLLRDNORM)) != 0) {
-		/* TODO: Start recording. */
-
-		/* Find out the boundary. */
-		lim = 1;
-		if (MIDIQ_LEN(scp->in_q) < lim)
-			/* No data ready, record select. */
-			selrecord(td, &scp->in_sel);
-		else
-			/* We can read now. */
-			ret |= events & (POLLIN | POLLRDNORM);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
+
+	return (ready);
+}
+
+static int
+seq_filter_write(struct knote *kn, long hint)
+{
+	struct seq_softc *scp = (struct seq_softc *)kn->kn_hook;
+	int ready = 0;
+	int lim;
+
+	SEQ_DEBUG(3, kprintf("seq_filter_write: unit %d.\n", scp->unit));
+	SEQ_DEBUG(1, kprintf("seq_filter_write: unit %d.\n", scp->unit));
+
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
+
+	/* Look up the apropriate queue and select it. */
+
+	/* Start playing. */
+	scp->playing = 1;
+	cv_broadcast(&scp->state_cv);
+	cv_broadcast(&scp->out_cv);
+
+	lim = scp->out_water;
+
+	if (MIDIQ_AVAIL(scp->out_q) >= lim) {
+		/* We can write now. */
+		ready = 1;
 	}
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 
-	return (ret);
+	return (ready);
 }
 
 #if 0
@@ -1470,7 +1531,7 @@ sein_qtr(void *p, void /* mididev_info */ *md)
 
 	scp = (struct seq_softc *)p;
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 
 	/* Restart playing if we have the data to output. */
 	if (scp->queueout_pending)
@@ -1479,7 +1540,7 @@ sein_qtr(void *p, void /* mididev_info */ *md)
 	if ((scp->flags & SEQ_F_READING) != 0)
 		seq_midiinput(scp, md);
 
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 }
 
 #endif
@@ -1564,13 +1625,13 @@ restart:
 		out[3] = chn;
 		out[4] = note;
 		out[5] = vel;
-		SEQ_DEBUG(4,printf("seq_playevent: synctimer\n"));
+		SEQ_DEBUG(4,kprintf("seq_playevent: synctimer\n"));
 		break;
 */
 
 	case SEQ_MIDIPUTC:
 		SEQ_DEBUG(4,
-		    printf("seq_playevent: put data 0x%02x, unit %d.\n",
+		    kprintf("seq_playevent: put data 0x%02x, unit %d.\n",
 		    event[1], event[2]));
 		/*
 		 * Pass through to the midi device.
@@ -1623,7 +1684,7 @@ restart:
 			 */
 		default:
 			SEQ_DEBUG(2,
-			    printf("seq_convertold: SEQ_EXTENDED type %d"
+			    kprintf("seq_convertold: SEQ_EXTENDED type %d"
 			    "not handled\n", event[1]));
 			break;
 		}
@@ -1635,7 +1696,7 @@ restart:
 		out[5] = event[3];
 		out[6] = event[4];
 
-		SEQ_DEBUG(5, printf("SEQ_WAIT %d",
+		SEQ_DEBUG(5, kprintf("SEQ_WAIT %d",
 		    event[2] + (event[3] << 8) + (event[4] << 24)));
 
 		used += 4;
@@ -1646,7 +1707,7 @@ restart:
 	case SEQ_PRIVATE:
 	default:
 		SEQ_DEBUG(2,
-		  printf("seq_convertold: event type %d not handled %d %d %d\n",
+		  kprintf("seq_convertold: event type %d not handled %d %d %d\n",
 		    event[0], event[1], event[2], event[3]));
 		break;
 	}
@@ -1661,16 +1722,16 @@ void
 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
 {
 
-	mtx_assert(&scp->seq_lock, MA_OWNED);
+	KKASSERT(lockowned(&scp->seq_lock));
 
 	if (MIDIQ_AVAIL(scp->in_q) < len) {
 		/*
 	         * ENOROOM?  EINPUTDROPPED? ETOUGHLUCK?
 	         */
-		SEQ_DEBUG(2, printf("seq_copytoinput: queue full\n"));
+		SEQ_DEBUG(2, kprintf("seq_copytoinput: queue full\n"));
 	} else {
 		MIDIQ_ENQ(scp->in_q, event, len);
-		selwakeup(&scp->in_sel);
+		KNOTE(&scp->in_kq.ki_note, 0);
 		cv_broadcast(&scp->in_cv);
 	}
 
@@ -1688,15 +1749,15 @@ seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
 	note = event[4];
 	parm = event[5];
 
-	mtx_assert(&scp->seq_lock, MA_OWNED);
+	KKASSERT(lockowned(&scp->seq_lock));
 
-	SEQ_DEBUG(5, printf("seq_chnvoice: unit %d, dev %d, cmd %s,"
+	SEQ_DEBUG(5, kprintf("seq_chnvoice: unit %d, dev %d, cmd %s,"
 	    " chn %d, note %d, parm %d.\n", scp->unit, event[1],
 	    midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
 
 	voice = SYNTH_ALLOC(md, chn, note);
 
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 
 	switch (cmd) {
 	case MIDI_NOTEON:
@@ -1708,26 +1769,26 @@ seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
 				 * number is the patch number.
 				 */
 				/*
-				mtx_unlock(&scp->seq_lock);
+				lockmgr(&scp->seq_lock, LK_RELEASE);
 				if (SYNTH_SETINSTR(md, voice, 128 + note)
 				    == EAGAIN) {
-					mtx_lock(&scp->seq_lock);
+					lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 					return (QUEUEFULL);
 				}
-				mtx_lock(&scp->seq_lock);
+				lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 				*/
 				note = 60;	/* Middle C. */
 			}
 #endif
 			if (scp->music) {
 				/*
-				mtx_unlock(&scp->seq_lock);
+				lockmgr(&scp->seq_lock, LK_RELEASE);
 				if (SYNTH_SETUPVOICE(md, voice, chn)
 				    == EAGAIN) {
-					mtx_lock(&scp->seq_lock);
+					lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 					return (QUEUEFULL);
 				}
-				mtx_lock(&scp->seq_lock);
+				lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 				*/
 			}
 			SYNTH_STARTNOTE(md, voice, note, parm);
@@ -1741,12 +1802,12 @@ seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
 		break;
 	default:
 		ret = 1;
-		SEQ_DEBUG(2, printf("seq_chnvoice event type %d not handled\n",
+		SEQ_DEBUG(2, kprintf("seq_chnvoice event type %d not handled\n",
 		    event[1]));
 		break;
 	}
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	return ret;
 }
 
@@ -1763,18 +1824,18 @@ seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
 	p1 = event[4];
 	w14 = *(u_short *)&event[6];
 
-	SEQ_DEBUG(5, printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
+	SEQ_DEBUG(5, kprintf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
 	    " p1 %d, w14 %d.\n", scp->unit, event[1],
 	    midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 	switch (cmd) {
 	case MIDI_PGM_CHANGE:
-		SEQ_DEBUG(4, printf("seq_chncommon pgmchn chn %d pg %d\n",
+		SEQ_DEBUG(4, kprintf("seq_chncommon pgmchn chn %d pg %d\n",
 		    chn, p1));
 		SYNTH_SETINSTR(md, chn, p1);
 		break;
 	case MIDI_CTL_CHANGE:
-		SEQ_DEBUG(4, printf("seq_chncommon ctlch chn %d pg %d %d\n",
+		SEQ_DEBUG(4, kprintf("seq_chncommon ctlch chn %d pg %d %d\n",
 		    chn, p1, w14));
 		SYNTH_CONTROLLER(md, chn, p1, w14);
 		break;
@@ -1784,7 +1845,7 @@ seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
 		         * TODO: MIDI_PITCH_BEND
 		         */
 #if 0
-			mtx_lock(&md->synth.vc_mtx);
+			lockmgr(&md->synth.vc_lock, LK_EXCLUSIVE);
 			md->synth.chn_info[chn].bender_value = w14;
 			if (md->midiunit >= 0) {
 				/*
@@ -1794,22 +1855,22 @@ seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
 				key = ((int)chn << 8);
 				for (i = 0; i < md->synth.alloc.max_voice; i++)
 					if ((md->synth.alloc.map[i] & 0xff00) == key) {
-						mtx_unlock(&md->synth.vc_mtx);
-						mtx_unlock(&scp->seq_lock);
+						lockmgr(&md->synth.vc_lock, LK_RELEASE);
+						lockmgr(&scp->seq_lock, LK_RELEASE);
 						if (md->synth.bender(md, i, w14) == EAGAIN) {
-							mtx_lock(&scp->seq_lock);
+							lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 							return (QUEUEFULL);
 						}
-						mtx_lock(&scp->seq_lock);
+						lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 					}
 			} else {
-				mtx_unlock(&md->synth.vc_mtx);
-				mtx_unlock(&scp->seq_lock);
+				lockmgr(&md->synth.vc_lock, LK_RELEASE);
+				lockmgr(&scp->seq_lock, LK_RELEASE);
 				if (md->synth.bender(md, chn, w14) == EAGAIN) {
-					mtx_lock(&scp->seq_lock);
+					lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 					return (QUEUEFULL);
 				}
-				mtx_lock(&scp->seq_lock);
+				lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 			}
 #endif
 		} else
@@ -1818,12 +1879,12 @@ seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
 	default:
 		ret = 1;
 		SEQ_DEBUG(2,
-		    printf("seq_chncommon event type %d not handled.\n",
+		    kprintf("seq_chncommon event type %d not handled.\n",
 		    event[1]));
 		break;
 
 	}
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	return ret;
 }
 
@@ -1837,7 +1898,7 @@ seq_timing(struct seq_softc *scp, u_char *event)
 	param = event[4] + (event[5] << 8) +
 	    (event[6] << 16) + (event[7] << 24);
 
-	SEQ_DEBUG(5, printf("seq_timing: unit %d, cmd %d, param %d.\n",
+	SEQ_DEBUG(5, kprintf("seq_timing: unit %d, cmd %d, param %d.\n",
 	    scp->unit, event[1], param));
 	switch (event[1]) {
 	case TMR_WAIT_REL:
@@ -1867,7 +1928,7 @@ seq_timing(struct seq_softc *scp, u_char *event)
 			param = 8;
 		if (param > 360)
 			param = 360;
-		SEQ_DEBUG(4, printf("Timer set tempo %d\n", param));
+		SEQ_DEBUG(4, kprintf("Timer set tempo %d\n", param));
 		timer_setvals(scp, param, scp->timerbase);
 		break;
 	case TMR_TIMERBASE:
@@ -1875,7 +1936,7 @@ seq_timing(struct seq_softc *scp, u_char *event)
 			param = 1;
 		if (param > 1000)
 			param = 1000;
-		SEQ_DEBUG(4, printf("Timer set timerbase %d\n", param));
+		SEQ_DEBUG(4, kprintf("Timer set timerbase %d\n", param));
 		timer_setvals(scp, scp->tempo, param);
 		break;
 	case TMR_ECHO:
@@ -1897,7 +1958,7 @@ seq_timing(struct seq_softc *scp, u_char *event)
 #endif
 		break;
 	default:
-		SEQ_DEBUG(2, printf("seq_timing event type %d not handled.\n",
+		SEQ_DEBUG(2, kprintf("seq_timing event type %d not handled.\n",
 		    event[1]));
 		ret = 1;
 		break;
@@ -1911,13 +1972,13 @@ seq_local(struct seq_softc *scp, u_char *event)
 	int ret;
 
 	ret = 0;
-	mtx_assert(&scp->seq_lock, MA_OWNED);
+	KKASSERT(lockowned(&scp->seq_lock));
 
-	SEQ_DEBUG(5, printf("seq_local: unit %d, cmd %d\n", scp->unit,
+	SEQ_DEBUG(5, kprintf("seq_local: unit %d, cmd %d\n", scp->unit,
 	    event[1]));
 	switch (event[1]) {
 	default:
-		SEQ_DEBUG(1, printf("seq_local event type %d not handled\n",
+		SEQ_DEBUG(1, kprintf("seq_local event type %d not handled\n",
 		    event[1]));
 		ret = 1;
 		break;
@@ -1930,19 +1991,19 @@ seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
 {
 	int i, l;
 
-	mtx_assert(&scp->seq_lock, MA_OWNED);
-	SEQ_DEBUG(5, printf("seq_sysex: unit %d device %d\n", scp->unit,
+	KKASSERT(lockowned(&scp->seq_lock));
+	SEQ_DEBUG(5, kprintf("seq_sysex: unit %d device %d\n", scp->unit,
 	    event[1]));
 	l = 0;
 	for (i = 0; i < 6 && event[i + 2] != 0xff; i++)
 		l = i + 1;
 	if (l > 0) {
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
-			mtx_lock(&scp->seq_lock);
+			lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 			return 1;
 		}
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	}
 	return 0;
 }
@@ -1957,9 +2018,9 @@ seq_reset(struct seq_softc *scp)
 	int chn, i;
 	kobj_t m;
 
-	mtx_assert(&scp->seq_lock, MA_OWNED);
+	KKASSERT(lockowned(&scp->seq_lock));
 
-	SEQ_DEBUG(5, printf("seq_reset: unit %d.\n", scp->unit));
+	SEQ_DEBUG(5, kprintf("seq_reset: unit %d.\n", scp->unit));
 
 	/*
 	 * Stop reading and writing.
@@ -1979,14 +2040,14 @@ seq_reset(struct seq_softc *scp)
 
 	for (i = 0; i < scp->midi_number; i++) {
 		m = scp->midis[i];
-		mtx_unlock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_RELEASE);
 		SYNTH_RESET(m);
 		for (chn = 0; chn < 16; chn++) {
 			SYNTH_CONTROLLER(m, chn, 123, 0);
 			SYNTH_CONTROLLER(m, chn, 121, 0);
 			SYNTH_BENDER(m, chn, 1 << 13);
 		}
-		mtx_lock(&scp->seq_lock);
+		lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	}
 }
 
@@ -2003,9 +2064,9 @@ seq_sync(struct seq_softc *scp)
 {
 	int i, rl, sync[16], done;
 
-	mtx_assert(&scp->seq_lock, MA_OWNED);
+	KKASSERT(lockowned(&scp->seq_lock));
 
-	SEQ_DEBUG(4, printf("seq_sync: unit %d.\n", scp->unit));
+	SEQ_DEBUG(4, kprintf("seq_sync: unit %d.\n", scp->unit));
 
 	/*
 	 * Wait until output queue is empty.  Check every so often to see if
@@ -2049,7 +2110,7 @@ seq_sync(struct seq_softc *scp)
 			 * TODO: Consider if the raw devices need to be flushed
 			 */
 
-			SEQ_DEBUG(1, printf("seq_sync queue stuck, aborting\n"));
+			SEQ_DEBUG(1, kprintf("seq_sync queue stuck, aborting\n"));
 
 			return i;
 		}
@@ -2060,7 +2121,7 @@ seq_sync(struct seq_softc *scp)
 	 * Since syncing a midi device might block, unlock scp->seq_lock.
 	 */
 
-	mtx_unlock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_RELEASE);
 	for (i = 0; i < scp->midi_number; i++)
 		sync[i] = 1;
 
@@ -2078,7 +2139,7 @@ seq_sync(struct seq_softc *scp)
 
 	} while (!done);
 
-	mtx_lock(&scp->seq_lock);
+	lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
 	return 0;
 }
 
diff --git a/sys/dev/sound/midi/synth_if.m b/sys/dev/sound/midi/synth_if.m
index faef209..03514f1 100644
--- a/sys/dev/sound/midi/synth_if.m
+++ b/sys/dev/sound/midi/synth_if.m
@@ -56,63 +56,63 @@ synth_alloc_t noalloc;
     int
 	nokillnote(void *_kobj, uint8_t _chn, uint8_t _note, uint8_t _vel)
 	{
-	    printf("nokillnote\n");
+	    kprintf("nokillnote\n");
 	    return 0;
 	}
 
     int
 	noopen(void *_kobj, void *_arg, int mode)
 	{
-	    printf("noopen\n");
+	    kprintf("noopen\n");
 	    return 0;
 	}
 
     int
 	noquery(void *_kboj)
 	{
-	    printf("noquery\n");
+	    kprintf("noquery\n");
 	    return 0;
 	}
 
     int
 	nostartnote(void *_kb, uint8_t _voice, uint8_t _note, uint8_t _parm)
 	{
-	    printf("nostartnote\n");
+	    kprintf("nostartnote\n");
 	    return 0;
 	}
 
     int
 	nosetinstr(void *_kb, uint8_t _chn, uint16_t _patchno)
 	{
-	    printf("nosetinstr\n");
+	    kprintf("nosetinstr\n");
 	    return 0;
 	}
 
     int
 	nohwcontrol(void *_kb, uint8_t *_event)
 	{
-	    printf("nohwcontrol\n");
+	    kprintf("nohwcontrol\n");
 	    return 0;
 	}
 
     int 
 	noaftertouch ( void /* X */ * _kobj, uint8_t _x1, uint8_t _x2)
 	{
-	    printf("noaftertouch\n");
+	    kprintf("noaftertouch\n");
 	    return 0;
 	}
 
     int
 	nopanning ( void /* X */ * _kobj, uint8_t _x1, uint8_t _x2)
 	{
-	    printf("nopanning\n");
+	    kprintf("nopanning\n");
 	    return 0;
 	}
 
     int 
 	nocontroller ( void /* X */ * _kobj, uint8_t _x1, uint8_t _x2, uint16_t _x3)
 	{
-	    printf("nocontroller\n");
+	    kprintf("nocontroller\n");
 	    return 0;
 	}
 
@@ -121,14 +121,14 @@ synth_alloc_t noalloc;
 		void /* X */ * _kobj,
 		uint8_t _x1)
 	{
-	    printf("novolumemethod\n");
+	    kprintf("novolumemethod\n");
 	    return 0;
 	}
 
     int 
 	nobender ( void /* X */ * _kobj, uint8_t _voice, uint16_t _bend)
 	{
-	    printf("nobender\n");
+	    kprintf("nobender\n");
 	    return 0;
 	}
 
@@ -136,28 +136,28 @@ synth_alloc_t noalloc;
 	nosetupvoice ( void /* X */ * _kobj, uint8_t _voice, uint8_t _chn)
 	{
 
-	    printf("nosetupvoice\n");
+	    kprintf("nosetupvoice\n");
 	    return 0;
 	}
 
     int 
 	nosendsysex ( void /* X */ * _kobj, void * _buf, size_t _len)
 	{
-	    printf("nosendsysex\n");
+	    kprintf("nosendsysex\n");
 	    return 0;
 	}
 
     int 
 	noallocvoice ( void /* X */ * _kobj, uint8_t _chn, uint8_t _note, void *_x)
 	{
-	    printf("noallocvoice\n");
+	    kprintf("noallocvoice\n");
 	    return 0;
 	}
 
     int 
 	nowriteraw ( void /* X */ * _kobjt, uint8_t * _buf, size_t _len)
 	{
-	    printf("nowriteraw\n");
+	    kprintf("nowriteraw\n");
 	    return 1;
 	}
 
@@ -165,14 +165,14 @@ synth_alloc_t noalloc;
 	noreset ( void /* X */ * _kobjt)
 	{
 
-	    printf("noreset\n");
+	    kprintf("noreset\n");
 	    return 0;
 	}
 
     char *
 	noshortname (void /* X */ * _kobjt)
 	{
-	    printf("noshortname\n");
+	    kprintf("noshortname\n");
 	    return "noshortname";
 	}
 
@@ -180,7 +180,7 @@ synth_alloc_t noalloc;
 	noclose ( void /* X */ * _kobjt)
 	{
 
-	    printf("noclose\n");
+	    kprintf("noclose\n");
 	    return 0;
 	}
 
@@ -188,14 +188,14 @@ synth_alloc_t noalloc;
 	noinsync (void /* X */ * _kobjt)
 	{
 
-	    printf("noinsync\n");
+	    kprintf("noinsync\n");
 	    return 0;
 	}
 
     int 
 	noalloc ( void /* x */ * _kbojt, uint8_t _chn, uint8_t _note)
 	{
-	    printf("noalloc\n");
+	    kprintf("noalloc\n");
 	    return 0;
 	}
 }
diff --git a/sys/dev/sound/pci/emu10kx-midi.c b/sys/dev/sound/pci/emu10kx-midi.c
index 5123214..feb8f23 100644
--- a/sys/dev/sound/pci/emu10kx-midi.c
+++ b/sys/dev/sound/pci/emu10kx-midi.c
@@ -31,13 +31,11 @@
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/bus.h>
-#include <machine/bus.h>
 #include <sys/rman.h>
 #include <sys/systm.h>
 #include <sys/sbuf.h>
 #include <sys/queue.h>
 #include <sys/lock.h>
-#include <sys/mutex.h>
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_snd.h"
@@ -54,7 +52,7 @@
 #include <dev/sound/pci/emu10kx.h>
 
 struct emu_midi_softc {
-	struct mtx	mtx;
+	struct lock	lock;
 	device_t	dev;
 	struct mpu401	*mpu;
 	mpu401_intr_t	*mpu_intr;
@@ -99,9 +97,9 @@ emu_muninit(struct mpu401 *arg __unused, void *cookie)
 {
 	struct emu_midi_softc *sc = cookie;
 
-	mtx_lock(&sc->mtx);
+	lockmgr(&sc->lock, LK_EXCLUSIVE);
 	sc->mpu_intr = NULL;
-	mtx_unlock(&sc->mtx);
+	lockmgr(&sc->lock, LK_RELEASE);
 
 	return (0);
 }
@@ -171,7 +169,7 @@ emu_midi_attach(device_t dev)
 	scp->port = midiinfo->port;
 	scp->card = midiinfo->card;
 
-	mtx_init(&scp->mtx, device_get_nameunit(dev), "midi softc", MTX_DEF);
+	lockinit(&scp->lock, device_get_nameunit(dev), 0, LK_CANRECURSE);
 
 	if (scp->is_emu10k1) {
 		/* SB Live! - only one MIDI device here */
@@ -203,7 +201,7 @@ emu_midi_attach(device_t dev)
 	scp->mpu = mpu401_init(&emu_mpu_class, scp, emu_midi_intr, &scp->mpu_intr);
 	if (scp->mpu == NULL) {
 		emu_intr_unregister(scp->card, scp->ihandle);
-		mtx_destroy(&scp->mtx);
+		lockuninit(&scp->lock);
 		return (ENOMEM);
 	}
 	/*
@@ -230,7 +228,7 @@ emu_midi_detach(device_t dev)
 	scp = device_get_softc(dev);
 	mpu401_uninit(scp->mpu);
 	emu_intr_unregister(scp->card, scp->ihandle);
-	mtx_destroy(&scp->mtx);
+	lockuninit(&scp->lock);
 	return (0);
 }
 
-- 
2.5.1

