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 #define SND_DECLARE_FXDIV
39 #include "snd_fxdiv_gen.h"
41 SND_DECLARE_FILE("$FreeBSD$");
44 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
48 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
49 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
57 sndbuf_destroy(struct snd_dbuf *b)
64 sndbuf_getbufaddr(struct snd_dbuf *buf)
66 return (buf->buf_addr);
70 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
72 struct snd_dbuf *b = (struct snd_dbuf *)arg;
75 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ",
76 (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len);
77 printf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr);
80 b->buf_addr = segs[0].ds_addr;
86 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
87 * the driver can call malloc(9) and sndbuf_setup() itself.
91 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags,
97 b->dmaflags = dmaflags | BUS_DMA_NOWAIT | BUS_DMA_COHERENT;
99 b->bufsize = b->maxsize;
101 b->flags |= SNDBUF_F_MANAGED;
102 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, b->dmaflags,
107 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
108 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
113 ret = sndbuf_resize(b, 2, b->maxsize / 2);
121 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
123 b->flags &= ~SNDBUF_F_MANAGED;
125 b->flags |= SNDBUF_F_MANAGED;
128 b->bufsize = b->maxsize;
129 return sndbuf_resize(b, 2, b->maxsize / 2);
133 sndbuf_free(struct snd_dbuf *b)
136 free(b->tmpbuf, M_DEVBUF);
139 free(b->shadbuf, M_DEVBUF);
142 if (b->flags & SNDBUF_F_MANAGED) {
144 bus_dmamap_unload(b->dmatag, b->dmamap);
146 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
148 free(b->buf, M_DEVBUF);
159 #define SNDBUF_CACHE_SHIFT 5
162 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
164 unsigned int bufsize, allocsize;
167 CHN_LOCK(b->channel);
174 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz) > b->maxsize) {
175 CHN_UNLOCK(b->channel);
178 if (blkcnt == b->blkcnt && blksz == b->blksz)
181 bufsize = blkcnt * blksz;
183 if (bufsize > b->allocsize ||
184 bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
185 allocsize = round_page(bufsize);
186 CHN_UNLOCK(b->channel);
187 tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
188 CHN_LOCK(b->channel);
190 printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n",
191 __func__, b, b->tmpbuf, tmpbuf,
192 b->allocsize, allocsize, bufsize);
193 if (b->tmpbuf != NULL)
194 free(b->tmpbuf, M_DEVBUF);
196 b->allocsize = allocsize;
197 } else if (snd_verbose > 3)
198 printf("%s(): b=%p %d [%d] NOCHANGE\n",
199 __func__, b, b->allocsize, b->bufsize);
203 b->bufsize = bufsize;
207 CHN_UNLOCK(b->channel);
212 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
214 unsigned int bufsize, allocsize;
215 u_int8_t *buf, *tmpbuf, *shadbuf;
217 if (blkcnt < 2 || blksz < 16)
220 bufsize = blksz * blkcnt;
222 if (bufsize > b->allocsize ||
223 bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
224 allocsize = round_page(bufsize);
225 CHN_UNLOCK(b->channel);
226 buf = malloc(allocsize, M_DEVBUF, M_WAITOK);
227 tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
228 shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
229 CHN_LOCK(b->channel);
231 free(b->buf, M_DEVBUF);
233 if (b->tmpbuf != NULL)
234 free(b->tmpbuf, M_DEVBUF);
236 if (b->shadbuf != NULL)
237 free(b->shadbuf, M_DEVBUF);
238 b->shadbuf = shadbuf;
240 printf("%s(): b=%p %d -> %d [%d]\n",
241 __func__, b, b->allocsize, allocsize, bufsize);
242 b->allocsize = allocsize;
243 } else if (snd_verbose > 3)
244 printf("%s(): b=%p %d [%d] NOCHANGE\n",
245 __func__, b, b->allocsize, b->bufsize);
249 b->bufsize = bufsize;
250 b->maxsize = bufsize;
259 * @brief Zero out space in buffer free area
261 * This function clears a chunk of @c length bytes in the buffer free area
262 * (i.e., where the next write will be placed).
264 * @param b buffer context
265 * @param length number of bytes to blank
268 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
275 if (length > b->bufsize)
278 data = sndbuf_zerodata(b->fmt);
280 i = sndbuf_getfreeptr(b);
281 p = sndbuf_getbuf(b);
292 * @brief Zap buffer contents, resetting "ready area" fields
294 * @param b buffer context
297 sndbuf_fillsilence(struct snd_dbuf *b)
300 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
306 sndbuf_fillsilence_rl(struct snd_dbuf *b, u_int rl)
309 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
311 b->rl = min(b->bufsize, rl);
315 * @brief Reset buffer w/o flushing statistics
317 * This function just zeroes out buffer contents and sets the "ready length"
318 * to zero. This was originally to facilitate minimal playback interruption
319 * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls.
321 * @param b buffer context
324 sndbuf_softreset(struct snd_dbuf *b)
327 if (b->buf && b->bufsize > 0)
328 sndbuf_clear(b, b->bufsize);
332 sndbuf_reset(struct snd_dbuf *b)
341 if (b->buf && b->bufsize > 0)
342 sndbuf_clear(b, b->bufsize);
343 sndbuf_clearshadow(b);
347 sndbuf_getfmt(struct snd_dbuf *b)
353 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
356 b->bps = AFMT_BPS(b->fmt);
357 b->align = AFMT_ALIGN(b->fmt);
359 b->bps = AFMT_CHANNEL(b->fmt);
360 if (b->fmt & AFMT_16BIT)
362 else if (b->fmt & AFMT_24BIT)
364 else if (b->fmt & AFMT_32BIT)
371 sndbuf_getspd(struct snd_dbuf *b)
377 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
383 sndbuf_getalign(struct snd_dbuf *b)
389 sndbuf_getblkcnt(struct snd_dbuf *b)
395 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
401 sndbuf_getblksz(struct snd_dbuf *b)
407 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
413 sndbuf_getbps(struct snd_dbuf *b)
419 sndbuf_getbuf(struct snd_dbuf *b)
425 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
427 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
433 sndbuf_getsize(struct snd_dbuf *b)
439 sndbuf_getmaxsize(struct snd_dbuf *b)
445 sndbuf_getallocsize(struct snd_dbuf *b)
451 sndbuf_runsz(struct snd_dbuf *b)
457 sndbuf_setrun(struct snd_dbuf *b, int go)
459 b->dl = go? b->blksz : 0;
463 sndbuf_getsel(struct snd_dbuf *b)
468 /************************************************************/
470 sndbuf_getxrun(struct snd_dbuf *b)
472 SNDBUF_LOCKASSERT(b);
478 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
480 SNDBUF_LOCKASSERT(b);
486 sndbuf_gethwptr(struct snd_dbuf *b)
488 SNDBUF_LOCKASSERT(b);
494 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
496 SNDBUF_LOCKASSERT(b);
502 sndbuf_getready(struct snd_dbuf *b)
504 SNDBUF_LOCKASSERT(b);
505 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
511 sndbuf_getreadyptr(struct snd_dbuf *b)
513 SNDBUF_LOCKASSERT(b);
514 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
520 sndbuf_getfree(struct snd_dbuf *b)
522 SNDBUF_LOCKASSERT(b);
523 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
525 return b->bufsize - b->rl;
529 sndbuf_getfreeptr(struct snd_dbuf *b)
531 SNDBUF_LOCKASSERT(b);
532 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
533 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
535 return (b->rp + b->rl) % b->bufsize;
539 sndbuf_getblocks(struct snd_dbuf *b)
541 SNDBUF_LOCKASSERT(b);
543 return b->total / b->blksz;
547 sndbuf_getprevblocks(struct snd_dbuf *b)
549 SNDBUF_LOCKASSERT(b);
551 return b->prev_total / b->blksz;
555 sndbuf_gettotal(struct snd_dbuf *b)
557 SNDBUF_LOCKASSERT(b);
563 sndbuf_getprevtotal(struct snd_dbuf *b)
565 SNDBUF_LOCKASSERT(b);
567 return b->prev_total;
571 sndbuf_updateprevtotal(struct snd_dbuf *b)
573 SNDBUF_LOCKASSERT(b);
575 b->prev_total = b->total;
579 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
581 if (from == NULL || to == NULL || v == 0)
584 return snd_xbytes(v, sndbuf_getalign(from) * sndbuf_getspd(from),
585 sndbuf_getalign(to) * sndbuf_getspd(to));
589 sndbuf_zerodata(u_int32_t fmt)
591 if (fmt & (AFMT_SIGNED | AFMT_PASSTHROUGH))
593 else if (fmt & AFMT_MU_LAW)
595 else if (fmt & AFMT_A_LAW)
600 /************************************************************/
603 * @brief Acquire buffer space to extend ready area
605 * This function extends the ready area length by @c count bytes, and may
606 * optionally copy samples from another location stored in @c from. The
607 * counter @c snd_dbuf::total is also incremented by @c count bytes.
609 * @param b audio buffer
610 * @param from sample source (optional)
611 * @param count number of bytes to acquire
613 * @retval 0 Unconditional
616 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
620 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
621 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
625 l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
626 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
633 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
639 * @brief Dispose samples from channel buffer, increasing size of ready area
641 * This function discards samples from the supplied buffer by advancing the
642 * ready area start pointer and decrementing the ready area length. If
643 * @c to is not NULL, then the discard samples will be copied to the location
646 * @param b PCM channel sound buffer
647 * @param to destination buffer (optional)
648 * @param count number of bytes to discard
650 * @returns 0 unconditionally
653 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
657 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
658 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
661 l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
662 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
665 b->rp = (b->rp + l) % b->bufsize;
670 b->rp = (b->rp + count) % b->bufsize;
672 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
677 #ifdef SND_DIAGNOSTIC
678 static uint32_t snd_feeder_maxfeed = 0;
679 SYSCTL_INT(_hw_snd, OID_AUTO, feeder_maxfeed, CTLFLAG_RD,
680 &snd_feeder_maxfeed, 0, "maximum feeder count request");
682 static uint32_t snd_feeder_maxcycle = 0;
683 SYSCTL_INT(_hw_snd, OID_AUTO, feeder_maxcycle, CTLFLAG_RD,
684 &snd_feeder_maxcycle, 0, "maximum feeder cycle");
687 /* count is number of bytes we want added to destination buffer */
689 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
691 unsigned int cnt, maxfeed;
692 #ifdef SND_DIAGNOSTIC
695 if (count > snd_feeder_maxfeed)
696 snd_feeder_maxfeed = count;
701 KASSERT(count > 0, ("can't feed 0 bytes"));
703 if (sndbuf_getfree(to) < count)
706 maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(to));
709 cnt = FEEDER_FEED(feeder, channel, to->tmpbuf,
710 min(count, maxfeed), from);
713 sndbuf_acquire(to, to->tmpbuf, cnt);
715 #ifdef SND_DIAGNOSTIC
718 } while (count != 0);
720 #ifdef SND_DIAGNOSTIC
721 if (cycle > snd_feeder_maxcycle)
722 snd_feeder_maxcycle = cycle;
728 /************************************************************/
731 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
735 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
737 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
739 printf(" total: %ju, prev_total: %ju, xrun: %d", (uintmax_t)b->total, (uintmax_t)b->prev_total, b->xrun);
741 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
743 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
747 /************************************************************/
749 sndbuf_getflags(struct snd_dbuf *b)
755 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
763 * @brief Clear the shadow buffer by filling with samples equal to zero.
765 * @param b buffer to clear
768 sndbuf_clearshadow(struct snd_dbuf *b)
770 KASSERT(b != NULL, ("b is a null pointer"));
771 KASSERT(b->sl >= 0, ("illegal shadow length"));
773 if ((b->shadbuf != NULL) && (b->sl > 0))
774 memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
777 #ifdef OSSV4_EXPERIMENT
779 * @brief Return peak value from samples in buffer ready area.
781 * Peak ranges from 0-32767. If channel is monaural, most significant 16
782 * bits will be zero. For now, only expects to work with 1-2 channel
785 * @note Currently only operates with linear PCM formats.
787 * @param b buffer to analyze
788 * @param lpeak pointer to store left peak value
789 * @param rpeak pointer to store right peak value
792 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
794 u_int32_t lpeak, rpeak;
800 * @todo fill this in later