xref: /freebsd/sys/dev/sound/pcm/buffer.c (revision 6524d2a5affd02623e6909fff0ca03ba21882a0c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifdef HAVE_KERNEL_OPTION_HEADERS
32 #include "opt_snd.h"
33 #endif
34 
35 #include <dev/sound/pcm/sound.h>
36 
37 #include "feeder_if.h"
38 
39 #define SND_USE_FXDIV
40 #define	SND_DECLARE_FXDIV
41 #include "snd_fxdiv_gen.h"
42 
43 struct snd_dbuf *
sndbuf_create(device_t dev,char * drv,char * desc,struct pcm_channel * channel)44 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
45 {
46 	struct snd_dbuf *b;
47 
48 	b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
49 	snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
50 	b->dev = dev;
51 	b->channel = channel;
52 
53 	return b;
54 }
55 
56 void
sndbuf_destroy(struct snd_dbuf * b)57 sndbuf_destroy(struct snd_dbuf *b)
58 {
59 	sndbuf_free(b);
60 	free(b, M_DEVBUF);
61 }
62 
63 bus_addr_t
sndbuf_getbufaddr(struct snd_dbuf * buf)64 sndbuf_getbufaddr(struct snd_dbuf *buf)
65 {
66 	return (buf->buf_addr);
67 }
68 
69 static void
sndbuf_setmap(void * arg,bus_dma_segment_t * segs,int nseg,int error)70 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
71 {
72 	struct snd_dbuf *b = (struct snd_dbuf *)arg;
73 
74 	if (snd_verbose > 3) {
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);
78 	}
79 	if (error == 0)
80 		b->buf_addr = segs[0].ds_addr;
81 	else
82 		b->buf_addr = 0;
83 }
84 
85 /*
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.
88  */
89 
90 int
sndbuf_alloc(struct snd_dbuf * b,bus_dma_tag_t dmatag,int dmaflags,unsigned int size)91 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags,
92     unsigned int size)
93 {
94 	int ret;
95 
96 	b->dmatag = dmatag;
97 	b->dmaflags = dmaflags | BUS_DMA_NOWAIT | BUS_DMA_COHERENT;
98 	b->maxsize = size;
99 	b->bufsize = b->maxsize;
100 	b->buf_addr = 0;
101 	b->flags |= SNDBUF_F_MANAGED;
102 	if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, b->dmaflags,
103 	    &b->dmamap)) {
104 		sndbuf_free(b);
105 		return (ENOMEM);
106 	}
107 	if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
108 	    sndbuf_setmap, b, BUS_DMA_NOWAIT) != 0 || b->buf_addr == 0) {
109 		sndbuf_free(b);
110 		return (ENOMEM);
111 	}
112 
113 	ret = sndbuf_resize(b, 2, b->maxsize / 2);
114 	if (ret != 0)
115 		sndbuf_free(b);
116 
117 	return (ret);
118 }
119 
120 int
sndbuf_setup(struct snd_dbuf * b,void * buf,unsigned int size)121 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
122 {
123 	b->flags &= ~SNDBUF_F_MANAGED;
124 	if (buf)
125 		b->flags |= SNDBUF_F_MANAGED;
126 	b->buf = buf;
127 	b->maxsize = size;
128 	b->bufsize = b->maxsize;
129 	return sndbuf_resize(b, 2, b->maxsize / 2);
130 }
131 
132 void
sndbuf_free(struct snd_dbuf * b)133 sndbuf_free(struct snd_dbuf *b)
134 {
135 	if (b->tmpbuf)
136 		free(b->tmpbuf, M_DEVBUF);
137 
138 	if (b->shadbuf)
139 		free(b->shadbuf, M_DEVBUF);
140 
141 	if (b->buf) {
142 		if (b->flags & SNDBUF_F_MANAGED) {
143 			if (b->buf_addr)
144 				bus_dmamap_unload(b->dmatag, b->dmamap);
145 			if (b->dmatag)
146 				bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
147 		} else
148 			free(b->buf, M_DEVBUF);
149 	}
150 	seldrain(sndbuf_getsel(b));
151 
152 	b->tmpbuf = NULL;
153 	b->shadbuf = NULL;
154 	b->buf = NULL;
155 	b->sl = 0;
156 	b->dmatag = NULL;
157 	b->dmamap = NULL;
158 }
159 
160 #define SNDBUF_CACHE_SHIFT	5
161 
162 int
sndbuf_resize(struct snd_dbuf * b,unsigned int blkcnt,unsigned int blksz)163 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
164 {
165 	unsigned int bufsize, allocsize;
166 	u_int8_t *tmpbuf;
167 
168 	CHN_LOCK(b->channel);
169 	if (b->maxsize == 0)
170 		goto out;
171 	if (blkcnt == 0)
172 		blkcnt = b->blkcnt;
173 	if (blksz == 0)
174 		blksz = b->blksz;
175 	if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz) > b->maxsize) {
176 		CHN_UNLOCK(b->channel);
177 		return EINVAL;
178 	}
179 	if (blkcnt == b->blkcnt && blksz == b->blksz)
180 		goto out;
181 
182 	bufsize = blkcnt * blksz;
183 
184 	if (bufsize > b->allocsize ||
185 	    bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
186 		allocsize = round_page(bufsize);
187 		CHN_UNLOCK(b->channel);
188 		tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
189 		CHN_LOCK(b->channel);
190 		if (snd_verbose > 3)
191 			printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n",
192 			    __func__, b, b->tmpbuf, tmpbuf,
193 			    b->allocsize, allocsize, bufsize);
194 		if (b->tmpbuf != NULL)
195 			free(b->tmpbuf, M_DEVBUF);
196 		b->tmpbuf = tmpbuf;
197 		b->allocsize = allocsize;
198 	} else if (snd_verbose > 3)
199 		printf("%s(): b=%p %d [%d] NOCHANGE\n",
200 		    __func__, b, b->allocsize, b->bufsize);
201 
202 	b->blkcnt = blkcnt;
203 	b->blksz = blksz;
204 	b->bufsize = bufsize;
205 
206 	sndbuf_reset(b);
207 out:
208 	CHN_UNLOCK(b->channel);
209 	return 0;
210 }
211 
212 int
sndbuf_remalloc(struct snd_dbuf * b,unsigned int blkcnt,unsigned int blksz)213 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
214 {
215         unsigned int bufsize, allocsize;
216 	u_int8_t *buf, *tmpbuf, *shadbuf;
217 
218 	if (blkcnt < 2 || blksz < 16)
219 		return EINVAL;
220 
221 	bufsize = blksz * blkcnt;
222 
223 	if (bufsize > b->allocsize ||
224 	    bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
225 		allocsize = round_page(bufsize);
226 		CHN_UNLOCK(b->channel);
227 		buf = malloc(allocsize, M_DEVBUF, M_WAITOK);
228 		tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
229 		shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
230 		CHN_LOCK(b->channel);
231 		if (b->buf != NULL)
232 			free(b->buf, M_DEVBUF);
233 		b->buf = buf;
234 		if (b->tmpbuf != NULL)
235 			free(b->tmpbuf, M_DEVBUF);
236 		b->tmpbuf = tmpbuf;
237 		if (b->shadbuf != NULL)
238 			free(b->shadbuf, M_DEVBUF);
239 		b->shadbuf = shadbuf;
240 		if (snd_verbose > 3)
241 			printf("%s(): b=%p %d -> %d [%d]\n",
242 			    __func__, b, b->allocsize, allocsize, bufsize);
243 		b->allocsize = allocsize;
244 	} else if (snd_verbose > 3)
245 		printf("%s(): b=%p %d [%d] NOCHANGE\n",
246 		    __func__, b, b->allocsize, b->bufsize);
247 
248 	b->blkcnt = blkcnt;
249 	b->blksz = blksz;
250 	b->bufsize = bufsize;
251 	b->maxsize = bufsize;
252 	b->sl = bufsize;
253 
254 	sndbuf_reset(b);
255 
256 	return 0;
257 }
258 
259 /**
260  * @brief Zero out space in buffer free area
261  *
262  * This function clears a chunk of @c length bytes in the buffer free area
263  * (i.e., where the next write will be placed).
264  *
265  * @param b		buffer context
266  * @param length	number of bytes to blank
267  */
268 void
sndbuf_clear(struct snd_dbuf * b,unsigned int length)269 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
270 {
271 	int i;
272 	u_char data, *p;
273 
274 	if (length == 0)
275 		return;
276 	if (length > b->bufsize)
277 		length = b->bufsize;
278 
279 	data = sndbuf_zerodata(b->fmt);
280 
281 	i = sndbuf_getfreeptr(b);
282 	p = sndbuf_getbuf(b);
283 	while (length > 0) {
284 		p[i] = data;
285 		length--;
286 		i++;
287 		if (i >= b->bufsize)
288 			i = 0;
289 	}
290 }
291 
292 /**
293  * @brief Zap buffer contents, resetting "ready area" fields
294  *
295  * @param b	buffer context
296  */
297 void
sndbuf_fillsilence(struct snd_dbuf * b)298 sndbuf_fillsilence(struct snd_dbuf *b)
299 {
300 	if (b->bufsize > 0)
301 		memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
302 	b->rp = 0;
303 	b->rl = b->bufsize;
304 }
305 
306 void
sndbuf_fillsilence_rl(struct snd_dbuf * b,u_int rl)307 sndbuf_fillsilence_rl(struct snd_dbuf *b, u_int rl)
308 {
309 	if (b->bufsize > 0)
310 		memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
311 	b->rp = 0;
312 	b->rl = min(b->bufsize, rl);
313 }
314 
315 /**
316  * @brief Reset buffer w/o flushing statistics
317  *
318  * This function just zeroes out buffer contents and sets the "ready length"
319  * to zero.  This was originally to facilitate minimal playback interruption
320  * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls.
321  *
322  * @param b	buffer context
323  */
324 void
sndbuf_softreset(struct snd_dbuf * b)325 sndbuf_softreset(struct snd_dbuf *b)
326 {
327 	b->rl = 0;
328 	if (b->buf && b->bufsize > 0)
329 		sndbuf_clear(b, b->bufsize);
330 }
331 
332 void
sndbuf_reset(struct snd_dbuf * b)333 sndbuf_reset(struct snd_dbuf *b)
334 {
335 	b->hp = 0;
336 	b->rp = 0;
337 	b->rl = 0;
338 	b->dl = 0;
339 	b->prev_total = 0;
340 	b->total = 0;
341 	b->xrun = 0;
342 	if (b->buf && b->bufsize > 0)
343 		sndbuf_clear(b, b->bufsize);
344 	sndbuf_clearshadow(b);
345 }
346 
347 u_int32_t
sndbuf_getfmt(struct snd_dbuf * b)348 sndbuf_getfmt(struct snd_dbuf *b)
349 {
350 	return b->fmt;
351 }
352 
353 int
sndbuf_setfmt(struct snd_dbuf * b,u_int32_t fmt)354 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
355 {
356 	b->fmt = fmt;
357 	b->bps = AFMT_BPS(b->fmt);
358 	b->align = AFMT_ALIGN(b->fmt);
359 	return 0;
360 }
361 
362 unsigned int
sndbuf_getspd(struct snd_dbuf * b)363 sndbuf_getspd(struct snd_dbuf *b)
364 {
365 	return b->spd;
366 }
367 
368 void
sndbuf_setspd(struct snd_dbuf * b,unsigned int spd)369 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
370 {
371 	b->spd = spd;
372 }
373 
374 unsigned int
sndbuf_getalign(struct snd_dbuf * b)375 sndbuf_getalign(struct snd_dbuf *b)
376 {
377 	return (b->align);
378 }
379 
380 unsigned int
sndbuf_getblkcnt(struct snd_dbuf * b)381 sndbuf_getblkcnt(struct snd_dbuf *b)
382 {
383 	return b->blkcnt;
384 }
385 
386 void
sndbuf_setblkcnt(struct snd_dbuf * b,unsigned int blkcnt)387 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
388 {
389 	b->blkcnt = blkcnt;
390 }
391 
392 unsigned int
sndbuf_getblksz(struct snd_dbuf * b)393 sndbuf_getblksz(struct snd_dbuf *b)
394 {
395 	return b->blksz;
396 }
397 
398 void
sndbuf_setblksz(struct snd_dbuf * b,unsigned int blksz)399 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
400 {
401 	b->blksz = blksz;
402 }
403 
404 unsigned int
sndbuf_getbps(struct snd_dbuf * b)405 sndbuf_getbps(struct snd_dbuf *b)
406 {
407 	return b->bps;
408 }
409 
410 void *
sndbuf_getbuf(struct snd_dbuf * b)411 sndbuf_getbuf(struct snd_dbuf *b)
412 {
413 	return b->buf;
414 }
415 
416 void *
sndbuf_getbufofs(struct snd_dbuf * b,unsigned int ofs)417 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
418 {
419 	KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
420 
421 	return b->buf + ofs;
422 }
423 
424 unsigned int
sndbuf_getsize(struct snd_dbuf * b)425 sndbuf_getsize(struct snd_dbuf *b)
426 {
427 	return b->bufsize;
428 }
429 
430 unsigned int
sndbuf_getmaxsize(struct snd_dbuf * b)431 sndbuf_getmaxsize(struct snd_dbuf *b)
432 {
433 	return b->maxsize;
434 }
435 
436 unsigned int
sndbuf_getallocsize(struct snd_dbuf * b)437 sndbuf_getallocsize(struct snd_dbuf *b)
438 {
439 	return b->allocsize;
440 }
441 
442 unsigned int
sndbuf_runsz(struct snd_dbuf * b)443 sndbuf_runsz(struct snd_dbuf *b)
444 {
445 	return b->dl;
446 }
447 
448 void
sndbuf_setrun(struct snd_dbuf * b,int go)449 sndbuf_setrun(struct snd_dbuf *b, int go)
450 {
451 	b->dl = go? b->blksz : 0;
452 }
453 
454 struct selinfo *
sndbuf_getsel(struct snd_dbuf * b)455 sndbuf_getsel(struct snd_dbuf *b)
456 {
457 	return &b->sel;
458 }
459 
460 /************************************************************/
461 unsigned int
sndbuf_getxrun(struct snd_dbuf * b)462 sndbuf_getxrun(struct snd_dbuf *b)
463 {
464 	return b->xrun;
465 }
466 
467 void
sndbuf_setxrun(struct snd_dbuf * b,unsigned int xrun)468 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
469 {
470 	b->xrun = xrun;
471 }
472 
473 unsigned int
sndbuf_gethwptr(struct snd_dbuf * b)474 sndbuf_gethwptr(struct snd_dbuf *b)
475 {
476 	return b->hp;
477 }
478 
479 void
sndbuf_sethwptr(struct snd_dbuf * b,unsigned int ptr)480 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
481 {
482 	b->hp = ptr;
483 }
484 
485 unsigned int
sndbuf_getready(struct snd_dbuf * b)486 sndbuf_getready(struct snd_dbuf *b)
487 {
488 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
489 
490 	return b->rl;
491 }
492 
493 unsigned int
sndbuf_getreadyptr(struct snd_dbuf * b)494 sndbuf_getreadyptr(struct snd_dbuf *b)
495 {
496 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
497 
498 	return b->rp;
499 }
500 
501 unsigned int
sndbuf_getfree(struct snd_dbuf * b)502 sndbuf_getfree(struct snd_dbuf *b)
503 {
504 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
505 
506 	return b->bufsize - b->rl;
507 }
508 
509 unsigned int
sndbuf_getfreeptr(struct snd_dbuf * b)510 sndbuf_getfreeptr(struct snd_dbuf *b)
511 {
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 u_int64_t
sndbuf_getblocks(struct snd_dbuf * b)519 sndbuf_getblocks(struct snd_dbuf *b)
520 {
521 	return b->total / b->blksz;
522 }
523 
524 u_int64_t
sndbuf_getprevblocks(struct snd_dbuf * b)525 sndbuf_getprevblocks(struct snd_dbuf *b)
526 {
527 	return b->prev_total / b->blksz;
528 }
529 
530 u_int64_t
sndbuf_gettotal(struct snd_dbuf * b)531 sndbuf_gettotal(struct snd_dbuf *b)
532 {
533 	return b->total;
534 }
535 
536 u_int64_t
sndbuf_getprevtotal(struct snd_dbuf * b)537 sndbuf_getprevtotal(struct snd_dbuf *b)
538 {
539 	return b->prev_total;
540 }
541 
542 void
sndbuf_updateprevtotal(struct snd_dbuf * b)543 sndbuf_updateprevtotal(struct snd_dbuf *b)
544 {
545 	b->prev_total = b->total;
546 }
547 
548 unsigned int
sndbuf_xbytes(unsigned int v,struct snd_dbuf * from,struct snd_dbuf * to)549 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
550 {
551 	if (from == NULL || to == NULL || v == 0)
552 		return 0;
553 
554 	return snd_xbytes(v, sndbuf_getalign(from) * sndbuf_getspd(from),
555 	    sndbuf_getalign(to) * sndbuf_getspd(to));
556 }
557 
558 u_int8_t
sndbuf_zerodata(u_int32_t fmt)559 sndbuf_zerodata(u_int32_t fmt)
560 {
561 	if (fmt & (AFMT_SIGNED | AFMT_PASSTHROUGH))
562 		return (0x00);
563 	else if (fmt & AFMT_MU_LAW)
564 		return (0x7f);
565 	else if (fmt & AFMT_A_LAW)
566 		return (0x55);
567 	return (0x80);
568 }
569 
570 /************************************************************/
571 
572 /**
573  * @brief Acquire buffer space to extend ready area
574  *
575  * This function extends the ready area length by @c count bytes, and may
576  * optionally copy samples from another location stored in @c from.  The
577  * counter @c snd_dbuf::total is also incremented by @c count bytes.
578  *
579  * @param b	audio buffer
580  * @param from	sample source (optional)
581  * @param count	number of bytes to acquire
582  *
583  * @retval 0	Unconditional
584  */
585 int
sndbuf_acquire(struct snd_dbuf * b,u_int8_t * from,unsigned int count)586 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
587 {
588 	int l;
589 
590 	KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
591 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
592 	b->total += count;
593 	if (from != NULL) {
594 		while (count > 0) {
595 			l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
596 			bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
597 			from += l;
598 			b->rl += l;
599 			count -= l;
600 		}
601 	} else
602 		b->rl += count;
603 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
604 
605 	return 0;
606 }
607 
608 /**
609  * @brief Dispose samples from channel buffer, increasing size of ready area
610  *
611  * This function discards samples from the supplied buffer by advancing the
612  * ready area start pointer and decrementing the ready area length.  If
613  * @c to is not NULL, then the discard samples will be copied to the location
614  * it points to.
615  *
616  * @param b	PCM channel sound buffer
617  * @param to	destination buffer (optional)
618  * @param count	number of bytes to discard
619  *
620  * @returns 0 unconditionally
621  */
622 int
sndbuf_dispose(struct snd_dbuf * b,u_int8_t * to,unsigned int count)623 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
624 {
625 	int l;
626 
627 	KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
628 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
629 	if (to != NULL) {
630 		while (count > 0) {
631 			l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
632 			bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
633 			to += l;
634 			b->rl -= l;
635 			b->rp = (b->rp + l) % b->bufsize;
636 			count -= l;
637 		}
638 	} else {
639 		b->rl -= count;
640 		b->rp = (b->rp + count) % b->bufsize;
641 	}
642 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
643 
644 	return 0;
645 }
646 
647 #ifdef SND_DIAGNOSTIC
648 static uint32_t snd_feeder_maxfeed = 0;
649 SYSCTL_UINT(_hw_snd, OID_AUTO, feeder_maxfeed, CTLFLAG_RD,
650     &snd_feeder_maxfeed, 0, "maximum feeder count request");
651 
652 static uint32_t snd_feeder_maxcycle = 0;
653 SYSCTL_UINT(_hw_snd, OID_AUTO, feeder_maxcycle, CTLFLAG_RD,
654     &snd_feeder_maxcycle, 0, "maximum feeder cycle");
655 #endif
656 
657 /* count is number of bytes we want added to destination buffer */
658 int
sndbuf_feed(struct snd_dbuf * from,struct snd_dbuf * to,struct pcm_channel * channel,struct pcm_feeder * feeder,unsigned int count)659 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
660 {
661 	unsigned int cnt, maxfeed;
662 #ifdef SND_DIAGNOSTIC
663 	unsigned int cycle;
664 
665 	if (count > snd_feeder_maxfeed)
666 		snd_feeder_maxfeed = count;
667 
668 	cycle = 0;
669 #endif
670 
671 	KASSERT(count > 0, ("can't feed 0 bytes"));
672 
673 	if (sndbuf_getfree(to) < count)
674 		return (EINVAL);
675 
676 	maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(to));
677 
678 	do {
679 		cnt = FEEDER_FEED(feeder, channel, to->tmpbuf,
680 		    min(count, maxfeed), from);
681 		if (cnt == 0)
682 			break;
683 		sndbuf_acquire(to, to->tmpbuf, cnt);
684 		count -= cnt;
685 #ifdef SND_DIAGNOSTIC
686 		cycle++;
687 #endif
688 	} while (count != 0);
689 
690 #ifdef SND_DIAGNOSTIC
691 	if (cycle > snd_feeder_maxcycle)
692 		snd_feeder_maxcycle = cycle;
693 #endif
694 
695 	return (0);
696 }
697 
698 /************************************************************/
699 
700 void
sndbuf_dump(struct snd_dbuf * b,char * s,u_int32_t what)701 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
702 {
703 	printf("%s: [", s);
704 	if (what & 0x01)
705 		printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
706 	if (what & 0x02)
707 		printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
708 	if (what & 0x04)
709 		printf(" total: %ju, prev_total: %ju, xrun: %d", (uintmax_t)b->total, (uintmax_t)b->prev_total, b->xrun);
710    	if (what & 0x08)
711 		printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
712 	if (what & 0x10)
713 		printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
714 	printf(" ]\n");
715 }
716 
717 /************************************************************/
718 u_int32_t
sndbuf_getflags(struct snd_dbuf * b)719 sndbuf_getflags(struct snd_dbuf *b)
720 {
721 	return b->flags;
722 }
723 
724 void
sndbuf_setflags(struct snd_dbuf * b,u_int32_t flags,int on)725 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
726 {
727 	b->flags &= ~flags;
728 	if (on)
729 		b->flags |= flags;
730 }
731 
732 /**
733  * @brief Clear the shadow buffer by filling with samples equal to zero.
734  *
735  * @param b buffer to clear
736  */
737 void
sndbuf_clearshadow(struct snd_dbuf * b)738 sndbuf_clearshadow(struct snd_dbuf *b)
739 {
740 	KASSERT(b != NULL, ("b is a null pointer"));
741 	KASSERT(b->sl >= 0, ("illegal shadow length"));
742 
743 	if ((b->shadbuf != NULL) && (b->sl > 0))
744 		memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
745 }
746 
747 #ifdef OSSV4_EXPERIMENT
748 /**
749  * @brief Return peak value from samples in buffer ready area.
750  *
751  * Peak ranges from 0-32767.  If channel is monaural, most significant 16
752  * bits will be zero.  For now, only expects to work with 1-2 channel
753  * buffers.
754  *
755  * @note  Currently only operates with linear PCM formats.
756  *
757  * @param b buffer to analyze
758  * @param lpeak pointer to store left peak value
759  * @param rpeak pointer to store right peak value
760  */
761 void
sndbuf_getpeaks(struct snd_dbuf * b,int * lp,int * rp)762 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
763 {
764 	u_int32_t lpeak, rpeak;
765 
766 	lpeak = 0;
767 	rpeak = 0;
768 
769 	/**
770 	 * @todo fill this in later
771 	 */
772 }
773 #endif
774