xref: /dflybsd-src/sys/dev/sound/pcm/feeder_mixer.c (revision 2a1ad637466621af45d5a17185b33f3dcaaa1b1c)
1*2a1ad637SFrançois Tigeot /*-
2*2a1ad637SFrançois Tigeot  * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
3*2a1ad637SFrançois Tigeot  * All rights reserved.
4*2a1ad637SFrançois Tigeot  *
5*2a1ad637SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
6*2a1ad637SFrançois Tigeot  * modification, are permitted provided that the following conditions
7*2a1ad637SFrançois Tigeot  * are met:
8*2a1ad637SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
9*2a1ad637SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer.
10*2a1ad637SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
11*2a1ad637SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
12*2a1ad637SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
13*2a1ad637SFrançois Tigeot  *
14*2a1ad637SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*2a1ad637SFrançois Tigeot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*2a1ad637SFrançois Tigeot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*2a1ad637SFrançois Tigeot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*2a1ad637SFrançois Tigeot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*2a1ad637SFrançois Tigeot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*2a1ad637SFrançois Tigeot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*2a1ad637SFrançois Tigeot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*2a1ad637SFrançois Tigeot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*2a1ad637SFrançois Tigeot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*2a1ad637SFrançois Tigeot  * SUCH DAMAGE.
25*2a1ad637SFrançois Tigeot  */
26*2a1ad637SFrançois Tigeot 
27*2a1ad637SFrançois Tigeot #ifdef _KERNEL
28*2a1ad637SFrançois Tigeot #ifdef HAVE_KERNEL_OPTION_HEADERS
29*2a1ad637SFrançois Tigeot #include "opt_snd.h"
30*2a1ad637SFrançois Tigeot #endif
31*2a1ad637SFrançois Tigeot #include <dev/sound/pcm/sound.h>
32*2a1ad637SFrançois Tigeot #include <dev/sound/pcm/pcm.h>
33*2a1ad637SFrançois Tigeot #include <dev/sound/pcm/vchan.h>
34*2a1ad637SFrançois Tigeot #include "feeder_if.h"
35*2a1ad637SFrançois Tigeot 
36*2a1ad637SFrançois Tigeot #define SND_USE_FXDIV
37*2a1ad637SFrançois Tigeot #include "snd_fxdiv_gen.h"
38*2a1ad637SFrançois Tigeot 
39*2a1ad637SFrançois Tigeot SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder_mixer.c 193640 2009-06-07 19:12:08Z ariff $");
40*2a1ad637SFrançois Tigeot #endif
41*2a1ad637SFrançois Tigeot 
42*2a1ad637SFrançois Tigeot #undef SND_FEEDER_MULTIFORMAT
43*2a1ad637SFrançois Tigeot #define SND_FEEDER_MULTIFORMAT	1
44*2a1ad637SFrançois Tigeot 
45*2a1ad637SFrançois Tigeot typedef void (*feed_mixer_t)(uint8_t *, uint8_t *, uint32_t);
46*2a1ad637SFrançois Tigeot 
47*2a1ad637SFrançois Tigeot #define FEEDMIXER_DECLARE(SIGN, BIT, ENDIAN)				\
48*2a1ad637SFrançois Tigeot static void								\
49*2a1ad637SFrançois Tigeot feed_mixer_##SIGN##BIT##ENDIAN(uint8_t *src, uint8_t *dst,		\
50*2a1ad637SFrançois Tigeot     uint32_t count)							\
51*2a1ad637SFrançois Tigeot {									\
52*2a1ad637SFrançois Tigeot 	intpcm##BIT##_t z;						\
53*2a1ad637SFrançois Tigeot 	intpcm_t x, y;							\
54*2a1ad637SFrançois Tigeot 									\
55*2a1ad637SFrançois Tigeot 	src += count;							\
56*2a1ad637SFrançois Tigeot 	dst += count;							\
57*2a1ad637SFrançois Tigeot 									\
58*2a1ad637SFrançois Tigeot 	do {								\
59*2a1ad637SFrançois Tigeot 		src -= PCM_##BIT##_BPS;					\
60*2a1ad637SFrançois Tigeot 		dst -= PCM_##BIT##_BPS;					\
61*2a1ad637SFrançois Tigeot 		count -= PCM_##BIT##_BPS;				\
62*2a1ad637SFrançois Tigeot 		x = PCM_READ_##SIGN##BIT##_##ENDIAN(src);		\
63*2a1ad637SFrançois Tigeot 		y = PCM_READ_##SIGN##BIT##_##ENDIAN(dst);		\
64*2a1ad637SFrançois Tigeot 		z = INTPCM##BIT##_T(x) + y;				\
65*2a1ad637SFrançois Tigeot 		x = PCM_CLAMP_##SIGN##BIT(z);				\
66*2a1ad637SFrançois Tigeot 		_PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x);		\
67*2a1ad637SFrançois Tigeot 	} while (count != 0);						\
68*2a1ad637SFrançois Tigeot }
69*2a1ad637SFrançois Tigeot 
70*2a1ad637SFrançois Tigeot #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
71*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S, 16, LE)
72*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S, 32, LE)
73*2a1ad637SFrançois Tigeot #endif
74*2a1ad637SFrançois Tigeot #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
75*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S, 16, BE)
76*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S, 32, BE)
77*2a1ad637SFrançois Tigeot #endif
78*2a1ad637SFrançois Tigeot #ifdef SND_FEEDER_MULTIFORMAT
79*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S,  8, NE)
80*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S, 24, LE)
81*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(S, 24, BE)
82*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U,  8, NE)
83*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U, 16, LE)
84*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U, 24, LE)
85*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U, 32, LE)
86*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U, 16, BE)
87*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U, 24, BE)
88*2a1ad637SFrançois Tigeot FEEDMIXER_DECLARE(U, 32, BE)
89*2a1ad637SFrançois Tigeot #endif
90*2a1ad637SFrançois Tigeot 
91*2a1ad637SFrançois Tigeot struct feed_mixer_info {
92*2a1ad637SFrançois Tigeot 	uint32_t format;
93*2a1ad637SFrançois Tigeot 	int bps;
94*2a1ad637SFrançois Tigeot 	feed_mixer_t mix;
95*2a1ad637SFrançois Tigeot };
96*2a1ad637SFrançois Tigeot 
97*2a1ad637SFrançois Tigeot #define FEEDMIXER_ENTRY(SIGN, BIT, ENDIAN)				\
98*2a1ad637SFrançois Tigeot 	{								\
99*2a1ad637SFrançois Tigeot 		AFMT_##SIGN##BIT##_##ENDIAN, PCM_##BIT##_BPS,		\
100*2a1ad637SFrançois Tigeot 		feed_mixer_##SIGN##BIT##ENDIAN				\
101*2a1ad637SFrançois Tigeot 	}
102*2a1ad637SFrançois Tigeot 
103*2a1ad637SFrançois Tigeot static struct feed_mixer_info feed_mixer_info_tab[] = {
104*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S,  8, NE),
105*2a1ad637SFrançois Tigeot #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
106*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S, 16, LE),
107*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S, 32, LE),
108*2a1ad637SFrançois Tigeot #endif
109*2a1ad637SFrançois Tigeot #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
110*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S, 16, BE),
111*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S, 32, BE),
112*2a1ad637SFrançois Tigeot #endif
113*2a1ad637SFrançois Tigeot #ifdef SND_FEEDER_MULTIFORMAT
114*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S, 24, LE),
115*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(S, 24, BE),
116*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U,  8, NE),
117*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U, 16, LE),
118*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U, 24, LE),
119*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U, 32, LE),
120*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U, 16, BE),
121*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U, 24, BE),
122*2a1ad637SFrançois Tigeot 	FEEDMIXER_ENTRY(U, 32, BE),
123*2a1ad637SFrançois Tigeot #endif
124*2a1ad637SFrançois Tigeot 	{    AFMT_AC3, PCM_16_BPS, NULL },
125*2a1ad637SFrançois Tigeot 	{ AFMT_MU_LAW,  PCM_8_BPS, feed_mixer_U8NE },	/* dummy */
126*2a1ad637SFrançois Tigeot 	{  AFMT_A_LAW,  PCM_8_BPS, feed_mixer_U8NE }	/* dummy */
127*2a1ad637SFrançois Tigeot };
128*2a1ad637SFrançois Tigeot 
129*2a1ad637SFrançois Tigeot #define FEEDMIXER_TAB_SIZE	((int32_t)				\
130*2a1ad637SFrançois Tigeot 				 (sizeof(feed_mixer_info_tab) /		\
131*2a1ad637SFrançois Tigeot 				  sizeof(feed_mixer_info_tab[0])))
132*2a1ad637SFrançois Tigeot 
133*2a1ad637SFrançois Tigeot #define FEEDMIXER_DATA(i, c)	((void *)				\
134*2a1ad637SFrançois Tigeot 				 ((uintptr_t)((((i) & 0x1f) << 5) |	\
135*2a1ad637SFrançois Tigeot 				 ((c) & 0x1f))))
136*2a1ad637SFrançois Tigeot #define FEEDMIXER_INFOIDX(d)	((uint32_t)((uintptr_t)(d) >> 5) & 0x1f)
137*2a1ad637SFrançois Tigeot #define FEEDMIXER_CHANNELS(d)	((uint32_t)((uintptr_t)(d)) & 0x1f)
138*2a1ad637SFrançois Tigeot 
139*2a1ad637SFrançois Tigeot static int
feed_mixer_init(struct pcm_feeder * f)140*2a1ad637SFrançois Tigeot feed_mixer_init(struct pcm_feeder *f)
141*2a1ad637SFrançois Tigeot {
142*2a1ad637SFrançois Tigeot 	int i;
143*2a1ad637SFrançois Tigeot 
144*2a1ad637SFrançois Tigeot 	if (f->desc->in != f->desc->out)
145*2a1ad637SFrançois Tigeot 		return (EINVAL);
146*2a1ad637SFrançois Tigeot 
147*2a1ad637SFrançois Tigeot 	for (i = 0; i < FEEDMIXER_TAB_SIZE; i++) {
148*2a1ad637SFrançois Tigeot 		if (AFMT_ENCODING(f->desc->in) ==
149*2a1ad637SFrançois Tigeot 		    feed_mixer_info_tab[i].format) {
150*2a1ad637SFrançois Tigeot 		    	f->data =
151*2a1ad637SFrançois Tigeot 			    FEEDMIXER_DATA(i, AFMT_CHANNEL(f->desc->in));
152*2a1ad637SFrançois Tigeot 			return (0);
153*2a1ad637SFrançois Tigeot 		}
154*2a1ad637SFrançois Tigeot 	}
155*2a1ad637SFrançois Tigeot 
156*2a1ad637SFrançois Tigeot 	return (EINVAL);
157*2a1ad637SFrançois Tigeot }
158*2a1ad637SFrançois Tigeot 
159*2a1ad637SFrançois Tigeot static int
feed_mixer_set(struct pcm_feeder * f,int what,int value)160*2a1ad637SFrançois Tigeot feed_mixer_set(struct pcm_feeder *f, int what, int value)
161*2a1ad637SFrançois Tigeot {
162*2a1ad637SFrançois Tigeot 
163*2a1ad637SFrançois Tigeot 	switch (what) {
164*2a1ad637SFrançois Tigeot 	case FEEDMIXER_CHANNELS:
165*2a1ad637SFrançois Tigeot 		if (value < SND_CHN_MIN || value > SND_CHN_MAX)
166*2a1ad637SFrançois Tigeot 			return (EINVAL);
167*2a1ad637SFrançois Tigeot 		f->data = FEEDMIXER_DATA(FEEDMIXER_INFOIDX(f->data), value);
168*2a1ad637SFrançois Tigeot 		break;
169*2a1ad637SFrançois Tigeot 	default:
170*2a1ad637SFrançois Tigeot 		return (EINVAL);
171*2a1ad637SFrançois Tigeot 		break;
172*2a1ad637SFrançois Tigeot 	}
173*2a1ad637SFrançois Tigeot 
174*2a1ad637SFrançois Tigeot 	return (0);
175*2a1ad637SFrançois Tigeot }
176*2a1ad637SFrançois Tigeot 
177*2a1ad637SFrançois Tigeot static __inline int
feed_mixer_rec(struct pcm_channel * c)178*2a1ad637SFrançois Tigeot feed_mixer_rec(struct pcm_channel *c)
179*2a1ad637SFrançois Tigeot {
180*2a1ad637SFrançois Tigeot 	struct pcm_channel *ch;
181*2a1ad637SFrançois Tigeot 	struct snd_dbuf *b, *bs;
182*2a1ad637SFrançois Tigeot 	uint32_t cnt, maxfeed;
183*2a1ad637SFrançois Tigeot 	int rdy;
184*2a1ad637SFrançois Tigeot 
185*2a1ad637SFrançois Tigeot 	/*
186*2a1ad637SFrançois Tigeot 	 * Reset ready and moving pointer. We're not using bufsoft
187*2a1ad637SFrançois Tigeot 	 * anywhere since its sole purpose is to become the primary
188*2a1ad637SFrançois Tigeot 	 * distributor for the recorded buffer and also as an interrupt
189*2a1ad637SFrançois Tigeot 	 * threshold progress indicator.
190*2a1ad637SFrançois Tigeot 	 */
191*2a1ad637SFrançois Tigeot 	b = c->bufsoft;
192*2a1ad637SFrançois Tigeot 	b->rp = 0;
193*2a1ad637SFrançois Tigeot 	b->rl = 0;
194*2a1ad637SFrançois Tigeot 	cnt = sndbuf_getsize(b);
195*2a1ad637SFrançois Tigeot 	maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(b));
196*2a1ad637SFrançois Tigeot 
197*2a1ad637SFrançois Tigeot 	do {
198*2a1ad637SFrançois Tigeot 		cnt = FEEDER_FEED(c->feeder->source, c, b->tmpbuf,
199*2a1ad637SFrançois Tigeot 		    min(cnt, maxfeed), c->bufhard);
200*2a1ad637SFrançois Tigeot 		if (cnt != 0) {
201*2a1ad637SFrançois Tigeot 			sndbuf_acquire(b, b->tmpbuf, cnt);
202*2a1ad637SFrançois Tigeot 			cnt = sndbuf_getfree(b);
203*2a1ad637SFrançois Tigeot 		}
204*2a1ad637SFrançois Tigeot 	} while (cnt != 0);
205*2a1ad637SFrançois Tigeot 
206*2a1ad637SFrançois Tigeot 	/* Not enough data */
207*2a1ad637SFrançois Tigeot 	if (b->rl < sndbuf_getalign(b)) {
208*2a1ad637SFrançois Tigeot 		b->rl = 0;
209*2a1ad637SFrançois Tigeot 		return (0);
210*2a1ad637SFrançois Tigeot 	}
211*2a1ad637SFrançois Tigeot 
212*2a1ad637SFrançois Tigeot 	/*
213*2a1ad637SFrançois Tigeot 	 * Keep track of ready and moving pointer since we will use
214*2a1ad637SFrançois Tigeot 	 * bufsoft over and over again, pretending nothing has happened.
215*2a1ad637SFrançois Tigeot 	 */
216*2a1ad637SFrançois Tigeot 	rdy = b->rl;
217*2a1ad637SFrançois Tigeot 
218*2a1ad637SFrançois Tigeot 	CHN_FOREACH(ch, c, children.busy) {
219*2a1ad637SFrançois Tigeot 		CHN_LOCK(ch);
220*2a1ad637SFrançois Tigeot 		if (CHN_STOPPED(ch) || (ch->flags & CHN_F_DIRTY)) {
221*2a1ad637SFrançois Tigeot 			CHN_UNLOCK(ch);
222*2a1ad637SFrançois Tigeot 			continue;
223*2a1ad637SFrançois Tigeot 		}
224*2a1ad637SFrançois Tigeot #ifdef SND_DEBUG
225*2a1ad637SFrançois Tigeot 		if ((c->flags & CHN_F_DIRTY) && VCHAN_SYNC_REQUIRED(ch)) {
226*2a1ad637SFrançois Tigeot 			if (vchan_sync(ch) != 0) {
227*2a1ad637SFrançois Tigeot 				CHN_UNLOCK(ch);
228*2a1ad637SFrançois Tigeot 				continue;
229*2a1ad637SFrançois Tigeot 			}
230*2a1ad637SFrançois Tigeot 		}
231*2a1ad637SFrançois Tigeot #endif
232*2a1ad637SFrançois Tigeot 		bs = ch->bufsoft;
233*2a1ad637SFrançois Tigeot 		if (ch->flags & CHN_F_MMAP)
234*2a1ad637SFrançois Tigeot 			sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
235*2a1ad637SFrançois Tigeot 		cnt = sndbuf_getfree(bs);
236*2a1ad637SFrançois Tigeot 		if (cnt < sndbuf_getalign(bs)) {
237*2a1ad637SFrançois Tigeot 			CHN_UNLOCK(ch);
238*2a1ad637SFrançois Tigeot 			continue;
239*2a1ad637SFrançois Tigeot 		}
240*2a1ad637SFrançois Tigeot 		maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(bs));
241*2a1ad637SFrançois Tigeot 		do {
242*2a1ad637SFrançois Tigeot 			cnt = FEEDER_FEED(ch->feeder, ch, bs->tmpbuf,
243*2a1ad637SFrançois Tigeot 			    min(cnt, maxfeed), b);
244*2a1ad637SFrançois Tigeot 			if (cnt != 0) {
245*2a1ad637SFrançois Tigeot 				sndbuf_acquire(bs, bs->tmpbuf, cnt);
246*2a1ad637SFrançois Tigeot 				cnt = sndbuf_getfree(bs);
247*2a1ad637SFrançois Tigeot 			}
248*2a1ad637SFrançois Tigeot 		} while (cnt != 0);
249*2a1ad637SFrançois Tigeot 		/*
250*2a1ad637SFrançois Tigeot 		 * Not entirely flushed out...
251*2a1ad637SFrançois Tigeot 		 */
252*2a1ad637SFrançois Tigeot 		if (b->rl != 0)
253*2a1ad637SFrançois Tigeot 			ch->xruns++;
254*2a1ad637SFrançois Tigeot 		CHN_UNLOCK(ch);
255*2a1ad637SFrançois Tigeot 		/*
256*2a1ad637SFrançois Tigeot 		 * Rewind buffer position for next virtual channel.
257*2a1ad637SFrançois Tigeot 		 */
258*2a1ad637SFrançois Tigeot 		b->rp = 0;
259*2a1ad637SFrançois Tigeot 		b->rl = rdy;
260*2a1ad637SFrançois Tigeot 	}
261*2a1ad637SFrançois Tigeot 
262*2a1ad637SFrançois Tigeot 	/*
263*2a1ad637SFrançois Tigeot 	 * Set ready pointer to indicate that our children are ready
264*2a1ad637SFrançois Tigeot 	 * to be woken up, also as an interrupt threshold progress
265*2a1ad637SFrançois Tigeot 	 * indicator.
266*2a1ad637SFrançois Tigeot 	 */
267*2a1ad637SFrançois Tigeot 	b->rl = 1;
268*2a1ad637SFrançois Tigeot 
269*2a1ad637SFrançois Tigeot 	c->flags &= ~CHN_F_DIRTY;
270*2a1ad637SFrançois Tigeot 
271*2a1ad637SFrançois Tigeot 	/*
272*2a1ad637SFrançois Tigeot 	 * Return 0 to bail out early from sndbuf_feed() loop.
273*2a1ad637SFrançois Tigeot 	 * No need to increase feedcount counter since part of this
274*2a1ad637SFrançois Tigeot 	 * feeder chains already include feed_root().
275*2a1ad637SFrançois Tigeot 	 */
276*2a1ad637SFrançois Tigeot 	return (0);
277*2a1ad637SFrançois Tigeot }
278*2a1ad637SFrançois Tigeot 
279*2a1ad637SFrançois Tigeot static int
feed_mixer_feed(struct pcm_feeder * f,struct pcm_channel * c,uint8_t * b,uint32_t count,void * source)280*2a1ad637SFrançois Tigeot feed_mixer_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
281*2a1ad637SFrançois Tigeot     uint32_t count, void *source)
282*2a1ad637SFrançois Tigeot {
283*2a1ad637SFrançois Tigeot 	struct feed_mixer_info *info;
284*2a1ad637SFrançois Tigeot 	struct snd_dbuf *src = source;
285*2a1ad637SFrançois Tigeot 	struct pcm_channel *ch;
286*2a1ad637SFrançois Tigeot 	uint32_t cnt, mcnt, rcnt, sz;
287*2a1ad637SFrançois Tigeot 	int passthrough;
288*2a1ad637SFrançois Tigeot 	uint8_t *tmp;
289*2a1ad637SFrançois Tigeot 
290*2a1ad637SFrançois Tigeot 	if (c->direction == PCMDIR_REC)
291*2a1ad637SFrançois Tigeot 		return (feed_mixer_rec(c));
292*2a1ad637SFrançois Tigeot 
293*2a1ad637SFrançois Tigeot 	sz = sndbuf_getsize(src);
294*2a1ad637SFrançois Tigeot 	if (sz < count)
295*2a1ad637SFrançois Tigeot 		count = sz;
296*2a1ad637SFrançois Tigeot 
297*2a1ad637SFrançois Tigeot 	info = &feed_mixer_info_tab[FEEDMIXER_INFOIDX(f->data)];
298*2a1ad637SFrançois Tigeot 	sz = info->bps * FEEDMIXER_CHANNELS(f->data);
299*2a1ad637SFrançois Tigeot 	count = SND_FXROUND(count, sz);
300*2a1ad637SFrançois Tigeot 	if (count < sz)
301*2a1ad637SFrançois Tigeot 		return (0);
302*2a1ad637SFrançois Tigeot 
303*2a1ad637SFrançois Tigeot 	/*
304*2a1ad637SFrançois Tigeot 	 * We are going to use our source as a temporary buffer since it's
305*2a1ad637SFrançois Tigeot 	 * got no other purpose.  We obtain our data by traversing the channel
306*2a1ad637SFrançois Tigeot 	 * list of children and calling mixer function to mix count bytes from
307*2a1ad637SFrançois Tigeot 	 * each into our destination buffer, b.
308*2a1ad637SFrançois Tigeot 	 */
309*2a1ad637SFrançois Tigeot 	tmp = sndbuf_getbuf(src);
310*2a1ad637SFrançois Tigeot 	rcnt = 0;
311*2a1ad637SFrançois Tigeot 	mcnt = 0;
312*2a1ad637SFrançois Tigeot 	passthrough = 0;	/* 'passthrough' / 'exclusive' marker */
313*2a1ad637SFrançois Tigeot 
314*2a1ad637SFrançois Tigeot 	CHN_FOREACH(ch, c, children.busy) {
315*2a1ad637SFrançois Tigeot 		CHN_LOCK(ch);
316*2a1ad637SFrançois Tigeot 		if (CHN_STOPPED(ch) || (ch->flags & CHN_F_DIRTY)) {
317*2a1ad637SFrançois Tigeot 			CHN_UNLOCK(ch);
318*2a1ad637SFrançois Tigeot 			continue;
319*2a1ad637SFrançois Tigeot 		}
320*2a1ad637SFrançois Tigeot #ifdef SND_DEBUG
321*2a1ad637SFrançois Tigeot 		if ((c->flags & CHN_F_DIRTY) && VCHAN_SYNC_REQUIRED(ch)) {
322*2a1ad637SFrançois Tigeot 			if (vchan_sync(ch) != 0) {
323*2a1ad637SFrançois Tigeot 				CHN_UNLOCK(ch);
324*2a1ad637SFrançois Tigeot 				continue;
325*2a1ad637SFrançois Tigeot 			}
326*2a1ad637SFrançois Tigeot 		}
327*2a1ad637SFrançois Tigeot #endif
328*2a1ad637SFrançois Tigeot 		if ((ch->flags & CHN_F_MMAP) && !(ch->flags & CHN_F_CLOSING))
329*2a1ad637SFrançois Tigeot 			sndbuf_acquire(ch->bufsoft, NULL,
330*2a1ad637SFrançois Tigeot 			    sndbuf_getfree(ch->bufsoft));
331*2a1ad637SFrançois Tigeot 		if (info->mix == NULL) {
332*2a1ad637SFrançois Tigeot 			/*
333*2a1ad637SFrançois Tigeot 			 * Passthrough. Dump the first digital/passthrough
334*2a1ad637SFrançois Tigeot 			 * channel into destination buffer, and the rest into
335*2a1ad637SFrançois Tigeot 			 * nothingness (mute effect).
336*2a1ad637SFrançois Tigeot 			 */
337*2a1ad637SFrançois Tigeot 			if (passthrough == 0 &&
338*2a1ad637SFrançois Tigeot 			    (ch->format & AFMT_PASSTHROUGH)) {
339*2a1ad637SFrançois Tigeot 				rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
340*2a1ad637SFrançois Tigeot 				    b, count, ch->bufsoft), sz);
341*2a1ad637SFrançois Tigeot 				passthrough = 1;
342*2a1ad637SFrançois Tigeot 			} else
343*2a1ad637SFrançois Tigeot 				FEEDER_FEED(ch->feeder, ch, tmp, count,
344*2a1ad637SFrançois Tigeot 				    ch->bufsoft);
345*2a1ad637SFrançois Tigeot 		} else if (c->flags & CHN_F_EXCLUSIVE) {
346*2a1ad637SFrançois Tigeot 			/*
347*2a1ad637SFrançois Tigeot 			 * Exclusive. Dump the first 'exclusive' channel into
348*2a1ad637SFrançois Tigeot 			 * destination buffer, and the rest into nothingness
349*2a1ad637SFrançois Tigeot 			 * (mute effect).
350*2a1ad637SFrançois Tigeot 			 */
351*2a1ad637SFrançois Tigeot 			if (passthrough == 0 && (ch->flags & CHN_F_EXCLUSIVE)) {
352*2a1ad637SFrançois Tigeot 				rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
353*2a1ad637SFrançois Tigeot 				    b, count, ch->bufsoft), sz);
354*2a1ad637SFrançois Tigeot 				passthrough = 1;
355*2a1ad637SFrançois Tigeot 			} else
356*2a1ad637SFrançois Tigeot 				FEEDER_FEED(ch->feeder, ch, tmp, count,
357*2a1ad637SFrançois Tigeot 				    ch->bufsoft);
358*2a1ad637SFrançois Tigeot 		} else {
359*2a1ad637SFrançois Tigeot 			if (rcnt == 0) {
360*2a1ad637SFrançois Tigeot 				rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
361*2a1ad637SFrançois Tigeot 				    b, count, ch->bufsoft), sz);
362*2a1ad637SFrançois Tigeot 				mcnt = count - rcnt;
363*2a1ad637SFrançois Tigeot 			} else {
364*2a1ad637SFrançois Tigeot 				cnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
365*2a1ad637SFrançois Tigeot 				    tmp, count, ch->bufsoft), sz);
366*2a1ad637SFrançois Tigeot 				if (cnt != 0) {
367*2a1ad637SFrançois Tigeot 					if (mcnt != 0) {
368*2a1ad637SFrançois Tigeot 						memset(b + rcnt,
369*2a1ad637SFrançois Tigeot 						    sndbuf_zerodata(
370*2a1ad637SFrançois Tigeot 						    f->desc->out), mcnt);
371*2a1ad637SFrançois Tigeot 						mcnt = 0;
372*2a1ad637SFrançois Tigeot 					}
373*2a1ad637SFrançois Tigeot 					info->mix(tmp, b, cnt);
374*2a1ad637SFrançois Tigeot 					if (cnt > rcnt)
375*2a1ad637SFrançois Tigeot 						rcnt = cnt;
376*2a1ad637SFrançois Tigeot 				}
377*2a1ad637SFrançois Tigeot 			}
378*2a1ad637SFrançois Tigeot 		}
379*2a1ad637SFrançois Tigeot 		CHN_UNLOCK(ch);
380*2a1ad637SFrançois Tigeot 	}
381*2a1ad637SFrançois Tigeot 
382*2a1ad637SFrançois Tigeot 	if (++c->feedcount == 0)
383*2a1ad637SFrançois Tigeot 		c->feedcount = 2;
384*2a1ad637SFrançois Tigeot 
385*2a1ad637SFrançois Tigeot 	c->flags &= ~CHN_F_DIRTY;
386*2a1ad637SFrançois Tigeot 
387*2a1ad637SFrançois Tigeot 	return (rcnt);
388*2a1ad637SFrançois Tigeot }
389*2a1ad637SFrançois Tigeot 
390*2a1ad637SFrançois Tigeot static struct pcm_feederdesc feeder_mixer_desc[] = {
391*2a1ad637SFrançois Tigeot 	{ FEEDER_MIXER, 0, 0, 0, 0 },
392*2a1ad637SFrançois Tigeot 	{ 0, 0, 0, 0, 0 }
393*2a1ad637SFrançois Tigeot };
394*2a1ad637SFrançois Tigeot 
395*2a1ad637SFrançois Tigeot static kobj_method_t feeder_mixer_methods[] = {
396*2a1ad637SFrançois Tigeot 	KOBJMETHOD(feeder_init,		feed_mixer_init),
397*2a1ad637SFrançois Tigeot 	KOBJMETHOD(feeder_set,		feed_mixer_set),
398*2a1ad637SFrançois Tigeot 	KOBJMETHOD(feeder_feed,		feed_mixer_feed),
399*2a1ad637SFrançois Tigeot 	KOBJMETHOD_END
400*2a1ad637SFrançois Tigeot };
401*2a1ad637SFrançois Tigeot 
402*2a1ad637SFrançois Tigeot FEEDER_DECLARE(feeder_mixer, NULL);
403