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 SNDBUF_LOCKASSERT(b);
465
466 return b->xrun;
467 }
468
469 void
sndbuf_setxrun(struct snd_dbuf * b,unsigned int xrun)470 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
471 {
472 SNDBUF_LOCKASSERT(b);
473
474 b->xrun = xrun;
475 }
476
477 unsigned int
sndbuf_gethwptr(struct snd_dbuf * b)478 sndbuf_gethwptr(struct snd_dbuf *b)
479 {
480 SNDBUF_LOCKASSERT(b);
481
482 return b->hp;
483 }
484
485 void
sndbuf_sethwptr(struct snd_dbuf * b,unsigned int ptr)486 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
487 {
488 SNDBUF_LOCKASSERT(b);
489
490 b->hp = ptr;
491 }
492
493 unsigned int
sndbuf_getready(struct snd_dbuf * b)494 sndbuf_getready(struct snd_dbuf *b)
495 {
496 SNDBUF_LOCKASSERT(b);
497 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
498
499 return b->rl;
500 }
501
502 unsigned int
sndbuf_getreadyptr(struct snd_dbuf * b)503 sndbuf_getreadyptr(struct snd_dbuf *b)
504 {
505 SNDBUF_LOCKASSERT(b);
506 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
507
508 return b->rp;
509 }
510
511 unsigned int
sndbuf_getfree(struct snd_dbuf * b)512 sndbuf_getfree(struct snd_dbuf *b)
513 {
514 SNDBUF_LOCKASSERT(b);
515 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
516
517 return b->bufsize - b->rl;
518 }
519
520 unsigned int
sndbuf_getfreeptr(struct snd_dbuf * b)521 sndbuf_getfreeptr(struct snd_dbuf *b)
522 {
523 SNDBUF_LOCKASSERT(b);
524 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
525 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
526
527 return (b->rp + b->rl) % b->bufsize;
528 }
529
530 u_int64_t
sndbuf_getblocks(struct snd_dbuf * b)531 sndbuf_getblocks(struct snd_dbuf *b)
532 {
533 SNDBUF_LOCKASSERT(b);
534
535 return b->total / b->blksz;
536 }
537
538 u_int64_t
sndbuf_getprevblocks(struct snd_dbuf * b)539 sndbuf_getprevblocks(struct snd_dbuf *b)
540 {
541 SNDBUF_LOCKASSERT(b);
542
543 return b->prev_total / b->blksz;
544 }
545
546 u_int64_t
sndbuf_gettotal(struct snd_dbuf * b)547 sndbuf_gettotal(struct snd_dbuf *b)
548 {
549 SNDBUF_LOCKASSERT(b);
550
551 return b->total;
552 }
553
554 u_int64_t
sndbuf_getprevtotal(struct snd_dbuf * b)555 sndbuf_getprevtotal(struct snd_dbuf *b)
556 {
557 SNDBUF_LOCKASSERT(b);
558
559 return b->prev_total;
560 }
561
562 void
sndbuf_updateprevtotal(struct snd_dbuf * b)563 sndbuf_updateprevtotal(struct snd_dbuf *b)
564 {
565 SNDBUF_LOCKASSERT(b);
566
567 b->prev_total = b->total;
568 }
569
570 unsigned int
sndbuf_xbytes(unsigned int v,struct snd_dbuf * from,struct snd_dbuf * to)571 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
572 {
573 if (from == NULL || to == NULL || v == 0)
574 return 0;
575
576 return snd_xbytes(v, sndbuf_getalign(from) * sndbuf_getspd(from),
577 sndbuf_getalign(to) * sndbuf_getspd(to));
578 }
579
580 u_int8_t
sndbuf_zerodata(u_int32_t fmt)581 sndbuf_zerodata(u_int32_t fmt)
582 {
583 if (fmt & (AFMT_SIGNED | AFMT_PASSTHROUGH))
584 return (0x00);
585 else if (fmt & AFMT_MU_LAW)
586 return (0x7f);
587 else if (fmt & AFMT_A_LAW)
588 return (0x55);
589 return (0x80);
590 }
591
592 /************************************************************/
593
594 /**
595 * @brief Acquire buffer space to extend ready area
596 *
597 * This function extends the ready area length by @c count bytes, and may
598 * optionally copy samples from another location stored in @c from. The
599 * counter @c snd_dbuf::total is also incremented by @c count bytes.
600 *
601 * @param b audio buffer
602 * @param from sample source (optional)
603 * @param count number of bytes to acquire
604 *
605 * @retval 0 Unconditional
606 */
607 int
sndbuf_acquire(struct snd_dbuf * b,u_int8_t * from,unsigned int count)608 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
609 {
610 int l;
611
612 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
613 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
614 b->total += count;
615 if (from != NULL) {
616 while (count > 0) {
617 l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
618 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
619 from += l;
620 b->rl += l;
621 count -= l;
622 }
623 } else
624 b->rl += count;
625 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
626
627 return 0;
628 }
629
630 /**
631 * @brief Dispose samples from channel buffer, increasing size of ready area
632 *
633 * This function discards samples from the supplied buffer by advancing the
634 * ready area start pointer and decrementing the ready area length. If
635 * @c to is not NULL, then the discard samples will be copied to the location
636 * it points to.
637 *
638 * @param b PCM channel sound buffer
639 * @param to destination buffer (optional)
640 * @param count number of bytes to discard
641 *
642 * @returns 0 unconditionally
643 */
644 int
sndbuf_dispose(struct snd_dbuf * b,u_int8_t * to,unsigned int count)645 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
646 {
647 int l;
648
649 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
650 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
651 if (to != NULL) {
652 while (count > 0) {
653 l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
654 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
655 to += l;
656 b->rl -= l;
657 b->rp = (b->rp + l) % b->bufsize;
658 count -= l;
659 }
660 } else {
661 b->rl -= count;
662 b->rp = (b->rp + count) % b->bufsize;
663 }
664 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
665
666 return 0;
667 }
668
669 #ifdef SND_DIAGNOSTIC
670 static uint32_t snd_feeder_maxfeed = 0;
671 SYSCTL_UINT(_hw_snd, OID_AUTO, feeder_maxfeed, CTLFLAG_RD,
672 &snd_feeder_maxfeed, 0, "maximum feeder count request");
673
674 static uint32_t snd_feeder_maxcycle = 0;
675 SYSCTL_UINT(_hw_snd, OID_AUTO, feeder_maxcycle, CTLFLAG_RD,
676 &snd_feeder_maxcycle, 0, "maximum feeder cycle");
677 #endif
678
679 /* count is number of bytes we want added to destination buffer */
680 int
sndbuf_feed(struct snd_dbuf * from,struct snd_dbuf * to,struct pcm_channel * channel,struct pcm_feeder * feeder,unsigned int count)681 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
682 {
683 unsigned int cnt, maxfeed;
684 #ifdef SND_DIAGNOSTIC
685 unsigned int cycle;
686
687 if (count > snd_feeder_maxfeed)
688 snd_feeder_maxfeed = count;
689
690 cycle = 0;
691 #endif
692
693 KASSERT(count > 0, ("can't feed 0 bytes"));
694
695 if (sndbuf_getfree(to) < count)
696 return (EINVAL);
697
698 maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(to));
699
700 do {
701 cnt = FEEDER_FEED(feeder, channel, to->tmpbuf,
702 min(count, maxfeed), from);
703 if (cnt == 0)
704 break;
705 sndbuf_acquire(to, to->tmpbuf, cnt);
706 count -= cnt;
707 #ifdef SND_DIAGNOSTIC
708 cycle++;
709 #endif
710 } while (count != 0);
711
712 #ifdef SND_DIAGNOSTIC
713 if (cycle > snd_feeder_maxcycle)
714 snd_feeder_maxcycle = cycle;
715 #endif
716
717 return (0);
718 }
719
720 /************************************************************/
721
722 void
sndbuf_dump(struct snd_dbuf * b,char * s,u_int32_t what)723 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
724 {
725 printf("%s: [", s);
726 if (what & 0x01)
727 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
728 if (what & 0x02)
729 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
730 if (what & 0x04)
731 printf(" total: %ju, prev_total: %ju, xrun: %d", (uintmax_t)b->total, (uintmax_t)b->prev_total, b->xrun);
732 if (what & 0x08)
733 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
734 if (what & 0x10)
735 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
736 printf(" ]\n");
737 }
738
739 /************************************************************/
740 u_int32_t
sndbuf_getflags(struct snd_dbuf * b)741 sndbuf_getflags(struct snd_dbuf *b)
742 {
743 return b->flags;
744 }
745
746 void
sndbuf_setflags(struct snd_dbuf * b,u_int32_t flags,int on)747 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
748 {
749 b->flags &= ~flags;
750 if (on)
751 b->flags |= flags;
752 }
753
754 /**
755 * @brief Clear the shadow buffer by filling with samples equal to zero.
756 *
757 * @param b buffer to clear
758 */
759 void
sndbuf_clearshadow(struct snd_dbuf * b)760 sndbuf_clearshadow(struct snd_dbuf *b)
761 {
762 KASSERT(b != NULL, ("b is a null pointer"));
763 KASSERT(b->sl >= 0, ("illegal shadow length"));
764
765 if ((b->shadbuf != NULL) && (b->sl > 0))
766 memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
767 }
768
769 #ifdef OSSV4_EXPERIMENT
770 /**
771 * @brief Return peak value from samples in buffer ready area.
772 *
773 * Peak ranges from 0-32767. If channel is monaural, most significant 16
774 * bits will be zero. For now, only expects to work with 1-2 channel
775 * buffers.
776 *
777 * @note Currently only operates with linear PCM formats.
778 *
779 * @param b buffer to analyze
780 * @param lpeak pointer to store left peak value
781 * @param rpeak pointer to store right peak value
782 */
783 void
sndbuf_getpeaks(struct snd_dbuf * b,int * lp,int * rp)784 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
785 {
786 u_int32_t lpeak, rpeak;
787
788 lpeak = 0;
789 rpeak = 0;
790
791 /**
792 * @todo fill this in later
793 */
794 }
795 #endif
796