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
27 #include <dev/sound/pcm/sound.h>
28 #include <dev/sound/pcm/vchan.h>
29 #include "feeder_if.h"
31 SND_DECLARE_FILE("$FreeBSD$");
36 #define VCHAN_DEFAULT_SPEED 48000
38 extern int feeder_rate_ratemin;
39 extern int feeder_rate_ratemax;
42 u_int32_t spd, fmt, blksz, bps, run;
43 struct pcm_channel *channel, *parent;
44 struct pcmchan_caps caps;
47 static u_int32_t vchan_fmt[] = {
48 AFMT_STEREO | AFMT_S16_LE,
53 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count)
56 * to is the output buffer, tmp is the input buffer
57 * count is the number of 16bit samples to mix
62 for(i = 0; i < count; i++) {
66 /* printf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
70 /* printf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
73 to[i] = x & 0x0000ffff;
79 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
81 /* we're going to abuse things a bit */
82 struct snd_dbuf *src = source;
83 struct pcmchan_children *cce;
84 struct pcm_channel *ch;
87 unsigned int cnt, rcnt = 0;
90 if (sndbuf_getsize(src) < count)
91 panic("feed_vchan_s16(%s): tmp buffer size %d < count %d, flags = 0x%x",
92 c->name, sndbuf_getsize(src), count, c->flags);
94 sz = sndbuf_getsize(src);
103 * we are going to use our source as a temporary buffer since it's
104 * got no other purpose. we obtain our data by traversing the channel
105 * list of children and calling vchan_mix_* to mix count bytes from each
106 * into our destination buffer, b
109 tmp = (int16_t *)sndbuf_getbuf(src);
111 SLIST_FOREACH(cce, &c->children, link) {
114 if (ch->flags & CHN_F_TRIGGERED) {
115 if (ch->flags & CHN_F_MAPPED)
116 sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
117 cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
118 vchan_mix_s16(dst, tmp, cnt >> 1);
128 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
129 {FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
132 static kobj_method_t feeder_vchan_s16_methods[] = {
133 KOBJMETHOD(feeder_feed, feed_vchan_s16),
136 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
138 /************************************************************/
141 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
144 struct pcm_channel *parent = devinfo;
146 KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
147 ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
153 ch->spd = DSP_DEFAULT_SPEED;
156 c->flags |= CHN_F_VIRTUAL;
162 vchan_free(kobj_t obj, void *data)
164 free(data, M_DEVBUF);
169 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
171 struct vchinfo *ch = data;
172 struct pcm_channel *parent = ch->parent;
173 struct pcm_channel *channel = ch->channel;
177 ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
178 if (ch->fmt & AFMT_16BIT)
180 else if (ch->fmt & AFMT_24BIT)
182 else if (ch->fmt & AFMT_32BIT)
185 chn_notify(parent, CHN_N_FORMAT);
187 sndbuf_setfmt(channel->bufsoft, format);
192 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
194 struct vchinfo *ch = data;
195 struct pcm_channel *parent = ch->parent;
196 struct pcm_channel *channel = ch->channel;
201 speed = sndbuf_getspd(parent->bufsoft);
208 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
210 struct vchinfo *ch = data;
211 struct pcm_channel *channel = ch->channel;
212 struct pcm_channel *parent = ch->parent;
213 /* struct pcm_channel *channel = ch->channel; */
216 ch->blksz = blocksize;
217 /* CHN_UNLOCK(channel); */
218 sndbuf_setblksz(channel->bufhard, blocksize);
219 chn_notify(parent, CHN_N_BLOCKSIZE);
221 /* CHN_LOCK(channel); */
223 crate = ch->spd * ch->bps;
224 prate = sndbuf_getspd(parent->bufsoft) * sndbuf_getbps(parent->bufsoft);
225 blocksize = sndbuf_getblksz(parent->bufsoft);
234 vchan_trigger(kobj_t obj, void *data, int go)
236 struct vchinfo *ch = data;
237 struct pcm_channel *parent = ch->parent;
238 struct pcm_channel *channel = ch->channel;
240 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
243 ch->run = (go == PCMTRIG_START)? 1 : 0;
245 chn_notify(parent, CHN_N_TRIGGER);
251 static struct pcmchan_caps *
252 vchan_getcaps(kobj_t obj, void *data)
254 struct vchinfo *ch = data;
256 ch->caps.minspeed = sndbuf_getspd(ch->parent->bufsoft);
257 ch->caps.maxspeed = ch->caps.minspeed;
258 ch->caps.fmtlist = vchan_fmt;
264 static kobj_method_t vchan_methods[] = {
265 KOBJMETHOD(channel_init, vchan_init),
266 KOBJMETHOD(channel_free, vchan_free),
267 KOBJMETHOD(channel_setformat, vchan_setformat),
268 KOBJMETHOD(channel_setspeed, vchan_setspeed),
269 KOBJMETHOD(channel_setblocksize, vchan_setblocksize),
270 KOBJMETHOD(channel_trigger, vchan_trigger),
271 KOBJMETHOD(channel_getcaps, vchan_getcaps),
274 CHANNEL_DECLARE(vchan);
278 * On the fly vchan rate settings
282 sysctl_hw_snd_vchanrate(SYSCTL_HANDLER_ARGS)
284 struct snddev_info *d;
285 struct snddev_channel *sce;
286 struct pcm_channel *c, *ch = NULL, *fake;
287 struct pcmchan_caps *caps;
292 if (!(d->flags & SD_F_AUTOVCHAN) || d->vchancount < 1)
294 if (pcm_inprog(d, 1) != 1 && req->newptr != NULL) {
298 SLIST_FOREACH(sce, &d->channels, link) {
301 if (c->direction == PCMDIR_PLAY) {
302 if (c->flags & CHN_F_VIRTUAL) {
304 if (ch != NULL && ch != c->parentchannel) {
309 if (req->newptr != NULL &&
310 (c->flags & CHN_F_BUSY)) {
315 } else if (c->flags & CHN_F_HAS_VCHAN) {
332 err = sysctl_handle_int(oidp, &newspd, sizeof(newspd), req);
333 if (err == 0 && req->newptr != NULL) {
334 if (newspd < 1 || newspd < feeder_rate_ratemin ||
335 newspd > feeder_rate_ratemax) {
340 caps = chn_getcaps(ch);
341 if (caps == NULL || newspd < caps->minspeed ||
342 newspd > caps->maxspeed) {
347 if (newspd != ch->speed) {
348 err = chn_setspeed(ch, newspd);
350 * Try to avoid FEEDER_RATE on parent channel if the
351 * requested value is not supported by the hardware.
353 if (!err && (ch->feederflags & (1 << FEEDER_RATE))) {
354 newspd = sndbuf_getspd(ch->bufhard);
355 err = chn_setspeed(ch, newspd);
359 fake = pcm_getfakechan(d);
362 fake->speed = newspd;
375 /* virtual channel interface */
378 vchan_create(struct pcm_channel *parent)
380 struct snddev_info *d = parent->parentsnddev;
381 struct pcmchan_children *pce;
382 struct pcm_channel *child, *fake;
383 struct pcmchan_caps *parent_caps;
384 int err, first, speed = 0;
386 if (!(parent->flags & CHN_F_BUSY))
392 pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
398 /* create a new playback channel */
399 child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
405 pce->channel = child;
407 /* add us to our grandparent's channel list */
409 * XXX maybe we shouldn't always add the dev_t
411 err = pcm_chn_add(d, child);
413 pcm_chn_destroy(child);
420 /* add us to our parent channel's children */
421 first = SLIST_EMPTY(&parent->children);
422 SLIST_INSERT_HEAD(&parent->children, pce, link);
423 parent->flags |= CHN_F_HAS_VCHAN;
426 parent_caps = chn_getcaps(parent);
427 if (parent_caps == NULL)
431 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
434 fake = pcm_getfakechan(d);
437 * Avoid querying kernel hint, use saved value
448 * This is very sad. Few soundcards advertised as being
449 * able to do (insanely) higher/lower speed, but in
450 * reality, they simply can't. At least, we give user chance
451 * to set sane value via kernel hints or sysctl.
456 r = resource_int_value(device_get_name(parent->dev),
457 device_get_unit(parent->dev),
458 "vchanrate", &speed);
462 * Workaround for sb16 running
463 * poorly at 45k / 49k.
465 switch (parent_caps->maxspeed) {
471 speed = VCHAN_DEFAULT_SPEED;
478 * Limit speed based on driver caps.
479 * This is supposed to help fixed rate, non-VRA
480 * AC97 cards, but.. (see below)
482 if (speed < parent_caps->minspeed)
483 speed = parent_caps->minspeed;
484 if (speed > parent_caps->maxspeed)
485 speed = parent_caps->maxspeed;
488 * We still need to limit the speed between
489 * feeder_rate_ratemin <-> feeder_rate_ratemax. This is
490 * just an escape goat if all of the above failed
493 if (speed < feeder_rate_ratemin)
494 speed = feeder_rate_ratemin;
495 if (speed > feeder_rate_ratemax)
496 speed = feeder_rate_ratemax;
498 err = chn_setspeed(parent, speed);
500 * Try to avoid FEEDER_RATE on parent channel if the
501 * requested value is not supported by the hardware.
503 if (!err && (parent->feederflags & (1 << FEEDER_RATE))) {
504 speed = sndbuf_getspd(parent->bufhard);
505 err = chn_setspeed(parent, speed);
508 if (!err && fake != NULL) {
510 * Save new value to fake channel.
521 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
522 parent->flags &= ~CHN_F_HAS_VCHAN;
525 if (pcm_chn_remove(d, child) == 0)
526 pcm_chn_destroy(child);
536 vchan_destroy(struct pcm_channel *c)
538 struct pcm_channel *parent = c->parentchannel;
539 struct snddev_info *d = parent->parentsnddev;
540 struct pcmchan_children *pce;
541 struct snddev_channel *sce;
546 if (!(parent->flags & CHN_F_BUSY)) {
550 if (SLIST_EMPTY(&parent->children)) {
555 /* remove us from our parent's children list */
556 SLIST_FOREACH(pce, &parent->children, link) {
557 if (pce->channel == c)
563 SLIST_FOREACH(sce, &d->channels, link) {
564 if (sce->channel == c) {
566 destroy_dev(sce->dsp_devt);
567 sce->dsp_devt = NULL;
569 if (sce->dspW_devt) {
570 destroy_dev(sce->dspW_devt);
571 sce->dspW_devt = NULL;
573 if (sce->audio_devt) {
574 destroy_dev(sce->audio_devt);
575 sce->audio_devt = NULL;
577 if (sce->dspr_devt) {
578 destroy_dev(sce->dspr_devt);
579 sce->dspr_devt = NULL;
585 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
588 if (SLIST_EMPTY(&parent->children)) {
589 parent->flags &= ~(CHN_F_BUSY | CHN_F_HAS_VCHAN);
591 if (chn_reset(parent, parent->format) == 0)
592 chn_setspeed(parent, spd);
595 /* remove us from our grandparent's channel list */
596 err = pcm_chn_remove(d, c);
599 /* destroy ourselves */
601 err = pcm_chn_destroy(c);
607 vchan_initsys(device_t dev)
610 struct snddev_info *d;
612 d = device_get_softc(dev);
613 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
614 OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
615 sysctl_hw_snd_vchans, "I", "");
617 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
618 OID_AUTO, "vchanrate", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
619 sysctl_hw_snd_vchanrate, "I", "");