Add forgotten semi-colon.
[dragonfly.git] / sys / dev / sound / pcm / vchan.c
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  * $FreeBSD: src/sys/dev/sound/pcm/vchan.c,v 1.5.2.4 2003/02/11 15:54:16 orion Exp $
27  * $DragonFly: src/sys/dev/sound/pcm/vchan.c,v 1.2 2003/06/17 04:28:31 dillon Exp $
28  */
29
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/pcm/vchan.h>
32 #include "feeder_if.h"
33
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/vchan.c,v 1.2 2003/06/17 04:28:31 dillon Exp $");
35
36 struct vchinfo {
37         u_int32_t spd, fmt, blksz, bps, run;
38         struct pcm_channel *channel, *parent;
39         struct pcmchan_caps caps;
40 };
41
42 static u_int32_t vchan_fmt[] = {
43         AFMT_STEREO | AFMT_S16_LE,
44         0
45 };
46
47 static int
48 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count)
49 {
50         /*
51          * to is the output buffer, tmp is the input buffer
52          * count is the number of 16bit samples to mix
53          */
54         int i;
55         int x;
56
57         for(i = 0; i < count; i++) {
58                 x = to[i];
59                 x += tmp[i];
60                 if (x < -32768) {
61                         /* printf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
62                         x = -32768;
63                 }
64                 if (x > 32767) {
65                         /* printf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
66                         x = 32767;
67                 }
68                 to[i] = x & 0x0000ffff;
69         }
70         return 0;
71 }
72
73 static int
74 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
75 {
76         /* we're going to abuse things a bit */
77         struct snd_dbuf *src = source;
78         struct pcmchan_children *cce;
79         struct pcm_channel *ch;
80         int16_t *tmp, *dst;
81         unsigned int cnt;
82
83         KASSERT(sndbuf_getsize(src) >= count, ("bad bufsize"));
84         count &= ~1;
85         bzero(b, count);
86
87         /*
88          * we are going to use our source as a temporary buffer since it's
89          * got no other purpose.  we obtain our data by traversing the channel
90          * list of children and calling vchan_mix_* to mix count bytes from each
91          * into our destination buffer, b
92          */
93         dst = (int16_t *)b;
94         tmp = (int16_t *)sndbuf_getbuf(src);
95         bzero(tmp, count);
96         SLIST_FOREACH(cce, &c->children, link) {
97                 ch = cce->channel;
98                 if (ch->flags & CHN_F_TRIGGERED) {
99                         if (ch->flags & CHN_F_MAPPED)
100                                 sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
101                         cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
102                         vchan_mix_s16(dst, tmp, cnt / 2);
103                 }
104         }
105
106         return count;
107 }
108
109 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
110         {FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
111         {0},
112 };
113 static kobj_method_t feeder_vchan_s16_methods[] = {
114         KOBJMETHOD(feeder_feed,         feed_vchan_s16),
115         { 0, 0 }
116 };
117 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
118
119 /************************************************************/
120
121 static void *
122 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
123 {
124         struct vchinfo *ch;
125         struct pcm_channel *parent = devinfo;
126
127         KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
128         ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
129         ch->parent = parent;
130         ch->channel = c;
131         ch->fmt = AFMT_U8;
132         ch->spd = DSP_DEFAULT_SPEED;
133         ch->blksz = 2048;
134
135         c->flags |= CHN_F_VIRTUAL;
136
137         return ch;
138 }
139
140 static int
141 vchan_free(kobj_t obj, void *data)
142 {
143         return 0;
144 }
145
146 static int
147 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
148 {
149         struct vchinfo *ch = data;
150         struct pcm_channel *parent = ch->parent;
151
152         ch->fmt = format;
153         ch->bps = 1;
154         ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
155         ch->bps <<= (ch->fmt & AFMT_16BIT)? 1 : 0;
156         ch->bps <<= (ch->fmt & AFMT_32BIT)? 2 : 0;
157         chn_notify(parent, CHN_N_FORMAT);
158         return 0;
159 }
160
161 static int
162 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
163 {
164         struct vchinfo *ch = data;
165         struct pcm_channel *parent = ch->parent;
166
167         ch->spd = speed;
168         chn_notify(parent, CHN_N_RATE);
169         return speed;
170 }
171
172 static int
173 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
174 {
175         struct vchinfo *ch = data;
176         struct pcm_channel *parent = ch->parent;
177         int prate, crate;
178
179         ch->blksz = blocksize;
180         chn_notify(parent, CHN_N_BLOCKSIZE);
181
182         crate = ch->spd * ch->bps;
183         prate = sndbuf_getspd(parent->bufhard) * sndbuf_getbps(parent->bufhard);
184         blocksize = sndbuf_getblksz(parent->bufhard);
185         blocksize *= prate;
186         blocksize /= crate;
187
188         return blocksize;
189 }
190
191 static int
192 vchan_trigger(kobj_t obj, void *data, int go)
193 {
194         struct vchinfo *ch = data;
195         struct pcm_channel *parent = ch->parent;
196
197         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
198                 return 0;
199
200         ch->run = (go == PCMTRIG_START)? 1 : 0;
201         chn_notify(parent, CHN_N_TRIGGER);
202
203         return 0;
204 }
205
206 static struct pcmchan_caps *
207 vchan_getcaps(kobj_t obj, void *data)
208 {
209         struct vchinfo *ch = data;
210
211         ch->caps.minspeed = sndbuf_getspd(ch->parent->bufhard);
212         ch->caps.maxspeed = ch->caps.minspeed;
213         ch->caps.fmtlist = vchan_fmt;
214         ch->caps.caps = 0;
215
216         return &ch->caps;
217 }
218
219 static kobj_method_t vchan_methods[] = {
220         KOBJMETHOD(channel_init,                vchan_init),
221         KOBJMETHOD(channel_free,                vchan_free),
222         KOBJMETHOD(channel_setformat,           vchan_setformat),
223         KOBJMETHOD(channel_setspeed,            vchan_setspeed),
224         KOBJMETHOD(channel_setblocksize,        vchan_setblocksize),
225         KOBJMETHOD(channel_trigger,             vchan_trigger),
226         KOBJMETHOD(channel_getcaps,             vchan_getcaps),
227         { 0, 0 }
228 };
229 CHANNEL_DECLARE(vchan);
230
231 /* virtual channel interface */
232
233 int
234 vchan_create(struct pcm_channel *parent)
235 {
236         struct snddev_info *d = parent->parentsnddev;
237         struct pcmchan_children *pce;
238         struct pcm_channel *child;
239         int err, first;
240
241         CHN_LOCK(parent);
242         if (!(parent->flags & CHN_F_BUSY)) {
243                 CHN_UNLOCK(parent);
244                 return EBUSY;
245         }
246
247         pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
248         if (!pce) {
249                 CHN_UNLOCK(parent);
250                 return ENOMEM;
251         }
252
253         /* create a new playback channel */
254         child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
255         if (!child) {
256                 free(pce, M_DEVBUF);
257                 CHN_UNLOCK(parent);
258                 return ENODEV;
259         }
260
261         first = SLIST_EMPTY(&parent->children);
262         /* add us to our parent channel's children */
263         pce->channel = child;
264         SLIST_INSERT_HEAD(&parent->children, pce, link);
265         CHN_UNLOCK(parent);
266
267         /* add us to our grandparent's channel list */
268         err = pcm_chn_add(d, child, !first);
269         if (err) {
270                 pcm_chn_destroy(child);
271                 free(pce, M_DEVBUF);
272         }
273
274         /* XXX gross ugly hack, kill murder death */
275         if (first && !err) {
276                 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
277                 if (err)
278                         printf("chn_reset: %d\n", err);
279                 err = chn_setspeed(parent, 44100);
280                 if (err)
281                         printf("chn_setspeed: %d\n", err);
282         }
283
284         return err;
285 }
286
287 int
288 vchan_destroy(struct pcm_channel *c)
289 {
290         struct pcm_channel *parent = c->parentchannel;
291         struct snddev_info *d = parent->parentsnddev;
292         struct pcmchan_children *pce;
293         int err, last;
294
295         CHN_LOCK(parent);
296         if (!(parent->flags & CHN_F_BUSY)) {
297                 CHN_UNLOCK(parent);
298                 return EBUSY;
299         }
300         if (SLIST_EMPTY(&parent->children)) {
301                 CHN_UNLOCK(parent);
302                 return EINVAL;
303         }
304
305         /* remove us from our parent's children list */
306         SLIST_FOREACH(pce, &parent->children, link) {
307                 if (pce->channel == c)
308                         goto gotch;
309         }
310         CHN_UNLOCK(parent);
311         return EINVAL;
312 gotch:
313         SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
314         free(pce, M_DEVBUF);
315
316         last = SLIST_EMPTY(&parent->children);
317         if (last)
318                 parent->flags &= ~CHN_F_BUSY;
319
320         /* remove us from our grantparent's channel list */
321         err = pcm_chn_remove(d, c, !last);
322         if (err)
323                 return err;
324
325         CHN_UNLOCK(parent);
326         /* destroy ourselves */
327         err = pcm_chn_destroy(c);
328
329         return err;
330 }
331
332 int
333 vchan_initsys(device_t dev)
334 {
335 #ifdef SND_DYNSYSCTL
336         struct snddev_info *d;
337
338         d = device_get_softc(dev);
339         SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
340             OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
341             sysctl_hw_snd_vchans, "I", "")
342 #endif
343
344         return 0;
345 }
346
347