xref: /freebsd/sys/dev/sound/pcm/vchan.c (revision c4f6a2a9e1b1879b618c436ab4f56ff75c73a0f5)
1 /*
2  * Copyright (c) 2001 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <dev/sound/pcm/sound.h>
28 #include <dev/sound/pcm/vchan.h>
29 #include "feeder_if.h"
30 
31 SND_DECLARE_FILE("$FreeBSD$");
32 
33 struct vchinfo {
34 	u_int32_t spd, fmt, blksz, bps, run;
35 	struct pcm_channel *channel, *parent;
36 	struct pcmchan_caps caps;
37 };
38 
39 static u_int32_t vchan_fmt[] = {
40 	AFMT_S16_LE,
41 	AFMT_STEREO | AFMT_S16_LE,
42 	0
43 };
44 
45 static int
46 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count)
47 {
48 	/*
49 	 * to is the output buffer, tmp is the input buffer
50 	 * count is the number of 16bit samples to mix
51 	 */
52 	int i;
53 	int x;
54 
55 	for(i = 0; i < count; i++) {
56 		x = to[i];
57 		x += tmp[i];
58 		if (x < -32768) {
59 			/* printf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
60 			x = -32768;
61 		}
62 		if (x > 32767) {
63 			/* printf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
64 			x = 32767;
65 		}
66 		to[i] = x & 0x0000ffff;
67 	}
68 	return 0;
69 }
70 
71 static int
72 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
73 {
74 	/* we're going to abuse things a bit */
75 	struct snd_dbuf *src = source;
76 	struct pcmchan_children *cce;
77 	struct pcm_channel *ch;
78 	int16_t *tmp, *dst;
79 	unsigned int cnt;
80 
81 	KASSERT(sndbuf_getsize(src) >= count, ("bad bufsize"));
82 	count &= ~1;
83 	bzero(b, count);
84 
85 	/*
86 	 * we are going to use our source as a temporary buffer since it's
87 	 * got no other purpose.  we obtain our data by traversing the channel
88 	 * list of children and calling vchan_mix_* to mix count bytes from each
89 	 * into our destination buffer, b
90 	 */
91 	dst = (int16_t *)b;
92 	tmp = (int16_t *)sndbuf_getbuf(src);
93 	bzero(tmp, count);
94 	SLIST_FOREACH(cce, &c->children, link) {
95 		ch = cce->channel;
96 		if (ch->flags & CHN_F_TRIGGERED) {
97 			if (ch->flags & CHN_F_MAPPED)
98 				sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
99 			cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
100 			vchan_mix_s16(dst, tmp, cnt / 2);
101 		}
102 	}
103 
104 	return count;
105 }
106 
107 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
108 	{FEEDER_MIXER, AFMT_S16_LE, AFMT_S16_LE, 0},
109 	{FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
110 	{0},
111 };
112 static kobj_method_t feeder_vchan_s16_methods[] = {
113     	KOBJMETHOD(feeder_feed,		feed_vchan_s16),
114 	{ 0, 0 }
115 };
116 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
117 
118 /************************************************************/
119 
120 static void *
121 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
122 {
123 	struct vchinfo *ch;
124 	struct pcm_channel *parent = devinfo;
125 
126 	KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
127 	ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
128 	ch->parent = parent;
129 	ch->channel = c;
130 	ch->fmt = AFMT_U8;
131 	ch->spd = DSP_DEFAULT_SPEED;
132 	ch->blksz = 2048;
133 
134 	c->flags |= CHN_F_VIRTUAL;
135 
136 	return ch;
137 }
138 
139 static int
140 vchan_free(kobj_t obj, void *data)
141 {
142 	return 0;
143 }
144 
145 static int
146 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
147 {
148 	struct vchinfo *ch = data;
149 	struct pcm_channel *parent = ch->parent;
150 
151 	ch->fmt = format;
152 	ch->bps = 1;
153 	ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
154 	ch->bps <<= (ch->fmt & AFMT_16BIT)? 1 : 0;
155 	ch->bps <<= (ch->fmt & AFMT_32BIT)? 2 : 0;
156 	chn_notify(parent, CHN_N_FORMAT);
157 	return 0;
158 }
159 
160 static int
161 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
162 {
163 	struct vchinfo *ch = data;
164 	struct pcm_channel *parent = ch->parent;
165 
166 	ch->spd = speed;
167 	chn_notify(parent, CHN_N_RATE);
168 	return speed;
169 }
170 
171 static int
172 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
173 {
174 	struct vchinfo *ch = data;
175 	struct pcm_channel *parent = ch->parent;
176 	int prate, crate;
177 
178 	ch->blksz = blocksize;
179 	chn_notify(parent, CHN_N_BLOCKSIZE);
180 
181 	crate = ch->spd * ch->bps;
182 	prate = sndbuf_getspd(parent->bufhard) * sndbuf_getbps(parent->bufhard);
183 	blocksize = sndbuf_getblksz(parent->bufhard);
184 	blocksize *= prate;
185 	blocksize /= crate;
186 
187 	return blocksize;
188 }
189 
190 static int
191 vchan_trigger(kobj_t obj, void *data, int go)
192 {
193 	struct vchinfo *ch = data;
194 	struct pcm_channel *parent = ch->parent;
195 
196 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
197 		return 0;
198 
199 	ch->run = (go == PCMTRIG_START)? 1 : 0;
200 	chn_notify(parent, CHN_N_TRIGGER);
201 
202 	return 0;
203 }
204 
205 static struct pcmchan_caps *
206 vchan_getcaps(kobj_t obj, void *data)
207 {
208 	struct vchinfo *ch = data;
209 
210 	ch->caps.minspeed = sndbuf_getspd(ch->parent->bufhard);
211 	ch->caps.maxspeed = ch->caps.minspeed;
212 	ch->caps.fmtlist = vchan_fmt;
213 	ch->caps.caps = 0;
214 
215 	return &ch->caps;
216 }
217 
218 static kobj_method_t vchan_methods[] = {
219     	KOBJMETHOD(channel_init,		vchan_init),
220     	KOBJMETHOD(channel_free,		vchan_free),
221     	KOBJMETHOD(channel_setformat,		vchan_setformat),
222     	KOBJMETHOD(channel_setspeed,		vchan_setspeed),
223     	KOBJMETHOD(channel_setblocksize,	vchan_setblocksize),
224     	KOBJMETHOD(channel_trigger,		vchan_trigger),
225     	KOBJMETHOD(channel_getcaps,		vchan_getcaps),
226 	{ 0, 0 }
227 };
228 CHANNEL_DECLARE(vchan);
229 
230 /* virtual channel interface */
231 
232 int
233 vchan_create(struct pcm_channel *parent)
234 {
235     	struct snddev_info *d = parent->parentsnddev;
236 	struct pcmchan_children *pce;
237 	struct pcm_channel *child;
238 	int err, first;
239 
240 	CHN_LOCK(parent);
241 	if (!(parent->flags & CHN_F_BUSY)) {
242 		CHN_UNLOCK(parent);
243 		return EBUSY;
244 	}
245 
246 	pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
247 	if (!pce) {
248 		CHN_UNLOCK(parent);
249 		return ENOMEM;
250 	}
251 
252 	/* create a new playback channel */
253 	child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
254 	if (!child) {
255 		free(pce, M_DEVBUF);
256 		CHN_UNLOCK(parent);
257 		return ENODEV;
258 	}
259 
260 	first = SLIST_EMPTY(&parent->children);
261 	/* add us to our parent channel's children */
262 	pce->channel = child;
263 	SLIST_INSERT_HEAD(&parent->children, pce, link);
264 	CHN_UNLOCK(parent);
265 
266 	/* add us to our grandparent's channel list */
267 	err = pcm_chn_add(d, child, !first);
268 	if (err) {
269 		pcm_chn_destroy(child);
270 		free(pce, M_DEVBUF);
271 	}
272 
273 	/* XXX gross ugly hack, kill murder death */
274 	if (first && !err) {
275 		err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
276 		if (err)
277 			printf("chn_reset: %d\n", err);
278 		err = chn_setspeed(parent, 44100);
279 		if (err)
280 			printf("chn_setspeed: %d\n", err);
281 	}
282 
283 	return err;
284 }
285 
286 int
287 vchan_destroy(struct pcm_channel *c)
288 {
289 	struct pcm_channel *parent = c->parentchannel;
290     	struct snddev_info *d = parent->parentsnddev;
291 	struct pcmchan_children *pce;
292 	int err, last;
293 
294 	CHN_LOCK(parent);
295 	if (!(parent->flags & CHN_F_BUSY)) {
296 		CHN_UNLOCK(parent);
297 		return EBUSY;
298 	}
299 	if (SLIST_EMPTY(&parent->children)) {
300 		CHN_UNLOCK(parent);
301 		return EINVAL;
302 	}
303 
304 	/* remove us from our parent's children list */
305 	SLIST_FOREACH(pce, &parent->children, link) {
306 		if (pce->channel == c)
307 			goto gotch;
308 	}
309 	CHN_UNLOCK(parent);
310 	return EINVAL;
311 gotch:
312 	SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
313 	free(pce, M_DEVBUF);
314 
315 	last = SLIST_EMPTY(&parent->children);
316 	if (last)
317 		parent->flags &= ~CHN_F_BUSY;
318 
319 	/* remove us from our grantparent's channel list */
320 	err = pcm_chn_remove(d, c, !last);
321 	if (err)
322 		return err;
323 
324 	CHN_UNLOCK(parent);
325 	/* destroy ourselves */
326 	err = pcm_chn_destroy(c);
327 
328 	return err;
329 }
330 
331 int
332 vchan_initsys(device_t dev)
333 {
334 #ifdef SND_DYNSYSCTL
335 	struct snddev_info *d;
336 
337     	d = device_get_softc(dev);
338 	SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
339             OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
340 	    sysctl_hw_snd_vchans, "I", "");
341 #endif
342 
343 	return 0;
344 }
345 
346 
347