xref: /freebsd/sys/dev/sound/pcm/sound.h (revision b985c9ca)
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  *
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 /*
32  * first, include kernel header files.
33  */
34 
35 #ifndef _OS_H_
36 #define _OS_H_
37 
38 #ifdef _KERNEL
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/eventhandler.h>
42 #include <sys/ioccom.h>
43 #include <sys/filio.h>
44 #include <sys/sockio.h>
45 #include <sys/fcntl.h>
46 #include <sys/selinfo.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h> /* for DATA_SET */
49 #include <sys/module.h>
50 #include <sys/conf.h>
51 #include <sys/file.h>
52 #include <sys/uio.h>
53 #include <sys/syslog.h>
54 #include <sys/errno.h>
55 #include <sys/malloc.h>
56 #include <sys/bus.h>
57 #include <machine/resource.h>
58 #include <machine/bus.h>
59 #include <sys/rman.h>
60 #include <sys/limits.h>
61 #include <sys/mman.h>
62 #include <sys/poll.h>
63 #include <sys/sbuf.h>
64 #include <sys/soundcard.h>
65 #include <sys/sndstat.h>
66 #include <sys/sysctl.h>
67 #include <sys/kobj.h>
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 
71 #include <sys/lock.h>
72 #include <sys/mutex.h>
73 #include <sys/condvar.h>
74 
75 #ifndef KOBJMETHOD_END
76 #define KOBJMETHOD_END	{ NULL, NULL }
77 #endif
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/matrix_map.h>
87 #include <dev/sound/pcm/channel.h>
88 #include <dev/sound/pcm/feeder.h>
89 #include <dev/sound/pcm/mixer.h>
90 #include <dev/sound/pcm/dsp.h>
91 
92 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
93 
94 #define SND_STATUSLEN	64
95 
96 #define SOUND_MODVER	5
97 
98 #define SOUND_MINVER	SOUND_MODVER
99 #define SOUND_PREFVER	SOUND_MODVER
100 #define SOUND_MAXVER	SOUND_MODVER
101 
102 /*
103  * By design, limit possible channels for each direction.
104  */
105 #define SND_MAXHWCHAN		256
106 #define SND_MAXVCHANS		SND_MAXHWCHAN
107 
108 #define SD_F_SIMPLEX		0x00000001
109 #define SD_F_AUTOVCHAN		0x00000002
110 #define SD_F_SOFTPCMVOL		0x00000004
111 #define SD_F_DYING		0x00000008
112 #define SD_F_DETACHING		0x00000010
113 #define SD_F_BUSY		0x00000020
114 #define SD_F_MPSAFE		0x00000040
115 #define SD_F_REGISTERED		0x00000080
116 #define SD_F_BITPERFECT		0x00000100
117 #define SD_F_VPC		0x00000200	/* volume-per-channel */
118 #define SD_F_EQ			0x00000400	/* EQ */
119 #define SD_F_EQ_ENABLED		0x00000800	/* EQ enabled */
120 #define SD_F_EQ_BYPASSED	0x00001000	/* EQ bypassed */
121 #define SD_F_EQ_PC		0x00002000	/* EQ per-channel */
122 
123 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
124 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
125 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
126 
127 #define SD_F_PRIO_RD		0x10000000
128 #define SD_F_PRIO_WR		0x20000000
129 #define SD_F_PRIO_SET		(SD_F_PRIO_RD | SD_F_PRIO_WR)
130 #define SD_F_DIR_SET		0x40000000
131 #define SD_F_TRANSIENT		0xf0000000
132 
133 #define SD_F_BITS		"\020"					\
134 				"\001SIMPLEX"				\
135 				"\002AUTOVCHAN"				\
136 				"\003SOFTPCMVOL"			\
137 				"\004DYING"				\
138 				"\005DETACHING"				\
139 				"\006BUSY"				\
140 				"\007MPSAFE"				\
141 				"\010REGISTERED"			\
142 				"\011BITPERFECT"			\
143 				"\012VPC"				\
144 				"\013EQ"				\
145 				"\014EQ_ENABLED"			\
146 				"\015EQ_BYPASSED"			\
147 				"\016EQ_PC"				\
148 				"\035PRIO_RD"				\
149 				"\036PRIO_WR"				\
150 				"\037DIR_SET"
151 
152 #define PCM_ALIVE(x)		((x) != NULL && (x)->lock != NULL &&	\
153 				 !((x)->flags & SD_F_DYING))
154 #define PCM_REGISTERED(x)	(PCM_ALIVE(x) &&			\
155 				 ((x)->flags & SD_F_REGISTERED))
156 
157 #define	PCM_DETACHING(x)	((x)->flags & SD_F_DETACHING)
158 
159 #define	PCM_CHANCOUNT(d)	\
160 	(d->playcount + d->pvchancount + d->reccount + d->rvchancount)
161 
162 /* many variables should be reduced to a range. Here define a macro */
163 #define RANGE(var, low, high) (var) = \
164 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
165 #define DSP_BUFFSIZE (8192)
166 
167 /* make figuring out what a format is easier. got AFMT_STEREO already */
168 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
169 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
170 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
171 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
172 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
173 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
174 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
175 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
176 			AFMT_S16_BE | AFMT_U16_BE)
177 
178 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
179 				 AFMT_32BIT)
180 
181 /* Supported vchan mixing formats */
182 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
183 
184 #define AFMT_PASSTHROUGH		AFMT_AC3
185 #define AFMT_PASSTHROUGH_RATE		48000
186 #define AFMT_PASSTHROUGH_CHANNEL	2
187 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
188 
189 /*
190  * We're simply using unused, contiguous bits from various AFMT_ definitions.
191  * ~(0xb00ff7ff)
192  */
193 #define AFMT_ENCODING_MASK	0xf00fffff
194 #define AFMT_CHANNEL_MASK	0x07f00000
195 #define AFMT_CHANNEL_SHIFT	20
196 #define AFMT_CHANNEL_MAX	0x7f
197 #define AFMT_EXTCHANNEL_MASK	0x08000000
198 #define AFMT_EXTCHANNEL_SHIFT	27
199 #define AFMT_EXTCHANNEL_MAX	1
200 
201 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
202 
203 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
204 				AFMT_EXTCHANNEL_SHIFT)
205 
206 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
207 				AFMT_CHANNEL_SHIFT)
208 
209 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
210 				(((v) & AFMT_24BIT) ? 24 :		\
211 				((((v) & AFMT_16BIT) ||			\
212 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
213 
214 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
215 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
216 
217 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
218 				(((c) << AFMT_CHANNEL_SHIFT) &		\
219 				AFMT_CHANNEL_MASK) |			\
220 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
221 				AFMT_EXTCHANNEL_MASK))
222 
223 #define AFMT_U8_NE	AFMT_U8
224 #define AFMT_S8_NE	AFMT_S8
225 
226 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
227 
228 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
229 			 AFMT_U24_NE | AFMT_U32_NE)
230 
231 /*
232  * Minor numbers for the sound driver.
233  *
234  * Unfortunately Creative called the codec chip of SB as a DSP. For this
235  * reason the /dev/dsp is reserved for digitized audio use. There is a
236  * device for true DSP processors but it will be called something else.
237  * In v3.0 it's /dev/sndproc but this could be a temporary solution.
238  */
239 
240 #define SND_DEV_CTL	0	/* Control port /dev/mixer */
241 #define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
242 #define SND_DEV_MIDIN	2	/* Raw midi access */
243 #define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
244 #define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
245 #define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
246 #define SND_DEV_STATUS	6	/* /dev/sndstat */
247 				/* #7 not in use now. */
248 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
249 #define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
250 #define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
251 #define SND_DEV_NORESET	10
252 
253 #define SND_DEV_DSPHW_PLAY	11	/* specific playback channel */
254 #define SND_DEV_DSPHW_VPLAY	12	/* specific virtual playback channel */
255 #define SND_DEV_DSPHW_REC	13	/* specific record channel */
256 #define SND_DEV_DSPHW_VREC	14	/* specific virtual record channel */
257 
258 #define SND_DEV_DSPHW_CD	15	/* s16le/stereo 44100Hz CD */
259 
260 /*
261  * OSSv4 compatible device. For now, it serve no purpose and
262  * the cloning itself will forward the request to ordinary /dev/dsp
263  * instead.
264  */
265 #define SND_DEV_DSP_MMAP	16	/* /dev/dsp_mmap     */
266 #define SND_DEV_DSP_AC3		17	/* /dev/dsp_ac3      */
267 #define SND_DEV_DSP_MULTICH	18	/* /dev/dsp_multich  */
268 #define SND_DEV_DSP_SPDIFOUT	19	/* /dev/dsp_spdifout */
269 #define SND_DEV_DSP_SPDIFIN	20	/* /dev/dsp_spdifin  */
270 
271 #define DSP_DEFAULT_SPEED	8000
272 
273 #define ON		1
274 #define OFF		0
275 
276 extern int pcm_veto_load;
277 extern int snd_unit;
278 extern int snd_maxautovchans;
279 extern int snd_verbose;
280 extern devclass_t pcm_devclass;
281 extern struct unrhdr *pcmsg_unrhdr;
282 
283 /*
284  * some macros for debugging purposes
285  * DDB/DEB to enable/disable debugging stuff
286  * BVDDB   to enable debugging when bootverbose
287  */
288 #define BVDDB(x) if (bootverbose) x
289 
290 #ifndef DEB
291 #define DEB(x)
292 #endif
293 
294 SYSCTL_DECL(_hw_snd);
295 
296 int pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num);
297 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
298     pid_t pid, char *comm);
299 
300 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo);
301 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
302 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
303 
304 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
305 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
306 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
307 int pcm_unregister(device_t dev);
308 int pcm_setstatus(device_t dev, char *str);
309 u_int32_t pcm_getflags(device_t dev);
310 void pcm_setflags(device_t dev, u_int32_t val);
311 void *pcm_getdevinfo(device_t dev);
312 
313 int snd_setup_intr(device_t dev, struct resource *res, int flags,
314 		   driver_intr_t hand, void *param, void **cookiep);
315 
316 void *snd_mtxcreate(const char *desc, const char *type);
317 void snd_mtxfree(void *m);
318 void snd_mtxassert(void *m);
319 #define	snd_mtxlock(m) mtx_lock(m)
320 #define	snd_mtxunlock(m) mtx_unlock(m)
321 
322 int sndstat_register(device_t dev, char *str);
323 int sndstat_unregister(device_t dev);
324 
325 /*
326  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
327  * so that the macro versions of pcm_{,un}lock can dereference them.
328  * we also have to do this now makedev() has gone away.
329  */
330 
331 struct snddev_info {
332 	struct {
333 		struct {
334 			SLIST_HEAD(, pcm_channel) head;
335 			struct {
336 				SLIST_HEAD(, pcm_channel) head;
337 			} busy;
338 			struct {
339 				SLIST_HEAD(, pcm_channel) head;
340 			} opened;
341 		} pcm;
342 	} channels;
343 	unsigned playcount, reccount, pvchancount, rvchancount;
344 	unsigned flags;
345 	unsigned int bufsz;
346 	void *devinfo;
347 	device_t dev;
348 	char status[SND_STATUSLEN];
349 	struct mtx *lock;
350 	struct cdev *mixer_dev;
351 	struct cdev *dsp_dev;
352 	uint32_t pvchanrate, pvchanformat;
353 	uint32_t rvchanrate, rvchanformat;
354 	int32_t eqpreamp;
355 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
356 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
357 	struct cv cv;
358 };
359 
360 void	sound_oss_sysinfo(oss_sysinfo *);
361 int	sound_oss_card_info(oss_card_info *);
362 
363 #define	PCM_MODE_MIXER		0x01
364 #define	PCM_MODE_PLAY		0x02
365 #define	PCM_MODE_REC		0x04
366 
367 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
368 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
369 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
370 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
371 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
372 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
373 
374 /*
375  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
376  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
377  */
378 #ifdef SND_DIAGNOSTIC
379 #define PCM_WAIT(x)		do {					\
380 	if (!PCM_LOCKOWNED(x))						\
381 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
382 		    __func__, __LINE__);				\
383 	while ((x)->flags & SD_F_BUSY) {				\
384 		if (snd_verbose > 3)					\
385 			device_printf((x)->dev,				\
386 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
387 			    __func__, __LINE__);			\
388 		cv_wait(&(x)->cv, (x)->lock);				\
389 	}								\
390 } while (0)
391 
392 #define PCM_ACQUIRE(x)		do {					\
393 	if (!PCM_LOCKOWNED(x))						\
394 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
395 		    __func__, __LINE__);				\
396 	if ((x)->flags & SD_F_BUSY)					\
397 		panic("%s(%d): [PCM ACQUIRE] "				\
398 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
399 	(x)->flags |= SD_F_BUSY;					\
400 } while (0)
401 
402 #define PCM_RELEASE(x)		do {					\
403 	if (!PCM_LOCKOWNED(x))						\
404 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
405 		    __func__, __LINE__);				\
406 	if ((x)->flags & SD_F_BUSY) {					\
407 		(x)->flags &= ~SD_F_BUSY;				\
408 		if ((x)->cv.cv_waiters != 0) {				\
409 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
410 				device_printf((x)->dev,			\
411 				    "%s(%d): [PCM RELEASE] "		\
412 				    "cv_waiters=%d > 1!\n",		\
413 				    __func__, __LINE__,			\
414 				    (x)->cv.cv_waiters);		\
415 			cv_broadcast(&(x)->cv);				\
416 		}							\
417 	} else								\
418 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
419 		    __func__, __LINE__);				\
420 } while (0)
421 
422 /* Quick version, for shorter path. */
423 #define PCM_ACQUIRE_QUICK(x)	do {					\
424 	if (PCM_LOCKOWNED(x))						\
425 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
426 		    __func__, __LINE__);				\
427 	PCM_LOCK(x);							\
428 	PCM_WAIT(x);							\
429 	PCM_ACQUIRE(x);							\
430 	PCM_UNLOCK(x);							\
431 } while (0)
432 
433 #define PCM_RELEASE_QUICK(x)	do {					\
434 	if (PCM_LOCKOWNED(x))						\
435 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
436 		    __func__, __LINE__);				\
437 	PCM_LOCK(x);							\
438 	PCM_RELEASE(x);							\
439 	PCM_UNLOCK(x);							\
440 } while (0)
441 
442 #define PCM_BUSYASSERT(x)	do {					\
443 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
444 		panic("%s(%d): [PCM BUSYASSERT] "			\
445 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
446 } while (0)
447 
448 #define PCM_GIANT_ENTER(x)	do {					\
449 	int _pcm_giant = 0;						\
450 	if (PCM_LOCKOWNED(x))						\
451 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
452 		    __func__, __LINE__);				\
453 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
454 		device_printf((x)->dev,					\
455 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
456 		    __func__, __LINE__);				\
457 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
458 		do {							\
459 			mtx_lock(&Giant);				\
460 			_pcm_giant = 1;					\
461 		} while (0)
462 
463 #define PCM_GIANT_EXIT(x)	do {					\
464 	if (PCM_LOCKOWNED(x))						\
465 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
466 		    __func__, __LINE__);				\
467 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
468 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
469 		    __func__, __LINE__);				\
470 	if ((x)->flags & SD_F_MPSAFE) {					\
471 		if (_pcm_giant == 1)					\
472 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
473 			    __func__, __LINE__);			\
474 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
475 			device_printf((x)->dev,				\
476 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
477 			    __func__, __LINE__);			\
478 	}								\
479 	if (_pcm_giant != 0) {						\
480 		if (mtx_owned(&Giant) == 0)				\
481 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
482 			    __func__, __LINE__);			\
483 		_pcm_giant = 0;						\
484 		mtx_unlock(&Giant);					\
485 	}								\
486 } while (0)
487 #else /* !SND_DIAGNOSTIC */
488 #define PCM_WAIT(x)		do {					\
489 	PCM_LOCKASSERT(x);						\
490 	while ((x)->flags & SD_F_BUSY)					\
491 		cv_wait(&(x)->cv, (x)->lock);				\
492 } while (0)
493 
494 #define PCM_ACQUIRE(x)		do {					\
495 	PCM_LOCKASSERT(x);						\
496 	KASSERT(!((x)->flags & SD_F_BUSY),				\
497 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
498 	    __func__, __LINE__));					\
499 	(x)->flags |= SD_F_BUSY;					\
500 } while (0)
501 
502 #define PCM_RELEASE(x)		do {					\
503 	PCM_LOCKASSERT(x);						\
504 	KASSERT((x)->flags & SD_F_BUSY,					\
505 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
506 	    __func__, __LINE__));					\
507 	(x)->flags &= ~SD_F_BUSY;					\
508 	if ((x)->cv.cv_waiters != 0)					\
509 		cv_broadcast(&(x)->cv);					\
510 } while (0)
511 
512 /* Quick version, for shorter path. */
513 #define PCM_ACQUIRE_QUICK(x)	do {					\
514 	PCM_UNLOCKASSERT(x);						\
515 	PCM_LOCK(x);							\
516 	PCM_WAIT(x);							\
517 	PCM_ACQUIRE(x);							\
518 	PCM_UNLOCK(x);							\
519 } while (0)
520 
521 #define PCM_RELEASE_QUICK(x)	do {					\
522 	PCM_UNLOCKASSERT(x);						\
523 	PCM_LOCK(x);							\
524 	PCM_RELEASE(x);							\
525 	PCM_UNLOCK(x);							\
526 } while (0)
527 
528 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
529 				    ((x)->flags & SD_F_BUSY),		\
530 				    ("%s(%d): [PCM BUSYASSERT] "	\
531 				    "Failed, snddev_info=%p",		\
532 				    __func__, __LINE__, x))
533 
534 #define PCM_GIANT_ENTER(x)	do {					\
535 	int _pcm_giant = 0;						\
536 	PCM_UNLOCKASSERT(x);						\
537 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
538 		do {							\
539 			mtx_lock(&Giant);				\
540 			_pcm_giant = 1;					\
541 		} while (0)
542 
543 #define PCM_GIANT_EXIT(x)	do {					\
544 	PCM_UNLOCKASSERT(x);						\
545 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
546 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
547 	    __func__, __LINE__));					\
548 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
549 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
550 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
551 	    __func__, __LINE__));					\
552 	if (_pcm_giant != 0) {						\
553 		mtx_assert(&Giant, MA_OWNED);				\
554 		_pcm_giant = 0;						\
555 		mtx_unlock(&Giant);					\
556 	}								\
557 } while (0)
558 #endif /* SND_DIAGNOSTIC */
559 
560 #define PCM_GIANT_LEAVE(x)						\
561 	PCM_GIANT_EXIT(x);						\
562 } while (0)
563 
564 #endif /* _KERNEL */
565 
566 #endif	/* _OS_H_ */
567