2 * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/sound/pcm/vchan.c,v 1.17.2.5 2007/02/04 06:17:14 ariff Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/vchan.c,v 1.8 2008/01/06 16:55:51 swildner Exp $
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/pcm/vchan.h>
32 #include "feeder_if.h"
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/vchan.c,v 1.8 2008/01/06 16:55:51 swildner Exp $");
39 #define VCHAN_DEFAULT_SPEED 48000
41 extern int feeder_rate_ratemin;
42 extern int feeder_rate_ratemax;
45 u_int32_t spd, fmt, blksz, bps, run;
46 struct pcm_channel *channel, *parent;
47 struct pcmchan_caps caps;
50 static u_int32_t vchan_fmt[] = {
51 AFMT_STEREO | AFMT_S16_LE,
56 * These values are: (1.022**v-1)*65536/(1.022**100-1)
58 * Inspired by inverting opensound (BSDL'd)'s mix_cvt.
60 static int log_mix_cvt[101] = {
61 0, 184, 373, 565, 762, 964, 1170, 1380, 1595, 1814, 2039, 2268, 2503, 2742,
62 2987, 3238, 3493, 3755, 4022, 4295, 4574, 4859, 5151, 5449, 5753, 6064, 6382,
63 6707, 7039, 7379, 7726, 8080, 8442, 8813, 9191, 9578, 9973, 10377, 10790,
64 11212, 11643, 12084, 12534, 12995, 13465, 13946, 14437, 14940, 15453, 15977,
65 16513, 17061, 17621, 18193, 18778, 19376, 19987, 20611, 21249, 21901, 22567,
66 23248, 23944, 24656, 25383, 26126, 26885, 27661, 28454, 29265, 30093, 30940,
67 31805, 32689, 33593, 34517, 35461, 36425, 37411, 38419, 39449, 40501, 41577,
68 42676, 43799, 44947, 46121, 47320, 48546, 49798, 51078, 52387, 53724, 55090,
69 56487, 57914, 59373, 60864, 62387, 63944, 65536
73 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count, int volume)
76 * to is the output buffer, tmp is the input buffer
77 * count is the number of 16bit samples to mix
78 * volume is in range 0-100
85 scale = log_mix_cvt[volume];
86 doscale = volume != 100;
88 for(i = 0; i < count; i++) {
91 x += ((int)tmp[i] * scale) >> 16;
95 /* kprintf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
99 /* kprintf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
102 to[i] = x & 0x0000ffff;
108 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
110 /* we're going to abuse things a bit */
111 struct snd_dbuf *src = source;
112 struct pcmchan_children *cce;
113 struct pcm_channel *ch;
116 unsigned int cnt, rcnt = 0;
120 if (sndbuf_getsize(src) < count)
121 panic("feed_vchan_s16(%s): tmp buffer size %d < count %d, flags = 0x%x",
122 c->name, sndbuf_getsize(src), count, c->flags);
124 sz = sndbuf_getsize(src);
133 * we are going to use our source as a temporary buffer since it's
134 * got no other purpose. we obtain our data by traversing the channel
135 * list of children and calling vchan_mix_* to mix count bytes from each
136 * into our destination buffer, b
139 tmp = (int16_t *)sndbuf_getbuf(src);
141 SLIST_FOREACH(cce, &c->children, link) {
144 if (ch->flags & CHN_F_TRIGGERED) {
145 if (ch->flags & CHN_F_MAPPED)
146 sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
147 cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
148 volume = ch->volume & 0xff; /* XXX do special stereo processing? */
149 volume += (ch->volume >> 8) & 0xff;
151 vchan_mix_s16(dst, tmp, cnt >> 1, volume);
161 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
162 {FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
165 static kobj_method_t feeder_vchan_s16_methods[] = {
166 KOBJMETHOD(feeder_feed, feed_vchan_s16),
169 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
171 /************************************************************/
174 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
177 struct pcm_channel *parent = devinfo;
179 KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
180 ch = kmalloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
184 ch->spd = DSP_DEFAULT_SPEED;
187 c->flags |= CHN_F_VIRTUAL;
193 vchan_free(kobj_t obj, void *data)
195 kfree(data, M_DEVBUF);
200 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
202 struct vchinfo *ch = data;
203 struct pcm_channel *parent = ch->parent;
204 struct pcm_channel *channel = ch->channel;
208 ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
209 if (ch->fmt & AFMT_16BIT)
211 else if (ch->fmt & AFMT_24BIT)
213 else if (ch->fmt & AFMT_32BIT)
216 chn_notify(parent, CHN_N_FORMAT);
218 sndbuf_setfmt(channel->bufsoft, format);
223 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
225 struct vchinfo *ch = data;
226 struct pcm_channel *parent = ch->parent;
227 struct pcm_channel *channel = ch->channel;
232 speed = sndbuf_getspd(parent->bufsoft);
239 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
241 struct vchinfo *ch = data;
242 struct pcm_channel *channel = ch->channel;
243 struct pcm_channel *parent = ch->parent;
244 /* struct pcm_channel *channel = ch->channel; */
247 ch->blksz = blocksize;
248 /* CHN_UNLOCK(channel); */
249 sndbuf_setblksz(channel->bufhard, blocksize);
250 chn_notify(parent, CHN_N_BLOCKSIZE);
252 /* CHN_LOCK(channel); */
254 crate = ch->spd * ch->bps;
255 prate = sndbuf_getspd(parent->bufsoft) * sndbuf_getbps(parent->bufsoft);
256 blocksize = sndbuf_getblksz(parent->bufsoft);
265 vchan_trigger(kobj_t obj, void *data, int go)
267 struct vchinfo *ch = data;
268 struct pcm_channel *parent = ch->parent;
269 struct pcm_channel *channel = ch->channel;
271 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
274 ch->run = (go == PCMTRIG_START)? 1 : 0;
276 chn_notify(parent, CHN_N_TRIGGER);
282 static struct pcmchan_caps *
283 vchan_getcaps(kobj_t obj, void *data)
285 struct vchinfo *ch = data;
287 ch->caps.minspeed = sndbuf_getspd(ch->parent->bufsoft);
288 ch->caps.maxspeed = ch->caps.minspeed;
289 ch->caps.fmtlist = vchan_fmt;
295 static kobj_method_t vchan_methods[] = {
296 KOBJMETHOD(channel_init, vchan_init),
297 KOBJMETHOD(channel_free, vchan_free),
298 KOBJMETHOD(channel_setformat, vchan_setformat),
299 KOBJMETHOD(channel_setspeed, vchan_setspeed),
300 KOBJMETHOD(channel_setblocksize, vchan_setblocksize),
301 KOBJMETHOD(channel_trigger, vchan_trigger),
302 KOBJMETHOD(channel_getcaps, vchan_getcaps),
305 CHANNEL_DECLARE(vchan);
309 * On the fly vchan rate settings
313 sysctl_hw_snd_vchanrate(SYSCTL_HANDLER_ARGS)
315 struct snddev_info *d;
316 struct snddev_channel *sce;
317 struct pcm_channel *c, *ch = NULL, *fake;
318 struct pcmchan_caps *caps;
323 if (!(d->flags & SD_F_AUTOVCHAN) || d->vchancount < 1)
325 if (pcm_inprog(d, 1) != 1 && req->newptr != NULL) {
329 SLIST_FOREACH(sce, &d->channels, link) {
332 if (c->direction == PCMDIR_PLAY) {
333 if (c->flags & CHN_F_VIRTUAL) {
335 if (ch != NULL && ch != c->parentchannel) {
340 if (req->newptr != NULL &&
341 (c->flags & CHN_F_BUSY)) {
346 } else if (c->flags & CHN_F_HAS_VCHAN) {
363 err = sysctl_handle_int(oidp, &newspd, sizeof(newspd), req);
364 if (err == 0 && req->newptr != NULL) {
365 if (newspd < 1 || newspd < feeder_rate_ratemin ||
366 newspd > feeder_rate_ratemax) {
371 caps = chn_getcaps(ch);
372 if (caps == NULL || newspd < caps->minspeed ||
373 newspd > caps->maxspeed) {
378 if (newspd != ch->speed) {
379 err = chn_setspeed(ch, newspd);
381 * Try to avoid FEEDER_RATE on parent channel if the
382 * requested value is not supported by the hardware.
384 if (!err && (ch->feederflags & (1 << FEEDER_RATE))) {
385 newspd = sndbuf_getspd(ch->bufhard);
386 err = chn_setspeed(ch, newspd);
390 fake = pcm_getfakechan(d);
393 fake->speed = newspd;
406 /* virtual channel interface */
409 vchan_create(struct pcm_channel *parent)
411 struct snddev_info *d = parent->parentsnddev;
412 struct pcmchan_children *pce;
413 struct pcm_channel *child, *fake;
414 struct pcmchan_caps *parent_caps;
415 int err, first, speed = 0;
417 if (!(parent->flags & CHN_F_BUSY))
423 pce = kmalloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
425 /* create a new playback channel */
426 child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
428 kfree(pce, M_DEVBUF);
432 pce->channel = child;
434 /* add us to our grandparent's channel list */
436 * XXX maybe we shouldn't always add the dev_t
438 err = pcm_chn_add(d, child);
440 pcm_chn_destroy(child);
441 kfree(pce, M_DEVBUF);
447 /* add us to our parent channel's children */
448 first = SLIST_EMPTY(&parent->children);
449 SLIST_INSERT_HEAD(&parent->children, pce, link);
450 parent->flags |= CHN_F_HAS_VCHAN;
453 parent_caps = chn_getcaps(parent);
454 if (parent_caps == NULL)
458 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
461 fake = pcm_getfakechan(d);
464 * Avoid querying kernel hint, use saved value
475 * This is very sad. Few soundcards advertised as being
476 * able to do (insanely) higher/lower speed, but in
477 * reality, they simply can't. At least, we give user chance
478 * to set sane value via kernel hints or sysctl.
483 r = resource_int_value(device_get_name(parent->dev),
484 device_get_unit(parent->dev),
485 "vchanrate", &speed);
489 * Workaround for sb16 running
490 * poorly at 45k / 49k.
492 switch (parent_caps->maxspeed) {
498 speed = VCHAN_DEFAULT_SPEED;
505 * Limit speed based on driver caps.
506 * This is supposed to help fixed rate, non-VRA
507 * AC97 cards, but.. (see below)
509 if (speed < parent_caps->minspeed)
510 speed = parent_caps->minspeed;
511 if (speed > parent_caps->maxspeed)
512 speed = parent_caps->maxspeed;
515 * We still need to limit the speed between
516 * feeder_rate_ratemin <-> feeder_rate_ratemax. This is
517 * just an escape goat if all of the above failed
520 if (speed < feeder_rate_ratemin)
521 speed = feeder_rate_ratemin;
522 if (speed > feeder_rate_ratemax)
523 speed = feeder_rate_ratemax;
525 err = chn_setspeed(parent, speed);
527 * Try to avoid FEEDER_RATE on parent channel if the
528 * requested value is not supported by the hardware.
530 if (!err && (parent->feederflags & (1 << FEEDER_RATE))) {
531 speed = sndbuf_getspd(parent->bufhard);
532 err = chn_setspeed(parent, speed);
535 if (!err && fake != NULL) {
537 * Save new value to fake channel.
548 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
549 parent->flags &= ~CHN_F_HAS_VCHAN;
551 kfree(pce, M_DEVBUF);
552 if (pcm_chn_remove(d, child) == 0)
553 pcm_chn_destroy(child);
563 vchan_destroy(struct pcm_channel *c)
565 struct pcm_channel *parent = c->parentchannel;
566 struct snddev_info *d = parent->parentsnddev;
567 struct pcmchan_children *pce;
568 struct snddev_channel *sce;
573 if (!(parent->flags & CHN_F_BUSY)) {
577 if (SLIST_EMPTY(&parent->children)) {
582 /* remove us from our parent's children list */
583 SLIST_FOREACH(pce, &parent->children, link) {
584 if (pce->channel == c)
590 SLIST_FOREACH(sce, &d->channels, link) {
591 if (sce->channel == c) {
593 destroy_dev(sce->dsp_devt);
594 sce->dsp_devt = NULL;
596 if (sce->dspW_devt) {
597 destroy_dev(sce->dspW_devt);
598 sce->dspW_devt = NULL;
600 if (sce->audio_devt) {
601 destroy_dev(sce->audio_devt);
602 sce->audio_devt = NULL;
604 if (sce->dspr_devt) {
605 destroy_dev(sce->dspr_devt);
606 sce->dspr_devt = NULL;
612 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
613 kfree(pce, M_DEVBUF);
615 if (SLIST_EMPTY(&parent->children)) {
616 parent->flags &= ~(CHN_F_BUSY | CHN_F_HAS_VCHAN);
618 if (chn_reset(parent, parent->format) == 0)
619 chn_setspeed(parent, spd);
622 /* remove us from our grandparent's channel list */
623 err = pcm_chn_remove(d, c);
626 /* destroy ourselves */
628 err = pcm_chn_destroy(c);
634 vchan_initsys(device_t dev)
637 struct snddev_info *d;
639 d = device_get_softc(dev);
640 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
641 OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
642 sysctl_hw_snd_vchans, "I", "");
644 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
645 OID_AUTO, "vchanrate", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
646 sysctl_hw_snd_vchanrate, "I", "");