xref: /freebsd/sys/dev/sound/pcm/buffer.c (revision 30d239bc4c510432e65a84fa1c14ed67a3ab1c92)
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <dev/sound/pcm/sound.h>
28 
29 #include "feeder_if.h"
30 
31 SND_DECLARE_FILE("$FreeBSD$");
32 
33 struct snd_dbuf *
34 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
35 {
36 	struct snd_dbuf *b;
37 
38 	b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
39 	snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
40 	b->dev = dev;
41 	b->channel = channel;
42 
43 	return b;
44 }
45 
46 void
47 sndbuf_destroy(struct snd_dbuf *b)
48 {
49 	sndbuf_free(b);
50 	free(b, M_DEVBUF);
51 }
52 
53 bus_addr_t
54 sndbuf_getbufaddr(struct snd_dbuf *buf)
55 {
56 	return (buf->buf_addr);
57 }
58 
59 static void
60 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
61 {
62 	struct snd_dbuf *b = (struct snd_dbuf *)arg;
63 
64 	if (bootverbose) {
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);
68 	}
69 	if (error == 0)
70 		b->buf_addr = segs[0].ds_addr;
71 	else
72 		b->buf_addr = 0;
73 }
74 
75 /*
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.
78  */
79 
80 int
81 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags,
82     unsigned int size)
83 {
84 	int ret;
85 
86 	b->dmatag = dmatag;
87 	b->dmaflags = dmaflags | BUS_DMA_NOWAIT;
88 	b->maxsize = size;
89 	b->bufsize = b->maxsize;
90 	b->buf_addr = 0;
91 	b->flags |= SNDBUF_F_MANAGED;
92 	if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, b->dmaflags,
93 	    &b->dmamap)) {
94 		sndbuf_free(b);
95 		return (ENOMEM);
96 	}
97 	if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
98 	    sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
99 		sndbuf_free(b);
100 		return (ENOMEM);
101 	}
102 
103 	ret = sndbuf_resize(b, 2, b->maxsize / 2);
104 	if (ret != 0)
105 		sndbuf_free(b);
106 
107 	return (ret);
108 }
109 
110 int
111 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
112 {
113 	b->flags &= ~SNDBUF_F_MANAGED;
114 	if (buf)
115 		b->flags |= SNDBUF_F_MANAGED;
116 	b->buf = buf;
117 	b->maxsize = size;
118 	b->bufsize = b->maxsize;
119 	return sndbuf_resize(b, 2, b->maxsize / 2);
120 }
121 
122 void
123 sndbuf_free(struct snd_dbuf *b)
124 {
125 	if (b->tmpbuf)
126 		free(b->tmpbuf, M_DEVBUF);
127 
128 	if (b->shadbuf)
129 		free(b->shadbuf, M_DEVBUF);
130 
131 	if (b->buf) {
132 		if (b->flags & SNDBUF_F_MANAGED) {
133 			if (b->dmamap)
134 				bus_dmamap_unload(b->dmatag, b->dmamap);
135 			if (b->dmatag)
136 				bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
137 		} else
138 			free(b->buf, M_DEVBUF);
139 	}
140 
141 	b->tmpbuf = NULL;
142 	b->shadbuf = NULL;
143 	b->buf = NULL;
144 	b->sl = 0;
145 	b->dmatag = NULL;
146 	b->dmamap = NULL;
147 }
148 
149 #define SNDBUF_CACHE_SHIFT	5
150 
151 int
152 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
153 {
154 	unsigned int bufsize, allocsize;
155 	u_int8_t *tmpbuf;
156 
157 	chn_lock(b->channel);
158 	if (b->maxsize == 0)
159 		goto out;
160 	if (blkcnt == 0)
161 		blkcnt = b->blkcnt;
162 	if (blksz == 0)
163 		blksz = b->blksz;
164 	if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz) > b->maxsize) {
165 		chn_unlock(b->channel);
166 		return EINVAL;
167 	}
168 	if (blkcnt == b->blkcnt && blksz == b->blksz)
169 		goto out;
170 
171 	bufsize = blkcnt * blksz;
172 
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);
179 		if (snd_verbose > 3)
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);
185 		b->tmpbuf = tmpbuf;
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);
190 
191 	b->blkcnt = blkcnt;
192 	b->blksz = blksz;
193 	b->bufsize = bufsize;
194 
195 	sndbuf_reset(b);
196 out:
197 	chn_unlock(b->channel);
198 	return 0;
199 }
200 
201 int
202 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
203 {
204         unsigned int bufsize, allocsize;
205 	u_int8_t *buf, *tmpbuf, *shadbuf;
206 
207 	if (blkcnt < 2 || blksz < 16)
208 		return EINVAL;
209 
210 	bufsize = blksz * blkcnt;
211 
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);
220 		if (b->buf != NULL)
221 			free(b->buf, M_DEVBUF);
222 		b->buf = buf;
223 		if (b->tmpbuf != NULL)
224 			free(b->tmpbuf, M_DEVBUF);
225 		b->tmpbuf = tmpbuf;
226 		if (b->shadbuf != NULL)
227 			free(b->shadbuf, M_DEVBUF);
228 		b->shadbuf = shadbuf;
229 		if (snd_verbose > 3)
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);
236 
237 	b->blkcnt = blkcnt;
238 	b->blksz = blksz;
239 	b->bufsize = bufsize;
240 	b->maxsize = bufsize;
241 	b->sl = bufsize;
242 
243 	sndbuf_reset(b);
244 
245 	return 0;
246 }
247 
248 /**
249  * @brief Zero out space in buffer free area
250  *
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).
253  *
254  * @param b		buffer context
255  * @param length	number of bytes to blank
256  */
257 void
258 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
259 {
260 	int i;
261 	u_char data, *p;
262 
263 	if (length == 0)
264 		return;
265 	if (length > b->bufsize)
266 		length = b->bufsize;
267 
268 	data = sndbuf_zerodata(b->fmt);
269 
270 	i = sndbuf_getfreeptr(b);
271 	p = sndbuf_getbuf(b);
272 	while (length > 0) {
273 		p[i] = data;
274 		length--;
275 		i++;
276 		if (i >= b->bufsize)
277 			i = 0;
278 	}
279 }
280 
281 /**
282  * @brief Zap buffer contents, resetting "ready area" fields
283  *
284  * @param b	buffer context
285  */
286 void
287 sndbuf_fillsilence(struct snd_dbuf *b)
288 {
289 	if (b->bufsize > 0)
290 		memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
291 	b->rp = 0;
292 	b->rl = b->bufsize;
293 }
294 
295 /**
296  * @brief Reset buffer w/o flushing statistics
297  *
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.
301  *
302  * @param b	buffer context
303  */
304 void
305 sndbuf_softreset(struct snd_dbuf *b)
306 {
307 	b->rl = 0;
308 	if (b->buf && b->bufsize > 0)
309 		sndbuf_clear(b, b->bufsize);
310 }
311 
312 void
313 sndbuf_reset(struct snd_dbuf *b)
314 {
315 	b->hp = 0;
316 	b->rp = 0;
317 	b->rl = 0;
318 	b->dl = 0;
319 	b->prev_total = 0;
320 	b->total = 0;
321 	b->xrun = 0;
322 	if (b->buf && b->bufsize > 0)
323 		sndbuf_clear(b, b->bufsize);
324 	sndbuf_clearshadow(b);
325 }
326 
327 u_int32_t
328 sndbuf_getfmt(struct snd_dbuf *b)
329 {
330 	return b->fmt;
331 }
332 
333 int
334 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
335 {
336 	b->fmt = fmt;
337 	b->bps = 1;
338 	b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
339 	if (b->fmt & AFMT_16BIT)
340 		b->bps <<= 1;
341 	else if (b->fmt & AFMT_24BIT)
342 		b->bps *= 3;
343 	else if (b->fmt & AFMT_32BIT)
344 		b->bps <<= 2;
345 	return 0;
346 }
347 
348 unsigned int
349 sndbuf_getspd(struct snd_dbuf *b)
350 {
351 	return b->spd;
352 }
353 
354 void
355 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
356 {
357 	b->spd = spd;
358 }
359 
360 unsigned int
361 sndbuf_getalign(struct snd_dbuf *b)
362 {
363 	static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
364 
365 	return align[b->bps - 1];
366 }
367 
368 unsigned int
369 sndbuf_getblkcnt(struct snd_dbuf *b)
370 {
371 	return b->blkcnt;
372 }
373 
374 void
375 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
376 {
377 	b->blkcnt = blkcnt;
378 }
379 
380 unsigned int
381 sndbuf_getblksz(struct snd_dbuf *b)
382 {
383 	return b->blksz;
384 }
385 
386 void
387 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
388 {
389 	b->blksz = blksz;
390 }
391 
392 unsigned int
393 sndbuf_getbps(struct snd_dbuf *b)
394 {
395 	return b->bps;
396 }
397 
398 void *
399 sndbuf_getbuf(struct snd_dbuf *b)
400 {
401 	return b->buf;
402 }
403 
404 void *
405 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
406 {
407 	KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
408 
409 	return b->buf + ofs;
410 }
411 
412 unsigned int
413 sndbuf_getsize(struct snd_dbuf *b)
414 {
415 	return b->bufsize;
416 }
417 
418 unsigned int
419 sndbuf_getmaxsize(struct snd_dbuf *b)
420 {
421 	return b->maxsize;
422 }
423 
424 unsigned int
425 sndbuf_getallocsize(struct snd_dbuf *b)
426 {
427 	return b->allocsize;
428 }
429 
430 unsigned int
431 sndbuf_runsz(struct snd_dbuf *b)
432 {
433 	return b->dl;
434 }
435 
436 void
437 sndbuf_setrun(struct snd_dbuf *b, int go)
438 {
439 	b->dl = go? b->blksz : 0;
440 }
441 
442 struct selinfo *
443 sndbuf_getsel(struct snd_dbuf *b)
444 {
445 	return &b->sel;
446 }
447 
448 /************************************************************/
449 unsigned int
450 sndbuf_getxrun(struct snd_dbuf *b)
451 {
452 	SNDBUF_LOCKASSERT(b);
453 
454 	return b->xrun;
455 }
456 
457 void
458 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
459 {
460 	SNDBUF_LOCKASSERT(b);
461 
462 	b->xrun = xrun;
463 }
464 
465 unsigned int
466 sndbuf_gethwptr(struct snd_dbuf *b)
467 {
468 	SNDBUF_LOCKASSERT(b);
469 
470 	return b->hp;
471 }
472 
473 void
474 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
475 {
476 	SNDBUF_LOCKASSERT(b);
477 
478 	b->hp = ptr;
479 }
480 
481 unsigned int
482 sndbuf_getready(struct snd_dbuf *b)
483 {
484 	SNDBUF_LOCKASSERT(b);
485 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
486 
487 	return b->rl;
488 }
489 
490 unsigned int
491 sndbuf_getreadyptr(struct snd_dbuf *b)
492 {
493 	SNDBUF_LOCKASSERT(b);
494 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
495 
496 	return b->rp;
497 }
498 
499 unsigned int
500 sndbuf_getfree(struct snd_dbuf *b)
501 {
502 	SNDBUF_LOCKASSERT(b);
503 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
504 
505 	return b->bufsize - b->rl;
506 }
507 
508 unsigned int
509 sndbuf_getfreeptr(struct snd_dbuf *b)
510 {
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));
514 
515 	return (b->rp + b->rl) % b->bufsize;
516 }
517 
518 unsigned int
519 sndbuf_getblocks(struct snd_dbuf *b)
520 {
521 	SNDBUF_LOCKASSERT(b);
522 
523 	return b->total / b->blksz;
524 }
525 
526 unsigned int
527 sndbuf_getprevblocks(struct snd_dbuf *b)
528 {
529 	SNDBUF_LOCKASSERT(b);
530 
531 	return b->prev_total / b->blksz;
532 }
533 
534 unsigned int
535 sndbuf_gettotal(struct snd_dbuf *b)
536 {
537 	SNDBUF_LOCKASSERT(b);
538 
539 	return b->total;
540 }
541 
542 void
543 sndbuf_updateprevtotal(struct snd_dbuf *b)
544 {
545 	SNDBUF_LOCKASSERT(b);
546 
547 	b->prev_total = b->total;
548 }
549 
550 unsigned int
551 snd_xbytes(unsigned int v, unsigned int from, unsigned int to)
552 {
553 	unsigned int w, x, y;
554 
555 	if (from == to)
556 		return v;
557 
558 	if (from == 0 || to == 0 || v == 0)
559 		return 0;
560 
561 	x = from;
562 	y = to;
563 	while (y != 0) {
564 		w = x % y;
565 		x = y;
566 		y = w;
567 	}
568 	from /= x;
569 	to /= x;
570 
571 	return (unsigned int)(((u_int64_t)v * to) / from);
572 }
573 
574 unsigned int
575 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
576 {
577 	if (from == NULL || to == NULL || v == 0)
578 		return 0;
579 
580 	return snd_xbytes(v, sndbuf_getbps(from) * sndbuf_getspd(from),
581 	    sndbuf_getbps(to) * sndbuf_getspd(to));
582 }
583 
584 u_int8_t
585 sndbuf_zerodata(u_int32_t fmt)
586 {
587 	if (fmt & AFMT_SIGNED)
588 		return (0x00);
589 	else if (fmt & AFMT_MU_LAW)
590 		return (0x7f);
591 	else if (fmt & AFMT_A_LAW)
592 		return (0x55);
593 	return (0x80);
594 }
595 
596 /************************************************************/
597 
598 /**
599  * @brief Acquire buffer space to extend ready area
600  *
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.
604  *
605  * @param b	audio buffer
606  * @param from	sample source (optional)
607  * @param count	number of bytes to acquire
608  *
609  * @retval 0	Unconditional
610  */
611 int
612 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
613 {
614 	int l;
615 
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));
618 	b->total += count;
619 	if (from != NULL) {
620 		while (count > 0) {
621 			l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
622 			bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
623 			from += l;
624 			b->rl += l;
625 			count -= l;
626 		}
627 	} else
628 		b->rl += count;
629 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
630 
631 	return 0;
632 }
633 
634 /**
635  * @brief Dispose samples from channel buffer, increasing size of ready area
636  *
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
640  * it points to.
641  *
642  * @param b	PCM channel sound buffer
643  * @param to	destination buffer (optional)
644  * @param count	number of bytes to discard
645  *
646  * @returns 0 unconditionally
647  */
648 int
649 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
650 {
651 	int l;
652 
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));
655 	if (to != NULL) {
656 		while (count > 0) {
657 			l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
658 			bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
659 			to += l;
660 			b->rl -= l;
661 			b->rp = (b->rp + l) % b->bufsize;
662 			count -= l;
663 		}
664 	} else {
665 		b->rl -= count;
666 		b->rp = (b->rp + count) % b->bufsize;
667 	}
668 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
669 
670 	return 0;
671 }
672 
673 /* count is number of bytes we want added to destination buffer */
674 int
675 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
676 {
677 	unsigned int cnt;
678 
679 	KASSERT(count > 0, ("can't feed 0 bytes"));
680 
681 	if (sndbuf_getfree(to) < count)
682 		return EINVAL;
683 
684 	do {
685 		cnt = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
686 		if (cnt) {
687 			sndbuf_acquire(to, to->tmpbuf, cnt);
688 			count -= cnt;
689 		}
690 	} while (count && cnt);
691 
692 	return 0;
693 }
694 
695 /************************************************************/
696 
697 void
698 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
699 {
700 	printf("%s: [", s);
701 	if (what & 0x01)
702 		printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
703 	if (what & 0x02)
704 		printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
705 	if (what & 0x04)
706 		printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
707    	if (what & 0x08)
708 		printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
709 	if (what & 0x10)
710 		printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
711 	printf(" ]\n");
712 }
713 
714 /************************************************************/
715 u_int32_t
716 sndbuf_getflags(struct snd_dbuf *b)
717 {
718 	return b->flags;
719 }
720 
721 void
722 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
723 {
724 	b->flags &= ~flags;
725 	if (on)
726 		b->flags |= flags;
727 }
728 
729 /**
730  * @brief Clear the shadow buffer by filling with samples equal to zero.
731  *
732  * @param b buffer to clear
733  */
734 void
735 sndbuf_clearshadow(struct snd_dbuf *b)
736 {
737 	KASSERT(b != NULL, ("b is a null pointer"));
738 	KASSERT(b->sl >= 0, ("illegal shadow length"));
739 
740 	if ((b->shadbuf != NULL) && (b->sl > 0))
741 		memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
742 }
743 
744 #ifdef OSSV4_EXPERIMENT
745 /**
746  * @brief Return peak value from samples in buffer ready area.
747  *
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
750  * buffers.
751  *
752  * @note  Currently only operates with linear PCM formats.
753  *
754  * @param b buffer to analyze
755  * @param lpeak pointer to store left peak value
756  * @param rpeak pointer to store right peak value
757  */
758 void
759 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
760 {
761 	u_int32_t lpeak, rpeak;
762 
763 	lpeak = 0;
764 	rpeak = 0;
765 
766 	/**
767 	 * @todo fill this in later
768 	 */
769 }
770 #endif
771