xref: /freebsd/sys/dev/sound/pcm/buffer.h (revision 5f62a964e9f8abc6a05d8338273fadd154f0a206)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5  * All rights reserved.
6  * Copyright (c) 2025 The FreeBSD Foundation
7  *
8  * Portions of this software were developed by Christos Margiolis
9  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #define	SNDBUF_F_MANAGED	0x00000001
34 
35 #define SNDBUF_NAMELEN	48
36 
37 struct snd_dbuf {
38 	u_int8_t *buf, *tmpbuf;
39 	u_int8_t *shadbuf; /**< shadow buffer used w/ S_D_SILENCE/SKIP */
40 	volatile int sl; /**< shadbuf ready length in # of bytes */
41 	unsigned int bufsize, maxsize, allocsize;
42 	volatile int dl; /* transfer size */
43 	volatile int rp; /* pointers to the ready area */
44 	volatile int rl; /* length of ready area */
45 	volatile int hp;
46 	volatile u_int64_t total, prev_total;
47 	int dmachan;       /* dma channel */
48 	u_int32_t fmt, spd, bps, align;
49 	unsigned int blksz, blkcnt;
50 	int xrun;
51 	u_int32_t flags;
52 	bus_dmamap_t dmamap;
53 	bus_dma_tag_t dmatag;
54 	bus_addr_t buf_addr;
55 	int dmaflags;
56 	struct selinfo sel;
57 	struct pcm_channel *channel;
58 	char name[SNDBUF_NAMELEN];
59 };
60 
61 struct snd_dbuf *sndbuf_create(struct pcm_channel *channel, const char *desc);
62 void sndbuf_destroy(struct snd_dbuf *b);
63 
64 int sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags, unsigned int size);
65 int sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size);
66 void sndbuf_free(struct snd_dbuf *b);
67 int sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz);
68 int sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz);
69 void sndbuf_reset(struct snd_dbuf *b);
70 void sndbuf_clear(struct snd_dbuf *b, unsigned int length);
71 void sndbuf_fillsilence(struct snd_dbuf *b);
72 void sndbuf_fillsilence_rl(struct snd_dbuf *b, u_int rl);
73 void sndbuf_softreset(struct snd_dbuf *b);
74 void sndbuf_clearshadow(struct snd_dbuf *b);
75 
76 int sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt);
77 void sndbuf_setspd(struct snd_dbuf *b, unsigned int spd);
78 
79 void *sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs);
80 unsigned int sndbuf_runsz(struct snd_dbuf *b);
81 void sndbuf_setrun(struct snd_dbuf *b, int go);
82 
83 void sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun);
84 unsigned int sndbuf_getfree(struct snd_dbuf *b);
85 unsigned int sndbuf_getfreeptr(struct snd_dbuf *b);
86 unsigned int sndbuf_getready(struct snd_dbuf *b);
87 unsigned int sndbuf_getreadyptr(struct snd_dbuf *b);
88 u_int64_t sndbuf_getblocks(struct snd_dbuf *b);
89 unsigned int sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to);
90 u_int8_t sndbuf_zerodata(u_int32_t fmt);
91 
92 int sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count);
93 int sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count);
94 int sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count);
95 
96 #ifdef OSSV4_EXPERIMENT
97 void sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp);
98 #endif
99 
100 static inline u_int32_t
101 snd_xbytes(u_int32_t v, u_int32_t from, u_int32_t to)
102 {
103 
104 	if (from == to)
105 		return (v);
106 	if (from == 0)
107 		return (0);
108 	return ((u_int64_t)v * to / from);
109 }
110