2 * Copyright (c) 1999 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>
29 #include "feeder_if.h"
31 SND_DECLARE_FILE("$FreeBSD$");
34 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
38 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
39 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
47 sndbuf_destroy(struct snd_dbuf *b)
54 sndbuf_getbufaddr(struct snd_dbuf *buf)
56 return (buf->buf_addr);
60 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
62 struct snd_dbuf *b = (struct snd_dbuf *)arg;
65 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ",
66 (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len);
67 printf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr);
70 b->buf_addr = segs[0].ds_addr;
76 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
77 * the driver can call malloc(9) and sndbuf_setup() itself.
81 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags,
87 b->dmaflags = dmaflags | BUS_DMA_NOWAIT;
89 b->bufsize = b->maxsize;
91 b->flags |= SNDBUF_F_MANAGED;
92 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, b->dmaflags,
97 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
98 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
103 ret = sndbuf_resize(b, 2, b->maxsize / 2);
111 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
113 b->flags &= ~SNDBUF_F_MANAGED;
115 b->flags |= SNDBUF_F_MANAGED;
118 b->bufsize = b->maxsize;
119 return sndbuf_resize(b, 2, b->maxsize / 2);
123 sndbuf_free(struct snd_dbuf *b)
126 free(b->tmpbuf, M_DEVBUF);
129 free(b->shadbuf, M_DEVBUF);
132 if (b->flags & SNDBUF_F_MANAGED) {
134 bus_dmamap_unload(b->dmatag, b->dmamap);
136 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
138 free(b->buf, M_DEVBUF);
149 #define SNDBUF_CACHE_SHIFT 5
152 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
154 unsigned int bufsize, allocsize;
157 chn_lock(b->channel);
164 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz) > b->maxsize) {
165 chn_unlock(b->channel);
168 if (blkcnt == b->blkcnt && blksz == b->blksz)
171 bufsize = blkcnt * blksz;
173 if (bufsize > b->allocsize ||
174 bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
175 allocsize = round_page(bufsize);
176 chn_unlock(b->channel);
177 tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
178 chn_lock(b->channel);
180 printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n",
181 __func__, b, b->tmpbuf, tmpbuf,
182 b->allocsize, allocsize, bufsize);
183 if (b->tmpbuf != NULL)
184 free(b->tmpbuf, M_DEVBUF);
186 b->allocsize = allocsize;
187 } else if (snd_verbose > 3)
188 printf("%s(): b=%p %d [%d] NOCHANGE\n",
189 __func__, b, b->allocsize, b->bufsize);
193 b->bufsize = bufsize;
197 chn_unlock(b->channel);
202 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
204 unsigned int bufsize, allocsize;
205 u_int8_t *buf, *tmpbuf, *shadbuf;
207 if (blkcnt < 2 || blksz < 16)
210 bufsize = blksz * blkcnt;
212 if (bufsize > b->allocsize ||
213 bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
214 allocsize = round_page(bufsize);
215 chn_unlock(b->channel);
216 buf = malloc(allocsize, M_DEVBUF, M_WAITOK);
217 tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
218 shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
219 chn_lock(b->channel);
221 free(b->buf, M_DEVBUF);
223 if (b->tmpbuf != NULL)
224 free(b->tmpbuf, M_DEVBUF);
226 if (b->shadbuf != NULL)
227 free(b->shadbuf, M_DEVBUF);
228 b->shadbuf = shadbuf;
230 printf("%s(): b=%p %d -> %d [%d]\n",
231 __func__, b, b->allocsize, allocsize, bufsize);
232 b->allocsize = allocsize;
233 } else if (snd_verbose > 3)
234 printf("%s(): b=%p %d [%d] NOCHANGE\n",
235 __func__, b, b->allocsize, b->bufsize);
239 b->bufsize = bufsize;
240 b->maxsize = bufsize;
249 * @brief Zero out space in buffer free area
251 * This function clears a chunk of @c length bytes in the buffer free area
252 * (i.e., where the next write will be placed).
254 * @param b buffer context
255 * @param length number of bytes to blank
258 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
265 if (length > b->bufsize)
268 data = sndbuf_zerodata(b->fmt);
270 i = sndbuf_getfreeptr(b);
271 p = sndbuf_getbuf(b);
282 * @brief Zap buffer contents, resetting "ready area" fields
284 * @param b buffer context
287 sndbuf_fillsilence(struct snd_dbuf *b)
290 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
296 * @brief Reset buffer w/o flushing statistics
298 * This function just zeroes out buffer contents and sets the "ready length"
299 * to zero. This was originally to facilitate minimal playback interruption
300 * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls.
302 * @param b buffer context
305 sndbuf_softreset(struct snd_dbuf *b)
308 if (b->buf && b->bufsize > 0)
309 sndbuf_clear(b, b->bufsize);
313 sndbuf_reset(struct snd_dbuf *b)
322 if (b->buf && b->bufsize > 0)
323 sndbuf_clear(b, b->bufsize);
324 sndbuf_clearshadow(b);
328 sndbuf_getfmt(struct snd_dbuf *b)
334 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
338 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
339 if (b->fmt & AFMT_16BIT)
341 else if (b->fmt & AFMT_24BIT)
343 else if (b->fmt & AFMT_32BIT)
349 sndbuf_getspd(struct snd_dbuf *b)
355 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
361 sndbuf_getalign(struct snd_dbuf *b)
363 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
365 return align[b->bps - 1];
369 sndbuf_getblkcnt(struct snd_dbuf *b)
375 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
381 sndbuf_getblksz(struct snd_dbuf *b)
387 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
393 sndbuf_getbps(struct snd_dbuf *b)
399 sndbuf_getbuf(struct snd_dbuf *b)
405 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
407 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
413 sndbuf_getsize(struct snd_dbuf *b)
419 sndbuf_getmaxsize(struct snd_dbuf *b)
425 sndbuf_getallocsize(struct snd_dbuf *b)
431 sndbuf_runsz(struct snd_dbuf *b)
437 sndbuf_setrun(struct snd_dbuf *b, int go)
439 b->dl = go? b->blksz : 0;
443 sndbuf_getsel(struct snd_dbuf *b)
448 /************************************************************/
450 sndbuf_getxrun(struct snd_dbuf *b)
452 SNDBUF_LOCKASSERT(b);
458 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
460 SNDBUF_LOCKASSERT(b);
466 sndbuf_gethwptr(struct snd_dbuf *b)
468 SNDBUF_LOCKASSERT(b);
474 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
476 SNDBUF_LOCKASSERT(b);
482 sndbuf_getready(struct snd_dbuf *b)
484 SNDBUF_LOCKASSERT(b);
485 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
491 sndbuf_getreadyptr(struct snd_dbuf *b)
493 SNDBUF_LOCKASSERT(b);
494 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
500 sndbuf_getfree(struct snd_dbuf *b)
502 SNDBUF_LOCKASSERT(b);
503 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
505 return b->bufsize - b->rl;
509 sndbuf_getfreeptr(struct snd_dbuf *b)
511 SNDBUF_LOCKASSERT(b);
512 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
513 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
515 return (b->rp + b->rl) % b->bufsize;
519 sndbuf_getblocks(struct snd_dbuf *b)
521 SNDBUF_LOCKASSERT(b);
523 return b->total / b->blksz;
527 sndbuf_getprevblocks(struct snd_dbuf *b)
529 SNDBUF_LOCKASSERT(b);
531 return b->prev_total / b->blksz;
535 sndbuf_gettotal(struct snd_dbuf *b)
537 SNDBUF_LOCKASSERT(b);
543 sndbuf_updateprevtotal(struct snd_dbuf *b)
545 SNDBUF_LOCKASSERT(b);
547 b->prev_total = b->total;
551 snd_xbytes(unsigned int v, unsigned int from, unsigned int to)
553 unsigned int w, x, y;
558 if (from == 0 || to == 0 || v == 0)
571 return (unsigned int)(((u_int64_t)v * to) / from);
575 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
577 if (from == NULL || to == NULL || v == 0)
580 return snd_xbytes(v, sndbuf_getbps(from) * sndbuf_getspd(from),
581 sndbuf_getbps(to) * sndbuf_getspd(to));
585 sndbuf_zerodata(u_int32_t fmt)
587 if (fmt & AFMT_SIGNED)
589 else if (fmt & AFMT_MU_LAW)
591 else if (fmt & AFMT_A_LAW)
596 /************************************************************/
599 * @brief Acquire buffer space to extend ready area
601 * This function extends the ready area length by @c count bytes, and may
602 * optionally copy samples from another location stored in @c from. The
603 * counter @c snd_dbuf::total is also incremented by @c count bytes.
605 * @param b audio buffer
606 * @param from sample source (optional)
607 * @param count number of bytes to acquire
609 * @retval 0 Unconditional
612 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
616 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
617 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
621 l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
622 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
629 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
635 * @brief Dispose samples from channel buffer, increasing size of ready area
637 * This function discards samples from the supplied buffer by advancing the
638 * ready area start pointer and decrementing the ready area length. If
639 * @c to is not NULL, then the discard samples will be copied to the location
642 * @param b PCM channel sound buffer
643 * @param to destination buffer (optional)
644 * @param count number of bytes to discard
646 * @returns 0 unconditionally
649 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
653 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
654 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
657 l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
658 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
661 b->rp = (b->rp + l) % b->bufsize;
666 b->rp = (b->rp + count) % b->bufsize;
668 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
673 /* count is number of bytes we want added to destination buffer */
675 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
679 KASSERT(count > 0, ("can't feed 0 bytes"));
681 if (sndbuf_getfree(to) < count)
685 cnt = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
687 sndbuf_acquire(to, to->tmpbuf, cnt);
690 } while (count && cnt);
695 /************************************************************/
698 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
702 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
704 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
706 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
708 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
710 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
714 /************************************************************/
716 sndbuf_getflags(struct snd_dbuf *b)
722 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
730 * @brief Clear the shadow buffer by filling with samples equal to zero.
732 * @param b buffer to clear
735 sndbuf_clearshadow(struct snd_dbuf *b)
737 KASSERT(b != NULL, ("b is a null pointer"));
738 KASSERT(b->sl >= 0, ("illegal shadow length"));
740 if ((b->shadbuf != NULL) && (b->sl > 0))
741 memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
744 #ifdef OSSV4_EXPERIMENT
746 * @brief Return peak value from samples in buffer ready area.
748 * Peak ranges from 0-32767. If channel is monaural, most significant 16
749 * bits will be zero. For now, only expects to work with 1-2 channel
752 * @note Currently only operates with linear PCM formats.
754 * @param b buffer to analyze
755 * @param lpeak pointer to store left peak value
756 * @param rpeak pointer to store right peak value
759 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
761 u_int32_t lpeak, rpeak;
767 * @todo fill this in later