2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
4 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include <dev/sound/pcm/sound.h>
35 #include "feeder_if.h"
38 #include "snd_fxdiv_gen.h"
40 SND_DECLARE_FILE("$FreeBSD$");
43 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
47 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
48 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
56 sndbuf_destroy(struct snd_dbuf *b)
63 sndbuf_getbufaddr(struct snd_dbuf *buf)
65 return (buf->buf_addr);
69 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
71 struct snd_dbuf *b = (struct snd_dbuf *)arg;
73 if (snd_verbose > 3) {
74 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ",
75 (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len);
76 printf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr);
79 b->buf_addr = segs[0].ds_addr;
85 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
86 * the driver can call malloc(9) and sndbuf_setup() itself.
90 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags,
96 b->dmaflags = dmaflags | BUS_DMA_NOWAIT | BUS_DMA_COHERENT;
98 b->bufsize = b->maxsize;
100 b->flags |= SNDBUF_F_MANAGED;
101 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, b->dmaflags,
106 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
107 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
112 ret = sndbuf_resize(b, 2, b->maxsize / 2);
120 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
122 b->flags &= ~SNDBUF_F_MANAGED;
124 b->flags |= SNDBUF_F_MANAGED;
127 b->bufsize = b->maxsize;
128 return sndbuf_resize(b, 2, b->maxsize / 2);
132 sndbuf_free(struct snd_dbuf *b)
135 free(b->tmpbuf, M_DEVBUF);
138 free(b->shadbuf, M_DEVBUF);
141 if (b->flags & SNDBUF_F_MANAGED) {
143 bus_dmamap_unload(b->dmatag, b->dmamap);
145 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
147 free(b->buf, M_DEVBUF);
158 #define SNDBUF_CACHE_SHIFT 5
161 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
163 unsigned int bufsize, allocsize;
166 CHN_LOCK(b->channel);
173 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz) > b->maxsize) {
174 CHN_UNLOCK(b->channel);
177 if (blkcnt == b->blkcnt && blksz == b->blksz)
180 bufsize = blkcnt * blksz;
182 if (bufsize > b->allocsize ||
183 bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
184 allocsize = round_page(bufsize);
185 CHN_UNLOCK(b->channel);
186 tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
187 CHN_LOCK(b->channel);
189 printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n",
190 __func__, b, b->tmpbuf, tmpbuf,
191 b->allocsize, allocsize, bufsize);
192 if (b->tmpbuf != NULL)
193 free(b->tmpbuf, M_DEVBUF);
195 b->allocsize = allocsize;
196 } else if (snd_verbose > 3)
197 printf("%s(): b=%p %d [%d] NOCHANGE\n",
198 __func__, b, b->allocsize, b->bufsize);
202 b->bufsize = bufsize;
206 CHN_UNLOCK(b->channel);
211 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
213 unsigned int bufsize, allocsize;
214 u_int8_t *buf, *tmpbuf, *shadbuf;
216 if (blkcnt < 2 || blksz < 16)
219 bufsize = blksz * blkcnt;
221 if (bufsize > b->allocsize ||
222 bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
223 allocsize = round_page(bufsize);
224 CHN_UNLOCK(b->channel);
225 buf = malloc(allocsize, M_DEVBUF, M_WAITOK);
226 tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
227 shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
228 CHN_LOCK(b->channel);
230 free(b->buf, M_DEVBUF);
232 if (b->tmpbuf != NULL)
233 free(b->tmpbuf, M_DEVBUF);
235 if (b->shadbuf != NULL)
236 free(b->shadbuf, M_DEVBUF);
237 b->shadbuf = shadbuf;
239 printf("%s(): b=%p %d -> %d [%d]\n",
240 __func__, b, b->allocsize, allocsize, bufsize);
241 b->allocsize = allocsize;
242 } else if (snd_verbose > 3)
243 printf("%s(): b=%p %d [%d] NOCHANGE\n",
244 __func__, b, b->allocsize, b->bufsize);
248 b->bufsize = bufsize;
249 b->maxsize = bufsize;
258 * @brief Zero out space in buffer free area
260 * This function clears a chunk of @c length bytes in the buffer free area
261 * (i.e., where the next write will be placed).
263 * @param b buffer context
264 * @param length number of bytes to blank
267 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
274 if (length > b->bufsize)
277 data = sndbuf_zerodata(b->fmt);
279 i = sndbuf_getfreeptr(b);
280 p = sndbuf_getbuf(b);
291 * @brief Zap buffer contents, resetting "ready area" fields
293 * @param b buffer context
296 sndbuf_fillsilence(struct snd_dbuf *b)
299 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
305 sndbuf_fillsilence_rl(struct snd_dbuf *b, u_int rl)
308 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
310 b->rl = min(b->bufsize, rl);
314 * @brief Reset buffer w/o flushing statistics
316 * This function just zeroes out buffer contents and sets the "ready length"
317 * to zero. This was originally to facilitate minimal playback interruption
318 * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls.
320 * @param b buffer context
323 sndbuf_softreset(struct snd_dbuf *b)
326 if (b->buf && b->bufsize > 0)
327 sndbuf_clear(b, b->bufsize);
331 sndbuf_reset(struct snd_dbuf *b)
340 if (b->buf && b->bufsize > 0)
341 sndbuf_clear(b, b->bufsize);
342 sndbuf_clearshadow(b);
346 sndbuf_getfmt(struct snd_dbuf *b)
352 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
355 b->bps = AFMT_BPS(b->fmt);
356 b->align = AFMT_ALIGN(b->fmt);
358 b->bps = AFMT_CHANNEL(b->fmt);
359 if (b->fmt & AFMT_16BIT)
361 else if (b->fmt & AFMT_24BIT)
363 else if (b->fmt & AFMT_32BIT)
370 sndbuf_getspd(struct snd_dbuf *b)
376 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
382 sndbuf_getalign(struct snd_dbuf *b)
388 sndbuf_getblkcnt(struct snd_dbuf *b)
394 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
400 sndbuf_getblksz(struct snd_dbuf *b)
406 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
412 sndbuf_getbps(struct snd_dbuf *b)
418 sndbuf_getbuf(struct snd_dbuf *b)
424 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
426 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
432 sndbuf_getsize(struct snd_dbuf *b)
438 sndbuf_getmaxsize(struct snd_dbuf *b)
444 sndbuf_getallocsize(struct snd_dbuf *b)
450 sndbuf_runsz(struct snd_dbuf *b)
456 sndbuf_setrun(struct snd_dbuf *b, int go)
458 b->dl = go? b->blksz : 0;
462 sndbuf_getsel(struct snd_dbuf *b)
467 /************************************************************/
469 sndbuf_getxrun(struct snd_dbuf *b)
471 SNDBUF_LOCKASSERT(b);
477 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
479 SNDBUF_LOCKASSERT(b);
485 sndbuf_gethwptr(struct snd_dbuf *b)
487 SNDBUF_LOCKASSERT(b);
493 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
495 SNDBUF_LOCKASSERT(b);
501 sndbuf_getready(struct snd_dbuf *b)
503 SNDBUF_LOCKASSERT(b);
504 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
510 sndbuf_getreadyptr(struct snd_dbuf *b)
512 SNDBUF_LOCKASSERT(b);
513 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
519 sndbuf_getfree(struct snd_dbuf *b)
521 SNDBUF_LOCKASSERT(b);
522 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
524 return b->bufsize - b->rl;
528 sndbuf_getfreeptr(struct snd_dbuf *b)
530 SNDBUF_LOCKASSERT(b);
531 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
532 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
534 return (b->rp + b->rl) % b->bufsize;
538 sndbuf_getblocks(struct snd_dbuf *b)
540 SNDBUF_LOCKASSERT(b);
542 return b->total / b->blksz;
546 sndbuf_getprevblocks(struct snd_dbuf *b)
548 SNDBUF_LOCKASSERT(b);
550 return b->prev_total / b->blksz;
554 sndbuf_gettotal(struct snd_dbuf *b)
556 SNDBUF_LOCKASSERT(b);
562 sndbuf_getprevtotal(struct snd_dbuf *b)
564 SNDBUF_LOCKASSERT(b);
566 return b->prev_total;
570 sndbuf_updateprevtotal(struct snd_dbuf *b)
572 SNDBUF_LOCKASSERT(b);
574 b->prev_total = b->total;
578 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
580 if (from == NULL || to == NULL || v == 0)
583 return snd_xbytes(v, sndbuf_getalign(from) * sndbuf_getspd(from),
584 sndbuf_getalign(to) * sndbuf_getspd(to));
588 sndbuf_zerodata(u_int32_t fmt)
590 if (fmt & (AFMT_SIGNED | AFMT_PASSTHROUGH))
592 else if (fmt & AFMT_MU_LAW)
594 else if (fmt & AFMT_A_LAW)
599 /************************************************************/
602 * @brief Acquire buffer space to extend ready area
604 * This function extends the ready area length by @c count bytes, and may
605 * optionally copy samples from another location stored in @c from. The
606 * counter @c snd_dbuf::total is also incremented by @c count bytes.
608 * @param b audio buffer
609 * @param from sample source (optional)
610 * @param count number of bytes to acquire
612 * @retval 0 Unconditional
615 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
619 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
620 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
624 l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
625 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
632 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
638 * @brief Dispose samples from channel buffer, increasing size of ready area
640 * This function discards samples from the supplied buffer by advancing the
641 * ready area start pointer and decrementing the ready area length. If
642 * @c to is not NULL, then the discard samples will be copied to the location
645 * @param b PCM channel sound buffer
646 * @param to destination buffer (optional)
647 * @param count number of bytes to discard
649 * @returns 0 unconditionally
652 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
656 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
657 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
660 l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
661 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
664 b->rp = (b->rp + l) % b->bufsize;
669 b->rp = (b->rp + count) % b->bufsize;
671 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
676 #ifdef SND_DIAGNOSTIC
677 static uint32_t snd_feeder_maxfeed = 0;
678 SYSCTL_UINT(_hw_snd, OID_AUTO, feeder_maxfeed, CTLFLAG_RD,
679 &snd_feeder_maxfeed, 0, "maximum feeder count request");
681 static uint32_t snd_feeder_maxcycle = 0;
682 SYSCTL_UINT(_hw_snd, OID_AUTO, feeder_maxcycle, CTLFLAG_RD,
683 &snd_feeder_maxcycle, 0, "maximum feeder cycle");
686 /* count is number of bytes we want added to destination buffer */
688 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
690 unsigned int cnt, maxfeed;
691 #ifdef SND_DIAGNOSTIC
694 if (count > snd_feeder_maxfeed)
695 snd_feeder_maxfeed = count;
700 KASSERT(count > 0, ("can't feed 0 bytes"));
702 if (sndbuf_getfree(to) < count)
705 maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(to));
708 cnt = FEEDER_FEED(feeder, channel, to->tmpbuf,
709 min(count, maxfeed), from);
712 sndbuf_acquire(to, to->tmpbuf, cnt);
714 #ifdef SND_DIAGNOSTIC
717 } while (count != 0);
719 #ifdef SND_DIAGNOSTIC
720 if (cycle > snd_feeder_maxcycle)
721 snd_feeder_maxcycle = cycle;
727 /************************************************************/
730 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
734 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
736 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
738 printf(" total: %ju, prev_total: %ju, xrun: %d", (uintmax_t)b->total, (uintmax_t)b->prev_total, b->xrun);
740 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
742 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
746 /************************************************************/
748 sndbuf_getflags(struct snd_dbuf *b)
754 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
762 * @brief Clear the shadow buffer by filling with samples equal to zero.
764 * @param b buffer to clear
767 sndbuf_clearshadow(struct snd_dbuf *b)
769 KASSERT(b != NULL, ("b is a null pointer"));
770 KASSERT(b->sl >= 0, ("illegal shadow length"));
772 if ((b->shadbuf != NULL) && (b->sl > 0))
773 memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
776 #ifdef OSSV4_EXPERIMENT
778 * @brief Return peak value from samples in buffer ready area.
780 * Peak ranges from 0-32767. If channel is monaural, most significant 16
781 * bits will be zero. For now, only expects to work with 1-2 channel
784 * @note Currently only operates with linear PCM formats.
786 * @param b buffer to analyze
787 * @param lpeak pointer to store left peak value
788 * @param rpeak pointer to store right peak value
791 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
793 u_int32_t lpeak, rpeak;
799 * @todo fill this in later