Module Name:    src
Committed By:   snj
Date:           Sat Sep 23 17:13:10 UTC 2017

Modified Files:
        src/sys/dev [netbsd-8]: audio.c audiovar.h

Log Message:
Pull up following revision(s) (requested by isaki in ticket #278):
        sys/dev/audiovar.h: revision 1.61-1.62
        sys/dev/audiovar.h: revision 1.63 via patch
        sys/dev/audio.c: revision 1.381, 1.390, 1.391, 1.393
        sys/dev/audio.c: revision 1.394 via patch
Use do .. while (0) for macros.
--
Introduce audio_destroy_pfilters()/audio_destroy_rfilters()
and use it.
--
Remove dead codes. chan will never be NULL in SIMPLEQ_FOREACH.
--
Move defines to the appropriate place.
--
Remove mixer chan from sc_audiochan.
Now sc_audiochan contains opened audio chan (and first special
element) only.
First I splitted sc_audiochan into sc_audiochan which has
audio chan (and first special element) and sc_mixerchan
which has mixer chan only.  However nobody else refers this
sc_mixerchan except additions to list and deletions from
list.  So mixer chan's list is not necessary.
--
Split the first special element from sc_audiochan list.
- This makes sc_audiochan a list of only open audio chan.
- "SIMPLEQ_FIRST(&sc->sc_audiochan)->vc" is now "sc->hwvc".
No functional changes (except one debug message).


To generate a diff of this commit:
cvs rdiff -u -r1.357.2.7 -r1.357.2.8 src/sys/dev/audio.c
cvs rdiff -u -r1.55.2.2 -r1.55.2.3 src/sys/dev/audiovar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio.c
diff -u src/sys/dev/audio.c:1.357.2.7 src/sys/dev/audio.c:1.357.2.8
--- src/sys/dev/audio.c:1.357.2.7	Mon Sep 11 05:33:23 2017
+++ src/sys/dev/audio.c	Sat Sep 23 17:13:10 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.357.2.7 2017/09/11 05:33:23 snj Exp $	*/
+/*	$NetBSD: audio.c,v 1.357.2.8 2017/09/23 17:13:10 snj Exp $	*/
 
 /*-
  * Copyright (c) 2016 Nathanial Sloss <[email protected]>
@@ -148,7 +148,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.357.2.7 2017/09/11 05:33:23 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.357.2.8 2017/09/23 17:13:10 snj Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -214,11 +214,15 @@ int	audiodebug = AUDIO_DEBUG;
 int	audio_idle_timeout = 30;
 #endif
 
-#define HW_LOCK(x)	if ((x) == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc) \
-				mutex_enter(sc->sc_intr_lock);
-
-#define HW_UNLOCK(x)	if ((x) == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc) \
-				mutex_exit(sc->sc_intr_lock);
+#define HW_LOCK(x)	do { \
+	if ((x) == sc->sc_hwvc) \
+		mutex_enter(sc->sc_intr_lock); \
+} while (0)
+
+#define HW_UNLOCK(x)	do { \
+	if ((x) == sc->sc_hwvc) \
+		mutex_exit(sc->sc_intr_lock); \
+} while (0)
 
 int	audio_blk_ms = AUDIO_BLK_MS;
 
@@ -280,7 +284,7 @@ void	audio_init_ringbuffer(struct audio_
 			      struct audio_ringbuffer *, int);
 int	audio_initbufs(struct audio_softc *, struct virtual_channel *);
 void	audio_calcwater(struct audio_softc *, struct virtual_channel *);
-int	audio_drain(struct audio_softc *, struct audio_chan *);
+int	audio_drain(struct audio_softc *, struct virtual_channel *);
 void	audio_clear(struct audio_softc *, struct virtual_channel *);
 void	audio_clear_intr_unlocked(struct audio_softc *sc,
 				  struct virtual_channel *);
@@ -294,6 +298,8 @@ static int audio_setup_pfilters(struct a
 			      stream_filter_list_t *, struct virtual_channel *);
 static int audio_setup_rfilters(struct audio_softc *, const audio_params_t *,
 			      stream_filter_list_t *, struct virtual_channel *);
+static void audio_destroy_pfilters(struct virtual_channel *);
+static void audio_destroy_rfilters(struct virtual_channel *);
 static void audio_stream_dtor(audio_stream_t *);
 static int audio_stream_ctor(audio_stream_t *, const audio_params_t *, int);
 static void stream_filter_list_append(stream_filter_list_t *,
@@ -476,7 +482,6 @@ audioattach(device_t parent, device_t se
 	struct audio_softc *sc;
 	struct audio_attach_args *sa;
 	struct virtual_channel *vc;
-	struct audio_chan *chan;
 	const struct audio_hw_if *hwp;
 	const struct sysctlnode *node;
 	void *hdlp;
@@ -522,11 +527,10 @@ audioattach(device_t parent, device_t se
 	sc->sc_trigger_started = false;
 	sc->sc_rec_started = false;
 	sc->sc_dying = false;
-	chan = kmem_zalloc(sizeof(struct audio_chan), KM_SLEEP);
-	vc = kmem_zalloc(sizeof(struct virtual_channel), KM_SLEEP);
-	chan->vc = vc;
 	SIMPLEQ_INIT(&sc->sc_audiochan);
-	SIMPLEQ_INSERT_HEAD(&sc->sc_audiochan, chan, entries);
+
+	vc = kmem_zalloc(sizeof(struct virtual_channel), KM_SLEEP);
+	sc->sc_hwvc = vc;
 	vc->sc_open = 0;
 	vc->sc_mode = 0;
 	vc->sc_npfilters = 0;
@@ -869,7 +873,7 @@ audiodetach(device_t self, int flags)
 {
 	struct audio_softc *sc;
 	struct audio_chan *chan;
-	int maj, mn, i, rc;
+	int maj, mn, rc;
 
 	sc = device_private(self);
 	DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags));
@@ -930,38 +934,19 @@ audiodetach(device_t self, int flags)
 
 	/* free resources */
 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
-		if (chan == NULL)
-			break;
-
-		if (chan->chan == MIXER_INUSE)
-			continue;
 		audio_free_ring(sc, &chan->vc->sc_mpr);
 		audio_free_ring(sc, &chan->vc->sc_mrr);
 	}
+	audio_free_ring(sc, &sc->sc_hwvc->sc_mpr);
+	audio_free_ring(sc, &sc->sc_hwvc->sc_mrr);
 	audio_free_ring(sc, &sc->sc_pr);
 	audio_free_ring(sc, &sc->sc_rr);
 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
-		if (chan == NULL)
-			break;
-
-		if (chan->chan == MIXER_INUSE)
-			continue;
-		for (i = 0; i < chan->vc->sc_npfilters; i++) {
-			chan->vc->sc_pfilters[i]->dtor
-			    (chan->vc->sc_pfilters[i]);
-			chan->vc->sc_pfilters[i] = NULL;
-			audio_stream_dtor(&chan->vc->sc_pstreams[i]);
-		}
-		chan->vc->sc_npfilters = 0;
-
-		for (i = 0; i < chan->vc->sc_nrfilters; i++) {
-			chan->vc->sc_rfilters[i]->dtor
-			    (chan->vc->sc_rfilters[i]);
-			chan->vc->sc_rfilters[i] = NULL;
-			audio_stream_dtor(&chan->vc->sc_rstreams[i]);
-		}
-		chan->vc->sc_nrfilters = 0;
+		audio_destroy_pfilters(chan->vc);
+		audio_destroy_rfilters(chan->vc);
 	}
+	audio_destroy_pfilters(sc->sc_hwvc);
+	audio_destroy_rfilters(sc->sc_hwvc);
 
 	auconv_delete_encodings(sc->sc_encodings);
 
@@ -974,10 +959,7 @@ audiodetach(device_t self, int flags)
 		sc->sc_sih_wr = NULL;
 	}
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	kmem_free(chan->vc, sizeof(struct virtual_channel));
-	SIMPLEQ_REMOVE(&sc->sc_audiochan, chan, audio_chan, entries);
-	kmem_free(chan, sizeof(struct audio_chan));
+	kmem_free(sc->sc_hwvc, sizeof(struct virtual_channel));
 	kmem_free(sc->sc_mixer_state, sizeof(mixer_ctrl_t) *
 	    (sc->sc_nmixer_states + 1));
 
@@ -1107,26 +1089,21 @@ void	audio_print_params(const char *, st
 void
 audio_printsc(struct audio_softc *sc)
 {
-	struct audio_chan *chan;
-	
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	
-	if (chan == NULL)
-		return;
+	struct virtual_channel *vc;
+
+	vc = sc->sc_hwvc;
 
 	printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if);
-	printf("open 0x%x mode 0x%x\n", chan->vc->sc_open,
-	    chan->vc->sc_mode);
+	printf("open 0x%x mode 0x%x\n", vc->sc_open, vc->sc_mode);
 	printf("rchan 0x%x wchan 0x%x ", cv_has_waiters(&sc->sc_rchan),
 	    cv_has_waiters(&sc->sc_wchan));
 	printf("rring used 0x%x pring used=%d\n",
-	       audio_stream_get_used(&chan->vc->sc_mrr.s),
-	       audio_stream_get_used(&chan->vc->sc_mpr.s));
-	printf("rbus 0x%x pbus 0x%x ", chan->vc->sc_rbus,
-	    chan->vc->sc_pbus);
-	printf("blksize %d", chan->vc->sc_mpr.blksize);
-	printf("hiwat %d lowat %d\n", chan->vc->sc_mpr.usedhigh,
-	    chan->vc->sc_mpr.usedlow);
+	    audio_stream_get_used(&vc->sc_mrr.s),
+	    audio_stream_get_used(&vc->sc_mpr.s));
+	printf("rbus 0x%x pbus 0x%x ", vc->sc_rbus, vc->sc_pbus);
+	printf("blksize %d", vc->sc_mpr.blksize);
+	printf("hiwat %d lowat %d\n", vc->sc_mpr.usedhigh,
+	    vc->sc_mpr.usedlow);
 }
 
 void
@@ -1190,13 +1167,13 @@ audio_alloc_ring(struct audio_softc *sc,
 		 int direction, size_t bufsize)
 {
 	const struct audio_hw_if *hw;
-	struct audio_chan *chan;
+	struct virtual_channel *vc;
 	void *hdl;
 	vaddr_t vstart;
 	vsize_t vsize;
 	int error;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
+	vc = sc->sc_hwvc;
 	hw = sc->hw_if;
 	hdl = sc->hw_hdl;
 	/*
@@ -1208,7 +1185,7 @@ audio_alloc_ring(struct audio_softc *sc,
 	if (hw->round_buffersize)
 		bufsize = hw->round_buffersize(hdl, direction, bufsize);
 
-	if (hw->allocm && (r == &chan->vc->sc_mpr || r == &chan->vc->sc_mrr)) {
+	if (hw->allocm && (r == &vc->sc_mpr || r == &vc->sc_mrr)) {
 		/* Hardware ringbuffer.	 No dedicated uvm object.*/
 		r->uobj = NULL;
 		r->s.start = hw->allocm(hdl, direction, bufsize);
@@ -1253,17 +1230,16 @@ audio_alloc_ring(struct audio_softc *sc,
 void
 audio_free_ring(struct audio_softc *sc, struct audio_ringbuffer *r)
 {
-	struct audio_chan *chan;
+	struct virtual_channel *vc;
 	vaddr_t vstart;
 	vsize_t vsize;
 
 	if (r->s.start == NULL)
 		return;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
+	vc = sc->sc_hwvc;
 
-	if (sc->hw_if->freem && (r == &chan->vc->sc_mpr ||
-					r == &chan->vc->sc_mrr)) {
+	if (sc->hw_if->freem && (r == &vc->sc_mpr || r == &vc->sc_mrr)) {
 		 /* Hardware ringbuffer.  */
 		KASSERT(r->uobj == NULL);
 		sc->hw_if->freem(sc->hw_hdl, r->s.start, r->s.bufsize);
@@ -1456,6 +1432,32 @@ audio_setup_rfilters(struct audio_softc 
 }
 
 static void
+audio_destroy_pfilters(struct virtual_channel *vc)
+{
+	int i;
+
+	for (i = 0; i < vc->sc_npfilters; i++) {
+		vc->sc_pfilters[i]->dtor(vc->sc_pfilters[i]);
+		vc->sc_pfilters[i] = NULL;
+		audio_stream_dtor(&vc->sc_pstreams[i]);
+	}
+	vc->sc_npfilters = 0;
+}
+
+static void
+audio_destroy_rfilters(struct virtual_channel *vc)
+{
+	int i;
+
+	for (i = 0; i < vc->sc_nrfilters; i++) {
+		vc->sc_rfilters[i]->dtor(vc->sc_rfilters[i]);
+		vc->sc_rfilters[i] = NULL;
+		audio_stream_dtor(&vc->sc_pstreams[i]);
+	}
+	vc->sc_nrfilters = 0;
+}
+
+static void
 audio_stream_dtor(audio_stream_t *stream)
 {
 
@@ -1585,8 +1587,6 @@ audio_waitio(struct audio_softc *sc, kco
 
 	found = false;
 	SIMPLEQ_FOREACH(vchan, &sc->sc_audiochan, entries) {
-		if (vchan == SIMPLEQ_FIRST(&sc->sc_audiochan))
-			continue;
 		if (vchan->vc == vc) {
 			found = true;
 			break;
@@ -2023,12 +2023,10 @@ int
 audio_initbufs(struct audio_softc *sc, struct virtual_channel *vc)
 {
 	const struct audio_hw_if *hw;
-	struct audio_chan *chan;
 	int error;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
 	if (vc == NULL) {
-		vc = chan->vc;
+		vc = sc->sc_hwvc;
 		sc->sc_pr.blksize = vc->sc_mrr.blksize;
 		sc->sc_rr.blksize = vc->sc_mrr.blksize;
 	}
@@ -2036,7 +2034,7 @@ audio_initbufs(struct audio_softc *sc, s
 	DPRINTF(("audio_initbufs: mode=0x%x\n", vc->sc_mode));
 	hw = sc->hw_if;
 	if (audio_can_capture(sc) &&
-		((vc->sc_open & AUOPEN_READ) || vc == chan->vc)) {
+		((vc->sc_open & AUOPEN_READ) || vc == sc->sc_hwvc)) {
 		audio_init_ringbuffer(sc, &vc->sc_mrr,
 		    AUMODE_RECORD);
 		if (sc->sc_opens == 0 && hw->init_input &&
@@ -2047,11 +2045,11 @@ audio_initbufs(struct audio_softc *sc, s
 				return error;
 		}
 	}
-	if (vc == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc)
+	if (vc == sc->sc_hwvc)
 		sc->sc_rr.blksize = vc->sc_mrr.blksize;
 
 	if (audio_can_playback(sc) &&
-		((vc->sc_open & AUOPEN_WRITE) || vc == chan->vc)) {
+		((vc->sc_open & AUOPEN_WRITE) || vc == sc->sc_hwvc)) {
 		audio_init_ringbuffer(sc, &vc->sc_mpr,
 		    AUMODE_PLAY);
 		vc->sc_sil_count = 0;
@@ -2063,7 +2061,7 @@ audio_initbufs(struct audio_softc *sc, s
 				return error;
 		}
 	}
-	if (vc == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc)
+	if (vc == sc->sc_hwvc)
 		sc->sc_pr.blksize = vc->sc_mpr.blksize;
 
 #ifdef AUDIO_INTR_TIME
@@ -2119,7 +2117,7 @@ audio_open(dev_t dev, struct audio_softc
     struct lwp *l, struct file **nfp)
 {
 	struct file *fp;
-	int error, fd, i, n;
+	int error, fd, n;
 	u_int mode;
 	const struct audio_hw_if *hw;
 	struct virtual_channel *vc;
@@ -2135,10 +2133,6 @@ audio_open(dev_t dev, struct audio_softc
 		return ENXIO;
 	n = 1;
 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
-		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan))
-			continue;
-		if (chan->chan == MIXER_INUSE)
-			continue;
 		n = chan->chan + 1;
 	}
 	if (n < 0)
@@ -2279,18 +2273,8 @@ audio_open(dev_t dev, struct audio_softc
 	return error;
 
 bad:
-	for (i = 0; i < vc->sc_npfilters; i++) {
-		vc->sc_pfilters[i]->dtor(vc->sc_pfilters[i]);
-		vc->sc_pfilters[i] = NULL;
-		audio_stream_dtor(&vc->sc_pstreams[i]);
-	}
-	vc->sc_npfilters = 0;
-	for (i = 0; i < vc->sc_nrfilters; i++) {
-		vc->sc_rfilters[i]->dtor(vc->sc_rfilters[i]);
-		vc->sc_rfilters[i] = NULL;
-		audio_stream_dtor(&vc->sc_rstreams[i]);
-	}
-	vc->sc_nrfilters = 0;
+	audio_destroy_pfilters(vc);
+	audio_destroy_rfilters(vc);
 	if (hw->close != NULL && sc->sc_opens == 0)
 		hw->close(sc->hw_hdl);
 	mutex_exit(sc->sc_lock);
@@ -2341,10 +2325,9 @@ audio_init_play(struct audio_softc *sc, 
 }
 
 int
-audio_drain(struct audio_softc *sc, struct audio_chan *chan)
+audio_drain(struct audio_softc *sc, struct virtual_channel *vc)
 {
 	struct audio_ringbuffer *cb;
-	struct virtual_channel *vc;
 	int error, drops;
 	int cc, i, used;
 	bool hw = false;
@@ -2353,14 +2336,13 @@ audio_drain(struct audio_softc *sc, stru
 	KASSERT(mutex_owned(sc->sc_intr_lock));
 	
 	error = 0;
-	vc = chan->vc;
 	DPRINTF(("audio_drain: enter busy=%d\n", vc->sc_pbus));
-	cb = &chan->vc->sc_mpr;
+	cb = &vc->sc_mpr;
 	if (cb->mmapped)
 		return 0;
 
 	used = audio_stream_get_used(&cb->s);
-	if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan)) {
+	if (vc == sc->sc_hwvc) {
 		hw = true;
 		used += audio_stream_get_used(&sc->sc_pr.s);
 	}
@@ -2414,8 +2396,8 @@ audio_drain(struct audio_softc *sc, stru
 	drops = cb->drops;
 	error = 0;
 	while (cb->drops == drops && !error) {
-		DPRINTF(("audio_drain: chan=%d used=%d, drops=%ld\n",
-			chan->chan,
+		DPRINTF(("audio_drain: vc=%p used=%d, drops=%ld\n",
+			vc,
 			audio_stream_get_used(&vc->sc_mpr.s),
 			cb->drops));
 		mutex_exit(sc->sc_intr_lock);
@@ -2438,7 +2420,6 @@ audio_close(struct audio_softc *sc, int 
 {
 	struct virtual_channel *vc;
 	const struct audio_hw_if *hw;
-	int o;
 
 	KASSERT(mutex_owned(sc->sc_lock));
 	
@@ -2473,12 +2454,12 @@ audio_close(struct audio_softc *sc, int 
 	 */
 	if ((flags & FWRITE) && vc->sc_pbus) {
 		if (!vc->sc_mpr.pause)
-			audio_drain(sc, chan);
+			audio_drain(sc, chan->vc);
 		vc->sc_pbus = false;
 	}
 	if (sc->sc_opens == 1) {
 		if (vc->sc_mpr.mmapped == false)
-			audio_drain(sc, SIMPLEQ_FIRST(&sc->sc_audiochan));
+			audio_drain(sc, sc->sc_hwvc);
 		if (hw->drain)
 			(void)hw->drain(sc->hw_hdl);
 		hw->halt_output(sc->hw_hdl);
@@ -2499,19 +2480,9 @@ audio_close(struct audio_softc *sc, int 
 	vc->sc_open = 0;
 	vc->sc_mode = 0;
 	vc->sc_full_duplex = 0;
-	
-	for (o = 0; o < vc->sc_npfilters; o++) {
-		vc->sc_pfilters[o]->dtor(vc->sc_pfilters[o]);
-		vc->sc_pfilters[o] = NULL;
-		audio_stream_dtor(&vc->sc_pstreams[o]);
-	}
-	vc->sc_npfilters = 0;
-	for (o = 0; o < vc->sc_nrfilters; o++) {
-		vc->sc_rfilters[o]->dtor(vc->sc_rfilters[o]);
-		vc->sc_rfilters[o] = NULL;
-		audio_stream_dtor(&vc->sc_rstreams[o]);
-	}
-	vc->sc_nrfilters = 0;
+
+	audio_destroy_pfilters(vc);
+	audio_destroy_rfilters(vc);
 
 	if (flags & FREAD)
 		sc->sc_recopens--;
@@ -2756,14 +2727,12 @@ audio_fill_silence(struct audio_params *
 int
 audio_silence_copyout(struct audio_softc *sc, int n, struct uio *uio)
 {
-	struct audio_chan *chan;
 	struct virtual_channel *vc;
 	uint8_t zerobuf[128];
 	int error;
 	int k;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	vc = chan->vc;
+	vc = sc->sc_hwvc;
 	audio_fill_silence(&vc->sc_rparams, zerobuf, sizeof zerobuf);
 
 	error = 0;
@@ -3014,8 +2983,6 @@ audio_ioctl(dev_t dev, struct audio_soft
 	KASSERT(mutex_owned(sc->sc_lock));
 
 	SIMPLEQ_FOREACH(pchan, &sc->sc_audiochan, entries) {
-		if (pchan == SIMPLEQ_FIRST(&sc->sc_audiochan))
-			continue;
 		if (pchan->chan == chan->deschan)
 			break;
 	}
@@ -3158,7 +3125,7 @@ audio_ioctl(dev_t dev, struct audio_soft
 	case AUDIO_DRAIN:
 		DPRINTF(("AUDIO_DRAIN\n"));
 		mutex_enter(sc->sc_intr_lock);
-		error = audio_drain(sc, pchan);
+		error = audio_drain(sc, pchan->vc);
 		if (!error && sc->sc_opens == 1 && hw->drain)
 		    error = hw->drain(sc->hw_hdl);
 		mutex_exit(sc->sc_intr_lock);
@@ -3474,8 +3441,6 @@ audio_mmap(struct audio_softc *sc, off_t
 int
 audiostartr(struct audio_softc *sc, struct virtual_channel *vc)
 {
-
-	struct audio_chan *chan;
 	int error;
 
 	KASSERT(mutex_owned(sc->sc_lock));
@@ -3484,10 +3449,9 @@ audiostartr(struct audio_softc *sc, stru
 		 vc->sc_mrr.s.start, audio_stream_get_used(&vc->sc_mrr.s),
 		 vc->sc_mrr.usedhigh, vc->sc_mrr.mmapped));
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
 	if (!audio_can_capture(sc))
 		return EINVAL;
-	if (vc == chan->vc)
+	if (vc == sc->sc_hwvc)
 		return 0;
 
 	error = 0;
@@ -3505,12 +3469,10 @@ audiostartr(struct audio_softc *sc, stru
 int
 audiostartp(struct audio_softc *sc, struct virtual_channel *vc)
 {
-	struct audio_chan *chan;
 	int error, used;
 
 	KASSERT(mutex_owned(sc->sc_lock));
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
 	error = 0;
 	used = audio_stream_get_used(&vc->sc_mpr.s);
 	DPRINTF(("audiostartp: start=%p used=%d(hi=%d blk=%d) mmapped=%d\n",
@@ -3519,7 +3481,7 @@ audiostartp(struct audio_softc *sc, stru
 
 	if (!audio_can_playback(sc))
 		return EINVAL;
-	if (vc == chan->vc)
+	if (vc == sc->sc_hwvc)
 		return 0;
 
 	if (!vc->sc_mpr.mmapped && used < vc->sc_mpr.blksize) {
@@ -3536,7 +3498,7 @@ audiostartp(struct audio_softc *sc, stru
 		error = mix_write(sc);
 		if (error)
 			goto done;
-		vc = chan->vc;
+		vc = sc->sc_hwvc;
 		vc->sc_mpr.s.outp =
 		    audio_stream_add_outp(&vc->sc_mpr.s,
 		      vc->sc_mpr.s.outp, vc->sc_mpr.blksize);
@@ -3653,13 +3615,11 @@ void
 audio_pint(void *v)
 {
 	struct audio_softc *sc;
-	struct audio_chan *chan;
 	struct virtual_channel *vc;
 	int blksize, cc, used;
 
 	sc = v;
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	vc = chan->vc;
+	vc = sc->sc_hwvc;
 	blksize = vc->sc_mpr.blksize;
 
 	if (sc->sc_dying == true || sc->sc_trigger_started == false)
@@ -3721,15 +3681,6 @@ audio_mix(void *v)
 		if (!sc->sc_opens)
 			break;		/* ignore interrupt if not open */
 
-		if (chan == NULL)
-			break;
-
-		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan))
-			continue;
-
-		if (chan->chan == MIXER_INUSE)
-			continue;
-
 		vc = chan->vc;
 
 		if (!vc->sc_open)
@@ -3864,7 +3815,7 @@ audio_mix(void *v)
 	}
 	mutex_enter(sc->sc_intr_lock);
 
-	vc = SIMPLEQ_FIRST(&sc->sc_audiochan)->vc;
+	vc = sc->sc_hwvc;
 	cb = &sc->sc_pr;
 	inp = cb->s.inp;
 	cc = blksize - (inp - cb->s.start) % blksize;
@@ -3938,15 +3889,6 @@ audio_upmix(void *v)
 		if (!sc->sc_opens)
 			break;		/* ignore interrupt if not open */
 
-		if (chan == NULL)
-			break;
-
-		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan))
-			continue;
-
-		if (chan->chan == MIXER_INUSE)
-			continue;
-
 		vc = chan->vc;
 
 		if (!(vc->sc_open & AUOPEN_READ))
@@ -4135,17 +4077,13 @@ static int
 audio_set_vchan_defaults(struct audio_softc *sc, u_int mode,
      const struct audio_format *format)
 {
-	struct audio_chan *chan;
 	struct virtual_channel *vc;
 	struct audio_info ai;
 	int error;
 
 	KASSERT(mutex_owned(sc->sc_lock));
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	if (chan == NULL)
-		return EINVAL;
-	vc = chan->vc;
+	vc = sc->sc_hwvc;
 
 	sc->sc_vchan_params.sample_rate = sc->sc_frequency;
 #if BYTE_ORDER == LITTLE_ENDIAN
@@ -4790,7 +4728,7 @@ audiosetinfo(struct audio_softc *sc, str
 			audio_init_record(sc, vc);
 	}
 
-	if (vc == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc) {
+	if (vc == sc->sc_hwvc) {
 		if (!cleared) {
 			audio_clear_intr_unlocked(sc, vc);
 			cleared = true;
@@ -4993,9 +4931,6 @@ mixer_open(dev_t dev, struct audio_softc
 
 	chan = kmem_zalloc(sizeof(struct audio_chan), KM_SLEEP);
 	chan->dev = dev;
-	chan->chan = MIXER_INUSE;
-
-	SIMPLEQ_INSERT_TAIL(&sc->sc_audiochan, chan, entries);
 
 	error = fd_clone(fp, fd, flags, &audio_fileops, chan);
 	KASSERT(error == EMOVEFD);
@@ -5057,7 +4992,6 @@ mixer_close(struct audio_softc *sc, int 
 
 	DPRINTF(("mixer_close: sc %p\n", sc));
 	mixer_remove(sc);
-	SIMPLEQ_REMOVE(&sc->sc_audiochan, chan, audio_chan, entries);
 
 	return 0;
 }
@@ -5297,10 +5231,6 @@ audio_suspend(device_t dv, const pmf_qua
 	mutex_enter(sc->sc_lock);
 	audio_mixer_capture(sc);
 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
-		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan) ||
-			chan->chan == MIXER_INUSE)
-			continue;
-
 		vc = chan->vc;
 		if (vc->sc_pbus && !pbus)
 			pbus = true;
@@ -5337,11 +5267,7 @@ audio_resume(device_t dv, const pmf_qual
 
 	audio_mixer_restore(sc);
 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
-		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan) ||
-				chan->chan == MIXER_INUSE)
-			continue;
 		vc = chan->vc;
-
 		if (vc->sc_lastinfovalid == true)
 			audiosetinfo(sc, &vc->sc_lastinfo, true, vc);
 		if (vc->sc_pbus == true && !vc->sc_mpr.pause)
@@ -5458,7 +5384,6 @@ int
 mix_read(void *arg)
 {
 	struct audio_softc *sc = arg;
-	struct audio_chan *chan;
 	struct virtual_channel *vc;
 	stream_filter_t *filter;
 	stream_fetcher_t *fetcher;
@@ -5466,8 +5391,7 @@ mix_read(void *arg)
 	int cc, cc1, blksize, error;
 	uint8_t *inp;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	vc = chan->vc;
+	vc = sc->sc_hwvc;
 	blksize = vc->sc_mrr.blksize;
 	cc = blksize;
 	error = 0;
@@ -5488,7 +5412,7 @@ mix_read(void *arg)
 	if (error) {
 		/* XXX does this really help? */
 		DPRINTF(("audio_upmix restart failed: %d\n", error));
-		audio_clear(sc, SIMPLEQ_FIRST(&sc->sc_audiochan)->vc);
+		audio_clear(sc, sc->sc_hwvc);
 		sc->sc_rec_started = false;
 		return error;
 	}
@@ -5526,7 +5450,6 @@ int
 mix_write(void *arg)
 {
 	struct audio_softc *sc = arg;
-	struct audio_chan *chan;
 	struct virtual_channel *vc;
 	stream_filter_t *filter;
 	stream_fetcher_t *fetcher;
@@ -5534,8 +5457,7 @@ mix_write(void *arg)
 	int cc, cc1, cc2, blksize, error, used;
 	uint8_t *inp, *orig, *tocopy;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	vc = chan->vc;
+	vc = sc->sc_hwvc;
 	blksize = vc->sc_mpr.blksize;
 	cc = blksize;
 	error = 0;
@@ -5599,7 +5521,7 @@ done:
 	if (error) {
 		/* XXX does this really help? */
 		DPRINTF(("audio_mix restart failed: %d\n", error));
-		audio_clear(sc, SIMPLEQ_FIRST(&sc->sc_audiochan)->vc);
+		audio_clear(sc, sc->sc_hwvc);
 		sc->sc_trigger_started = false;
 	}
 
@@ -5736,9 +5658,6 @@ find_vchan_vol(struct audio_softc *sc, i
 	
 	j = 0;
 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
-		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan) ||
-		    chan->chan == MIXER_INUSE)
-			continue;
 		if (j == n)
 			break;
 		j++;
@@ -5836,13 +5755,11 @@ audio_set_params(struct audio_softc *sc,
 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil,
 		 struct virtual_channel *vc)
 {
-	struct audio_chan *chan;
-	
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
 	int error = 0;
+
 	KASSERT(mutex_owned(sc->sc_lock));
 
-	if (vc == chan->vc && sc->hw_if->set_params != NULL) {
+	if (vc == sc->sc_hwvc && sc->hw_if->set_params != NULL) {
 		sc->sc_ready = true;
 		if (sc->sc_precision == 8)
 			play->encoding = rec->encoding = AUDIO_ENCODING_SLINEAR;
@@ -6059,12 +5976,10 @@ audio_sysctl_channels(SYSCTLFN_ARGS)
 static int
 vchan_autoconfig(struct audio_softc *sc)
 {
-	struct audio_chan *chan;
 	struct virtual_channel *vc;
 	int error, i, j, k;
 
-	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
-	vc = chan->vc;
+	vc = sc->sc_hwvc;
 	error = 0;
 
 	mutex_enter(sc->sc_lock);

Index: src/sys/dev/audiovar.h
diff -u src/sys/dev/audiovar.h:1.55.2.2 src/sys/dev/audiovar.h:1.55.2.3
--- src/sys/dev/audiovar.h:1.55.2.2	Mon Sep 11 05:29:37 2017
+++ src/sys/dev/audiovar.h	Sat Sep 23 17:13:10 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: audiovar.h,v 1.55.2.2 2017/09/11 05:29:37 snj Exp $	*/
+/*	$NetBSD: audiovar.h,v 1.55.2.3 2017/09/23 17:13:10 snj Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -113,7 +113,6 @@ struct audio_ringbuffer {
 
 struct audio_chan {
 	dev_t	dev;
-#define MIXER_INUSE	-2
 	struct virtual_channel	*vc;
 	int	chan;			/* virtual channel */
 	int	deschan;		/* desired channel for ioctls*/
@@ -122,6 +121,8 @@ struct audio_chan {
 
 struct virtual_channel {
 	u_char			sc_open;	/* multiple use device */
+#define AUOPEN_READ	0x01
+#define AUOPEN_WRITE	0x02
 	u_char			sc_mode;	/* bitmask for RECORD/PLAY */
 
 	bool			sc_blkset;	/* Blocksize has been set */
@@ -181,9 +182,8 @@ struct audio_softc {
 	void		*hw_hdl;	/* Hardware driver handle */
 	const struct audio_hw_if *hw_if; /* Hardware interface */
 	device_t	sc_dev;		/* Hardware device struct */
-	struct chan_queue sc_audiochan; /* queue of open chans */
-#define AUOPEN_READ	0x01
-#define AUOPEN_WRITE	0x02
+	struct chan_queue sc_audiochan; /* queue of open audio chans */
+	struct virtual_channel *sc_hwvc;
 
 	struct audio_encoding_set *sc_encodings;
 	struct	selinfo sc_wsel; /* write selector */
@@ -225,9 +225,9 @@ struct audio_softc {
 	 * play_thread
 	 *    sc_pr
 	 *      |
-	 *  vchan[0]->sc_pustream 	(First elemendt in sc_audiochan)
+	 *  sc_hwvc->sc_pustream
 	 *      |
-	 *  vchan[0]->sc_mpr
+	 *  sc_hwvc->sc_mpr
 	 *      |
 	 *  hardware
 	 */
@@ -237,10 +237,10 @@ struct audio_softc {
 	/**
 	 *  hardware
 	 *      |
-	 * oc->sc_mrr		oc = sc->sc_vchan[0]
+	 * sc_hwvc->sc_mrr
 	 *      :		Transform though filters same process as each
 	 *      :		 vc to IF
-	 * oc->sc_rustream	Audio now in intermediate format (IF)
+	 * sc_hwvc->sc_rustream	Audio now in intermediate format (IF)
 	 *      |	mix_read();
 	 *    sc_rr
 	 *      |	audio_upmix	vc = sc->sc_vchan[n]

Reply via email to