xref: /freebsd/sys/dev/sound/pcm/sound.h (revision b19e61f223a1982cce103b9716568391a071892a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
6  * Copyright (c) 1995 Hannu Savolainen
7  * All rights reserved.
8  * Copyright (c) 2024-2025 The FreeBSD Foundation
9  *
10  * Portions of this software were developed by Christos Margiolis
11  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * first, include kernel header files.
37  */
38 
39 #ifndef _OS_H_
40 #define _OS_H_
41 
42 #ifdef _KERNEL
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/eventhandler.h>
46 #include <sys/ioccom.h>
47 #include <sys/filio.h>
48 #include <sys/sockio.h>
49 #include <sys/fcntl.h>
50 #include <sys/selinfo.h>
51 #include <sys/proc.h>
52 #include <sys/kernel.h> /* for DATA_SET */
53 #include <sys/module.h>
54 #include <sys/conf.h>
55 #include <sys/file.h>
56 #include <sys/uio.h>
57 #include <sys/syslog.h>
58 #include <sys/errno.h>
59 #include <sys/malloc.h>
60 #include <sys/bus.h>
61 #include <machine/resource.h>
62 #include <machine/bus.h>
63 #include <sys/rman.h>
64 #include <sys/limits.h>
65 #include <sys/mman.h>
66 #include <sys/poll.h>
67 #include <sys/sbuf.h>
68 #include <sys/soundcard.h>
69 #include <sys/sndstat.h>
70 #include <sys/sysctl.h>
71 #include <sys/kobj.h>
72 #include <vm/vm.h>
73 #include <vm/pmap.h>
74 
75 #include <sys/lock.h>
76 #include <sys/mutex.h>
77 #include <sys/condvar.h>
78 
79 struct pcm_channel;
80 struct pcm_feeder;
81 struct snd_dbuf;
82 struct snd_mixer;
83 
84 #include <dev/sound/pcm/buffer.h>
85 #include <dev/sound/pcm/matrix.h>
86 #include <dev/sound/pcm/channel.h>
87 #include <dev/sound/pcm/feeder.h>
88 #include <dev/sound/pcm/mixer.h>
89 #include <dev/sound/pcm/dsp.h>
90 
91 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
92 
93 #define SND_STATUSLEN	64
94 
95 #define SOUND_MODVER	5
96 
97 #define SOUND_MINVER	SOUND_MODVER
98 #define SOUND_PREFVER	SOUND_MODVER
99 #define SOUND_MAXVER	SOUND_MODVER
100 
101 #define SD_F_SIMPLEX		0x00000001
102 /* unused			0x00000002 */
103 #define SD_F_SOFTPCMVOL		0x00000004
104 #define SD_F_BUSY		0x00000008
105 #define SD_F_MPSAFE		0x00000010
106 #define SD_F_REGISTERED		0x00000020
107 #define SD_F_BITPERFECT		0x00000040
108 #define SD_F_VPC		0x00000080	/* volume-per-channel */
109 #define SD_F_EQ			0x00000100	/* EQ */
110 #define SD_F_EQ_ENABLED		0x00000200	/* EQ enabled */
111 #define SD_F_EQ_BYPASSED	0x00000400	/* EQ bypassed */
112 #define SD_F_EQ_PC		0x00000800	/* EQ per-channel */
113 #define SD_F_PVCHANS		0x00001000	/* Playback vchans enabled */
114 #define SD_F_RVCHANS		0x00002000	/* Recording vchans enabled */
115 
116 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
117 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
118 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
119 
120 #define SD_F_BITS		"\020"					\
121 				"\001SIMPLEX"				\
122 				/* "\002 */				\
123 				"\003SOFTPCMVOL"			\
124 				"\004BUSY"				\
125 				"\005MPSAFE"				\
126 				"\006REGISTERED"			\
127 				"\007BITPERFECT"			\
128 				"\010VPC"				\
129 				"\011EQ"				\
130 				"\012EQ_ENABLED"			\
131 				"\013EQ_BYPASSED"			\
132 				"\014EQ_PC"				\
133 				"\015PVCHANS"				\
134 				"\016RVCHANS"
135 
136 #define PCM_ALIVE(x)		((x) != NULL && (x)->lock != NULL)
137 #define PCM_REGISTERED(x)	(PCM_ALIVE(x) && ((x)->flags & SD_F_REGISTERED))
138 
139 #define	PCM_MAXCHANS		10000
140 #define	PCM_CHANCOUNT(d)	\
141 	(d->playcount + d->pvchancount + d->reccount + d->rvchancount)
142 
143 /* many variables should be reduced to a range. Here define a macro */
144 #define RANGE(var, low, high) (var) = \
145 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
146 
147 extern int snd_unit;
148 extern int snd_verbose;
149 extern devclass_t pcm_devclass;
150 extern struct unrhdr *pcmsg_unrhdr;
151 
152 #ifndef DEB
153 #define DEB(x)
154 #endif
155 
156 SYSCTL_DECL(_hw_snd);
157 
158 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
159 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
160 void pcm_init(device_t dev, void *devinfo);
161 int pcm_register(device_t dev, char *str);
162 int pcm_unregister(device_t dev);
163 u_int32_t pcm_getflags(device_t dev);
164 void pcm_setflags(device_t dev, u_int32_t val);
165 void *pcm_getdevinfo(device_t dev);
166 
167 int snd_setup_intr(device_t dev, struct resource *res, int flags,
168 		   driver_intr_t hand, void *param, void **cookiep);
169 
170 void *snd_mtxcreate(const char *desc, const char *type);
171 void snd_mtxfree(void *m);
172 void snd_mtxassert(void *m);
173 #define	snd_mtxlock(m) mtx_lock(m)
174 #define	snd_mtxunlock(m) mtx_unlock(m)
175 
176 int sndstat_register(device_t dev, char *str);
177 int sndstat_unregister(device_t dev);
178 
179 /* These are the function codes assigned to the children of sound cards. */
180 enum {
181 	SCF_PCM,
182 	SCF_MIDI,
183 };
184 
185 /*
186  * This is the device information struct, used by a bridge device to pass the
187  * device function code to the children.
188  */
189 struct sndcard_func {
190 	int func;	/* The function code. */
191 	void *varinfo;	/* Bridge-specific information. */
192 };
193 
194 /*
195  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
196  * so that the macro versions of pcm_{,un}lock can dereference them.
197  * we also have to do this now makedev() has gone away.
198  */
199 
200 struct snddev_info {
201 	struct {
202 		struct {
203 			SLIST_HEAD(, pcm_channel) head;
204 			struct {
205 				SLIST_HEAD(, pcm_channel) head;
206 			} busy;
207 			struct {
208 				SLIST_HEAD(, pcm_channel) head;
209 			} opened;
210 			struct {
211 				SLIST_HEAD(, pcm_channel) head;
212 			} primary;
213 		} pcm;
214 	} channels;
215 	unsigned playcount, reccount, pvchancount, rvchancount;
216 	unsigned flags;
217 	unsigned int bufsz;
218 	void *devinfo;
219 	device_t dev;
220 	char status[SND_STATUSLEN];
221 	struct mtx *lock;
222 	struct cdev *mixer_dev;
223 	struct cdev *dsp_dev;
224 	uint32_t pvchanrate, pvchanformat, pvchanmode;
225 	uint32_t rvchanrate, rvchanformat, rvchanmode;
226 	int32_t eqpreamp;
227 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
228 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
229 	struct cv cv;
230 	struct unrhdr *p_unr;
231 	struct unrhdr *vp_unr;
232 	struct unrhdr *r_unr;
233 	struct unrhdr *vr_unr;
234 };
235 
236 void	sound_oss_sysinfo(oss_sysinfo *);
237 int	sound_oss_card_info(oss_card_info *);
238 
239 #define	PCM_MODE_MIXER		0x01
240 #define	PCM_MODE_PLAY		0x02
241 #define	PCM_MODE_REC		0x04
242 
243 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
244 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
245 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
246 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
247 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
248 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
249 
250 /*
251  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
252  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
253  */
254 #define PCM_WAIT(x)		do {					\
255 	PCM_LOCKASSERT(x);						\
256 	while ((x)->flags & SD_F_BUSY)					\
257 		cv_wait(&(x)->cv, (x)->lock);				\
258 } while (0)
259 
260 #define PCM_ACQUIRE(x)		do {					\
261 	PCM_LOCKASSERT(x);						\
262 	KASSERT(!((x)->flags & SD_F_BUSY),				\
263 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
264 	    __func__, __LINE__));					\
265 	(x)->flags |= SD_F_BUSY;					\
266 } while (0)
267 
268 #define PCM_RELEASE(x)		do {					\
269 	PCM_LOCKASSERT(x);						\
270 	KASSERT((x)->flags & SD_F_BUSY,					\
271 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
272 	    __func__, __LINE__));					\
273 	(x)->flags &= ~SD_F_BUSY;					\
274 	cv_broadcast(&(x)->cv);						\
275 } while (0)
276 
277 /* Quick version, for shorter path. */
278 #define PCM_ACQUIRE_QUICK(x)	do {					\
279 	PCM_UNLOCKASSERT(x);						\
280 	PCM_LOCK(x);							\
281 	PCM_WAIT(x);							\
282 	PCM_ACQUIRE(x);							\
283 	PCM_UNLOCK(x);							\
284 } while (0)
285 
286 #define PCM_RELEASE_QUICK(x)	do {					\
287 	PCM_UNLOCKASSERT(x);						\
288 	PCM_LOCK(x);							\
289 	PCM_RELEASE(x);							\
290 	PCM_UNLOCK(x);							\
291 } while (0)
292 
293 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
294 				    ((x)->flags & SD_F_BUSY),		\
295 				    ("%s(%d): [PCM BUSYASSERT] "	\
296 				    "Failed, snddev_info=%p",		\
297 				    __func__, __LINE__, x))
298 
299 #define PCM_GIANT_ENTER(x)	do {					\
300 	int _pcm_giant = 0;						\
301 	PCM_UNLOCKASSERT(x);						\
302 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
303 		do {							\
304 			mtx_lock(&Giant);				\
305 			_pcm_giant = 1;					\
306 		} while (0)
307 
308 #define PCM_GIANT_EXIT(x)	do {					\
309 	PCM_UNLOCKASSERT(x);						\
310 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
311 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
312 	    __func__, __LINE__));					\
313 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
314 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
315 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
316 	    __func__, __LINE__));					\
317 	if (_pcm_giant != 0) {						\
318 		mtx_assert(&Giant, MA_OWNED);				\
319 		_pcm_giant = 0;						\
320 		mtx_unlock(&Giant);					\
321 	}								\
322 } while (0)
323 
324 #define PCM_GIANT_LEAVE(x)						\
325 	PCM_GIANT_EXIT(x);						\
326 } while (0)
327 
328 #endif /* _KERNEL */
329 
330 /* make figuring out what a format is easier. got AFMT_STEREO already */
331 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE | \
332 			AFMT_F32_LE | AFMT_F32_BE)
333 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
334 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
335 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
336 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
337 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_F32_LE | AFMT_F32_BE | \
338 			AFMT_S24_LE | AFMT_S24_BE | \
339 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
340 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_F32_BE | \
341 			AFMT_S24_BE | AFMT_U24_BE | AFMT_S16_BE | AFMT_U16_BE)
342 
343 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
344 				 AFMT_32BIT)
345 
346 /* Supported vchan mixing formats */
347 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
348 
349 #define AFMT_PASSTHROUGH		AFMT_AC3
350 #define AFMT_PASSTHROUGH_RATE		48000
351 #define AFMT_PASSTHROUGH_CHANNEL	2
352 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
353 
354 /*
355  * We're simply using unused, contiguous bits from various AFMT_ definitions.
356  * ~(0xb00ff7ff)
357  */
358 #define AFMT_ENCODING_MASK	0xf00fffff
359 #define AFMT_CHANNEL_MASK	0x07f00000
360 #define AFMT_CHANNEL_SHIFT	20
361 #define AFMT_CHANNEL_MAX	0x7f
362 #define AFMT_EXTCHANNEL_MASK	0x08000000
363 #define AFMT_EXTCHANNEL_SHIFT	27
364 #define AFMT_EXTCHANNEL_MAX	1
365 
366 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
367 
368 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
369 				AFMT_EXTCHANNEL_SHIFT)
370 
371 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
372 				AFMT_CHANNEL_SHIFT)
373 
374 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
375 				(((v) & AFMT_24BIT) ? 24 :		\
376 				((((v) & AFMT_16BIT) ||			\
377 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
378 
379 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
380 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
381 
382 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
383 				(((c) << AFMT_CHANNEL_SHIFT) &		\
384 				AFMT_CHANNEL_MASK) |			\
385 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
386 				AFMT_EXTCHANNEL_MASK))
387 
388 #define AFMT_U8_NE	AFMT_U8
389 #define AFMT_S8_NE	AFMT_S8
390 
391 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | \
392 			AFMT_S32_NE | AFMT_F32_NE)
393 
394 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
395 			 AFMT_U24_NE | AFMT_U32_NE)
396 
397 #endif	/* _OS_H_ */
398