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 vchan_mix_s16(int16_t *to
, int16_t *tmp
, unsigned int count
, int volume
)
59 * to is the output buffer, tmp is the input buffer
60 * count is the number of 16bit samples to mix
61 * volume is in range 0-100
68 scale
= (volume
<< 16) / 100;
69 doscale
= volume
!= 100;
71 for(i
= 0; i
< count
; i
++) {
74 x
+= ((int)tmp
[i
] * scale
) >> 16;
78 /* kprintf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
82 /* kprintf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
85 to
[i
] = x
& 0x0000ffff;
91 feed_vchan_s16(struct pcm_feeder
*f
, struct pcm_channel
*c
, u_int8_t
*b
, u_int32_t count
, void *source
)
93 /* we're going to abuse things a bit */
94 struct snd_dbuf
*src
= source
;
95 struct pcmchan_children
*cce
;
96 struct pcm_channel
*ch
;
99 unsigned int cnt
, rcnt
= 0;
103 if (sndbuf_getsize(src
) < count
)
104 panic("feed_vchan_s16(%s): tmp buffer size %d < count %d, flags = 0x%x",
105 c
->name
, sndbuf_getsize(src
), count
, c
->flags
);
107 sz
= sndbuf_getsize(src
);
116 * we are going to use our source as a temporary buffer since it's
117 * got no other purpose. we obtain our data by traversing the channel
118 * list of children and calling vchan_mix_* to mix count bytes from each
119 * into our destination buffer, b
122 tmp
= (int16_t *)sndbuf_getbuf(src
);
124 SLIST_FOREACH(cce
, &c
->children
, link
) {
127 if (ch
->flags
& CHN_F_TRIGGERED
) {
128 if (ch
->flags
& CHN_F_MAPPED
)
129 sndbuf_acquire(ch
->bufsoft
, NULL
, sndbuf_getfree(ch
->bufsoft
));
130 cnt
= FEEDER_FEED(ch
->feeder
, ch
, (u_int8_t
*)tmp
, count
, ch
->bufsoft
);
131 volume
= ch
->volume
& 0xff; /* XXX do special stereo processing? */
132 volume
+= (ch
->volume
>> 8) & 0xff;
134 vchan_mix_s16(dst
, tmp
, cnt
>> 1, volume
);
144 static struct pcm_feederdesc feeder_vchan_s16_desc
[] = {
145 {FEEDER_MIXER
, AFMT_S16_LE
| AFMT_STEREO
, AFMT_S16_LE
| AFMT_STEREO
, 0},
148 static kobj_method_t feeder_vchan_s16_methods
[] = {
149 KOBJMETHOD(feeder_feed
, feed_vchan_s16
),
152 FEEDER_DECLARE(feeder_vchan_s16
, 2, NULL
);
154 /************************************************************/
157 vchan_init(kobj_t obj
, void *devinfo
, struct snd_dbuf
*b
, struct pcm_channel
*c
, int dir
)
160 struct pcm_channel
*parent
= devinfo
;
162 KASSERT(dir
== PCMDIR_PLAY
, ("vchan_init: bad direction"));
163 ch
= kmalloc(sizeof(*ch
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
167 ch
->spd
= DSP_DEFAULT_SPEED
;
170 c
->flags
|= CHN_F_VIRTUAL
;
176 vchan_free(kobj_t obj
, void *data
)
178 kfree(data
, M_DEVBUF
);
183 vchan_setformat(kobj_t obj
, void *data
, u_int32_t format
)
185 struct vchinfo
*ch
= data
;
186 struct pcm_channel
*parent
= ch
->parent
;
187 struct pcm_channel
*channel
= ch
->channel
;
191 ch
->bps
<<= (ch
->fmt
& AFMT_STEREO
)? 1 : 0;
192 if (ch
->fmt
& AFMT_16BIT
)
194 else if (ch
->fmt
& AFMT_24BIT
)
196 else if (ch
->fmt
& AFMT_32BIT
)
199 chn_notify(parent
, CHN_N_FORMAT
);
201 sndbuf_setfmt(channel
->bufsoft
, format
);
206 vchan_setspeed(kobj_t obj
, void *data
, u_int32_t speed
)
208 struct vchinfo
*ch
= data
;
209 struct pcm_channel
*parent
= ch
->parent
;
210 struct pcm_channel
*channel
= ch
->channel
;
215 speed
= sndbuf_getspd(parent
->bufsoft
);
222 vchan_setblocksize(kobj_t obj
, void *data
, u_int32_t blocksize
)
224 struct vchinfo
*ch
= data
;
225 struct pcm_channel
*channel
= ch
->channel
;
226 struct pcm_channel
*parent
= ch
->parent
;
227 /* struct pcm_channel *channel = ch->channel; */
230 ch
->blksz
= blocksize
;
231 /* CHN_UNLOCK(channel); */
232 sndbuf_setblksz(channel
->bufhard
, blocksize
);
233 chn_notify(parent
, CHN_N_BLOCKSIZE
);
235 /* CHN_LOCK(channel); */
237 crate
= ch
->spd
* ch
->bps
;
238 prate
= sndbuf_getspd(parent
->bufsoft
) * sndbuf_getbps(parent
->bufsoft
);
239 blocksize
= sndbuf_getblksz(parent
->bufsoft
);
248 vchan_trigger(kobj_t obj
, void *data
, int go
)
250 struct vchinfo
*ch
= data
;
251 struct pcm_channel
*parent
= ch
->parent
;
252 struct pcm_channel
*channel
= ch
->channel
;
254 if (go
== PCMTRIG_EMLDMAWR
|| go
== PCMTRIG_EMLDMARD
)
257 ch
->run
= (go
== PCMTRIG_START
)? 1 : 0;
259 chn_notify(parent
, CHN_N_TRIGGER
);
265 static struct pcmchan_caps
*
266 vchan_getcaps(kobj_t obj
, void *data
)
268 struct vchinfo
*ch
= data
;
270 ch
->caps
.minspeed
= sndbuf_getspd(ch
->parent
->bufsoft
);
271 ch
->caps
.maxspeed
= ch
->caps
.minspeed
;
272 ch
->caps
.fmtlist
= vchan_fmt
;
278 static kobj_method_t vchan_methods
[] = {
279 KOBJMETHOD(channel_init
, vchan_init
),
280 KOBJMETHOD(channel_free
, vchan_free
),
281 KOBJMETHOD(channel_setformat
, vchan_setformat
),
282 KOBJMETHOD(channel_setspeed
, vchan_setspeed
),
283 KOBJMETHOD(channel_setblocksize
, vchan_setblocksize
),
284 KOBJMETHOD(channel_trigger
, vchan_trigger
),
285 KOBJMETHOD(channel_getcaps
, vchan_getcaps
),
288 CHANNEL_DECLARE(vchan
);
292 * On the fly vchan rate settings
296 sysctl_hw_snd_vchanrate(SYSCTL_HANDLER_ARGS
)
298 struct snddev_info
*d
;
299 struct snddev_channel
*sce
;
300 struct pcm_channel
*c
, *ch
= NULL
, *fake
;
301 struct pcmchan_caps
*caps
;
306 if (!(d
->flags
& SD_F_AUTOVCHAN
) || d
->vchancount
< 1)
308 if (pcm_inprog(d
, 1) != 1 && req
->newptr
!= NULL
) {
312 SLIST_FOREACH(sce
, &d
->channels
, link
) {
315 if (c
->direction
== PCMDIR_PLAY
) {
316 if (c
->flags
& CHN_F_VIRTUAL
) {
318 if (ch
!= NULL
&& ch
!= c
->parentchannel
) {
323 if (req
->newptr
!= NULL
&&
324 (c
->flags
& CHN_F_BUSY
)) {
329 } else if (c
->flags
& CHN_F_HAS_VCHAN
) {
346 err
= sysctl_handle_int(oidp
, &newspd
, sizeof(newspd
), req
);
347 if (err
== 0 && req
->newptr
!= NULL
) {
348 if (newspd
< 1 || newspd
< feeder_rate_ratemin
||
349 newspd
> feeder_rate_ratemax
) {
354 caps
= chn_getcaps(ch
);
355 if (caps
== NULL
|| newspd
< caps
->minspeed
||
356 newspd
> caps
->maxspeed
) {
361 if (newspd
!= ch
->speed
) {
362 err
= chn_setspeed(ch
, newspd
);
364 * Try to avoid FEEDER_RATE on parent channel if the
365 * requested value is not supported by the hardware.
367 if (!err
&& (ch
->feederflags
& (1 << FEEDER_RATE
))) {
368 newspd
= sndbuf_getspd(ch
->bufhard
);
369 err
= chn_setspeed(ch
, newspd
);
373 fake
= pcm_getfakechan(d
);
376 fake
->speed
= newspd
;
389 /* virtual channel interface */
392 vchan_create(struct pcm_channel
*parent
)
394 struct snddev_info
*d
= parent
->parentsnddev
;
395 struct pcmchan_children
*pce
;
396 struct pcm_channel
*child
, *fake
;
397 struct pcmchan_caps
*parent_caps
;
398 int err
, first
, speed
= 0;
400 if (!(parent
->flags
& CHN_F_BUSY
))
406 pce
= kmalloc(sizeof(*pce
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
408 /* create a new playback channel */
409 child
= pcm_chn_create(d
, parent
, &vchan_class
, PCMDIR_VIRTUAL
, parent
);
411 kfree(pce
, M_DEVBUF
);
415 pce
->channel
= child
;
417 /* add us to our grandparent's channel list */
419 * XXX maybe we shouldn't always add the dev_t
421 err
= pcm_chn_add(d
, child
);
423 pcm_chn_destroy(child
);
424 kfree(pce
, M_DEVBUF
);
430 /* add us to our parent channel's children */
431 first
= SLIST_EMPTY(&parent
->children
);
432 SLIST_INSERT_HEAD(&parent
->children
, pce
, link
);
433 parent
->flags
|= CHN_F_HAS_VCHAN
;
436 parent_caps
= chn_getcaps(parent
);
437 if (parent_caps
== NULL
)
441 err
= chn_reset(parent
, AFMT_STEREO
| AFMT_S16_LE
);
444 fake
= pcm_getfakechan(d
);
447 * Avoid querying kernel hint, use saved value
458 * This is very sad. Few soundcards advertised as being
459 * able to do (insanely) higher/lower speed, but in
460 * reality, they simply can't. At least, we give user chance
461 * to set sane value via kernel hints or sysctl.
466 r
= resource_int_value(device_get_name(parent
->dev
),
467 device_get_unit(parent
->dev
),
468 "vchanrate", &speed
);
472 * Workaround for sb16 running
473 * poorly at 45k / 49k.
475 switch (parent_caps
->maxspeed
) {
481 speed
= VCHAN_DEFAULT_SPEED
;
488 * Limit speed based on driver caps.
489 * This is supposed to help fixed rate, non-VRA
490 * AC97 cards, but.. (see below)
492 if (speed
< parent_caps
->minspeed
)
493 speed
= parent_caps
->minspeed
;
494 if (speed
> parent_caps
->maxspeed
)
495 speed
= parent_caps
->maxspeed
;
498 * We still need to limit the speed between
499 * feeder_rate_ratemin <-> feeder_rate_ratemax. This is
500 * just an escape goat if all of the above failed
503 if (speed
< feeder_rate_ratemin
)
504 speed
= feeder_rate_ratemin
;
505 if (speed
> feeder_rate_ratemax
)
506 speed
= feeder_rate_ratemax
;
508 err
= chn_setspeed(parent
, speed
);
510 * Try to avoid FEEDER_RATE on parent channel if the
511 * requested value is not supported by the hardware.
513 if (!err
&& (parent
->feederflags
& (1 << FEEDER_RATE
))) {
514 speed
= sndbuf_getspd(parent
->bufhard
);
515 err
= chn_setspeed(parent
, speed
);
518 if (!err
&& fake
!= NULL
) {
520 * Save new value to fake channel.
531 SLIST_REMOVE(&parent
->children
, pce
, pcmchan_children
, link
);
532 parent
->flags
&= ~CHN_F_HAS_VCHAN
;
534 kfree(pce
, M_DEVBUF
);
535 if (pcm_chn_remove(d
, child
) == 0)
536 pcm_chn_destroy(child
);
546 vchan_destroy(struct pcm_channel
*c
)
548 struct pcm_channel
*parent
= c
->parentchannel
;
549 struct snddev_info
*d
= parent
->parentsnddev
;
550 struct pcmchan_children
*pce
;
551 struct snddev_channel
*sce
;
556 if (!(parent
->flags
& CHN_F_BUSY
)) {
560 if (SLIST_EMPTY(&parent
->children
)) {
565 /* remove us from our parent's children list */
566 SLIST_FOREACH(pce
, &parent
->children
, link
) {
567 if (pce
->channel
== c
)
573 SLIST_FOREACH(sce
, &d
->channels
, link
) {
574 if (sce
->channel
== c
) {
576 destroy_dev(sce
->dsp_devt
);
577 sce
->dsp_devt
= NULL
;
579 if (sce
->dspW_devt
) {
580 destroy_dev(sce
->dspW_devt
);
581 sce
->dspW_devt
= NULL
;
583 if (sce
->audio_devt
) {
584 destroy_dev(sce
->audio_devt
);
585 sce
->audio_devt
= NULL
;
587 if (sce
->dspr_devt
) {
588 destroy_dev(sce
->dspr_devt
);
589 sce
->dspr_devt
= NULL
;
595 SLIST_REMOVE(&parent
->children
, pce
, pcmchan_children
, link
);
596 kfree(pce
, M_DEVBUF
);
598 if (SLIST_EMPTY(&parent
->children
)) {
599 parent
->flags
&= ~(CHN_F_BUSY
| CHN_F_HAS_VCHAN
);
601 if (chn_reset(parent
, parent
->format
) == 0)
602 chn_setspeed(parent
, spd
);
605 /* remove us from our grandparent's channel list */
606 err
= pcm_chn_remove(d
, c
);
609 /* destroy ourselves */
611 err
= pcm_chn_destroy(c
);
617 vchan_initsys(device_t dev
)
620 struct snddev_info
*d
;
622 d
= device_get_softc(dev
);
623 SYSCTL_ADD_PROC(snd_sysctl_tree(dev
), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev
)),
624 OID_AUTO
, "vchans", CTLTYPE_INT
| CTLFLAG_RW
, d
, sizeof(d
),
625 sysctl_hw_snd_vchans
, "I", "");
627 SYSCTL_ADD_PROC(snd_sysctl_tree(dev
), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev
)),
628 OID_AUTO
, "vchanrate", CTLTYPE_INT
| CTLFLAG_RW
, d
, sizeof(d
),
629 sysctl_hw_snd_vchanrate
, "I", "");